note 116106 deleted from function.imagecrop by salathe
| From: | salathe@php.net | Date: | Wed, 12 Nov 2014 14:44:05 +0000 |
| Subject: | note 116106 deleted from function.imagecrop by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-200887@lists.php.net to get a copy of this message | ||
Note Submitter: me at milandavidek dot cz
----
Here is my quick implementation for older versions of PHP:
if(!function_exists('imagecrop')) {
function imagecrop($image, array $rect) {
$radiusX = $rect['width'] / 2;
$radiusY = $rect['height'] / 2;
$center_x = $rect['x'] + $radiusX - 1;
$center_y = $rect['y'] + $radiusY - 1;
$radiusXsqr = $radiusX * $radiusX;
$radiusYsqr = $radiusY * $radiusY;
$max_x = min($image_width = imagesx($image), $rect['x'] +
$rect['width']) - 1;
$max_y = min($image_height = imagesy($image), $rect['y'] +
$rect['height']) - 1;
$copy = imagecreatetruecolor($image_width, $image_height);
imagecopy($copy, $image, 0, 0, 0, 0, $image_width, $image_height); //make copy of image
imagefilledrectangle($image, 0, 0, $image_width - 1, $image_height - 1,
imagecolorallocate($image, 0, 0, 0)); //fill the image with black color
imagesavealpha($image, true);
imagefill($image, 0, 0, imagecolorallocatealpha($image, 0, 0, 0, 127)); //replace the black
with transparent
//copy back to image only pixels inside ellipse
for ($x = max($rect['x'], 0); $x <= $max_x; $x++) {
for ($y = max($rect['y'], 0); $y <= $max_y; $y++) {
if(pow($x - $center_x, 2) / $radiusXsqr + pow($y - $center_y, 2) / $radiusYsqr <=
1) { //pixel is inside of ellipse
imagesetpixel($image, $x, $y, imagecolorat($copy, $x, $y)); //copy the pixel
}
}
}
imagedestroy($copy);
}
}