Using PHP Image Processing Functions for Cropping, Resizing, Rotating, and Adding Watermarks

This article explains how PHP's image processing functions such as imagecrop, imagecopyresized, imagerotate, and imagestring can be used to crop, scale, rotate, and watermark images, providing code examples and best practices for memory management.

php Courses
php Courses
php Courses
Using PHP Image Processing Functions for Cropping, Resizing, Rotating, and Adding Watermarks

PHP image processing functions are a set of functions specifically for handling and editing images, providing developers with rich capabilities such as cropping, scaling, rotating, and adding watermarks to satisfy various image processing requirements.

First, we demonstrate how to use the imagecrop() function to crop an image. By passing the coordinates and size of the cropping area, the image can be cropped. Example code:

$sourceImage = imagecreatefromjpeg('source.jpg');
$croppedImage = imagecrop($sourceImage, ['x' => 50, 'y' => 50, 'width' => 200, 'height' => 200]);
imagejpeg($croppedImage, 'cropped.jpg');
imagedestroy($sourceImage);
imagedestroy($croppedImage);

In the code above, imagecreatefromjpeg() loads the source image, imagecrop() performs the cropping, imagejpeg() saves the cropped image, and imagedestroy() releases memory.

Next, we explore image scaling using the imagecopyresized() and imagecopyresampled() functions. Depending on the requirements, either function can be used. Example code:

$sourceImage = imagecreatefromjpeg('source.jpg');
$width = imagesx($sourceImage);
$height = imagesy($sourceImage);
$newWidth = $width * 0.5; // scale to half size
$newHeight = $height * 0.5;
$targetImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($targetImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($targetImage, 'resized.jpg');
imagedestroy($sourceImage);
imagedestroy($targetImage);

This code loads the source image with imagecreatefromjpeg(), obtains its dimensions, calculates the scaled dimensions, creates a target image, scales the source into the target using imagecopyresized(), and saves the result with imagejpeg().

Beyond cropping and scaling, PHP also supports rotation via imagerotate() and watermarking via imagestring(). Note that rotation may cause distortion, so it should be used cautiously.

Overall, PHP image processing functions provide developers with convenient tools for editing images, enabling easy implementation of cropping, scaling, rotation, and watermarking while emphasizing proper error handling and memory management to avoid resource waste and leaks.

PHP Learning Recommendations:

Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Expert Recommended Course

Workerman+TP6 Real‑time Chat System Limited‑time Offer

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Image ProcessingWatermarkPHProtationcroppingresizing
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.