note 116106 added to function.imagecrop
| From: | me at milandavidek dot cz | Date: | Mon, 10 Nov 2014 12:22:44 +0000 |
| Subject: | note 116106 added to function.imagecrop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200864@lists.php.net to get a copy of this message | ||
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);
}
}
----
Server IP: 72.52.91.14
Probable Submitter: 109.105.44.71
----
Manual Page -- http://php.net/manual/en/function.imagecrop.php
Edit -- https://master.php.net/note/edit/116106
Del: integrated -- https://master.php.net/note/delete/116106/integrated
Del: useless -- https://master.php.net/note/delete/116106/useless
Del: bad code -- https://master.php.net/note/delete/116106/bad+code
Del: spam -- https://master.php.net/note/delete/116106/spam
Del: non-english -- https://master.php.net/note/delete/116106/non-english
Del: in docs -- https://master.php.net/note/delete/116106/in+docs
Del: other reasons-- https://master.php.net/note/delete/116106
Reject -- https://master.php.net/note/reject/116106
Search -- https://master.php.net/manage/user-notes.php