A simple question motivated by a curiosity, with probably a complex answer: Is it possible to emulate the new PHP 5.5 imagecrop() in earlier versions, like 5.4, by combining other GD functions?
Awn.. But without the imagecrop() black line bug, please. :p
The imagecrop() function is an inbuilt function in PHP which is used to crop an image to the given rectangle. This function crops an image to the given rectangular area and returns the resulting image.
Returns a cropped image object on success or false on failure. If the complete image was cropped, imagecrop () returns false . image expects a GdImage instance now; previously, a resource was expected. On success, this function returns a GDImage instance now; previously, a resource was returned.
As noted in the return value section, imagecropauto () returns false if the whole image was cropped. In this example we have an image object $im which should be automatically cropped only if there is something to crop; otherwise we want to proceed with the original image.
Automatically crops an image according to the given mode . A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor (). One of the following constants: Same as IMG_CROP_TRANSPARENT. Before PHP 7.4.0, the bundled libgd fell back to IMG_CROP_SIDES, if the image had no transparent color.
This should be a drop-in replacement for imagecrop()
(without the bug...):
function mycrop($src, array $rect)
{
$dest = imagecreatetruecolor($rect['width'], $rect['height']);
imagecopy(
$dest,
$src,
0,
0,
$rect['x'],
$rect['y'],
$rect['width'],
$rect['height']
);
return $dest;
}
Usage:
$img = mycrop($img, ['x' => 10, 'y' => 10, 'width' => 100, 'height' => 100]);
Note that the bug is apparently fixed in PHP 5.6.12.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With