Current Location: Home> Latest Articles> PHP Image Processing Techniques: How to Implement Image Upload and Editing with PHP and CGI

PHP Image Processing Techniques: How to Implement Image Upload and Editing with PHP and CGI

M66 2025-06-19

Introduction

With the rapid development of the internet and mobile applications, the demand for image processing techniques has increased significantly. PHP and CGI are two commonly used technologies for implementing image upload and editing functionalities. This article will provide detailed instructions on how to use PHP and CGI to implement image upload, cropping, scaling, watermarking, and other editing features, along with code examples.

1. Image Upload

Let’s first look at how to implement the image upload functionality. Users can select and upload their images through an upload form. In PHP, we use the $_FILES array to retrieve information about the uploaded file.

Code Example:

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="image">
    <input type="submit" value="Upload">
</form>

In the "upload.php" file, we can use the move_uploaded_file function to move the uploaded image to the desired directory.

Code Example:

$target_dir = "uploads/";  // Specify the directory for uploaded files
$target_file = $target_dir . basename($_FILES["image"]["name"]);  // Get the filename of the uploaded file
move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);  // Move the file from the temporary directory to the target directory

With the code above, we can implement a basic image upload feature.

2. Image Editing

For image editing, we can use the GD library, a powerful open-source library for image processing in PHP. GD supports various operations such as cropping, scaling, and adding watermarks.

First, we open and read an image using the GD library.

Code Example:

$source_image = imagecreatefromjpeg("uploads/image.jpg");  // Open and read the image

Next, we can perform various editing operations on the image, such as cropping, scaling, and adding a watermark.

Image Cropping

We can use the imagecrop function to crop an image.

Code Example:

$cropped_image = imagecrop($source_image, ["x" => 100, "y" => 100, "width" => 300, "height" => 200]);  // Crop the image

Image Scaling

We can use the imagescale function to scale the image.

Code Example:

$scale_image = imagescale($source_image, 500, 300);  // Scale the image

Adding a Watermark

We can use the imagecopy function to add a watermark to the image.

Code Example:

$watermark_image = imagecreatefrompng("watermark.png");  // Open and read the watermark image
$watermark_width = imagesx($watermark_image);
$watermark_height = imagesy($watermark_image);
imagecopy($source_image, $watermark_image, 0, 0, 0, 0, $watermark_width, $watermark_height);  // Add the watermark

Finally, we can save the edited image.

Code Example:

imagejpeg($source_image, "edited_image.jpg");  // Save the edited image

With the above steps, we can implement image editing functionalities, including cropping, scaling, and adding a watermark.

3. Conclusion

This article introduced how to implement image upload and editing functionalities using PHP and CGI, with relevant code examples. By implementing image upload and editing, we can better meet user demands for image processing and enhance the user experience for internet and mobile applications. We hope this article helps developers quickly grasp PHP image processing techniques.