Tag: Image Cropping

Image cropping in PHP using GD library

// Load the image
$image = imagecreatefromjpeg('path/to/image.jpg');

// Set the crop dimensions
$crop_width = 200;
$crop_height = 200;
$x = 50; // starting x-coordinate of the crop
$y = 50; // starting y-coordinate of the crop

// Create the cropped image
$cropped_image = imagecrop($image, ['x' => $x, 'y' => $y, 'width' => $crop_width, 'height' => $crop_height]);

// Save the cropped image
imagejpeg($cropped_image, 'path/to/cropped-image.jpg', 100);

// Free up memory
imagedestroy($image);
imagedestroy($cropped_image);