note 55607 added to function.imagesetpixel
| From: | weitheismatgmail dot com at osu1 dot php dot net | Date: | Mon, 08 Aug 2005 19:22:25 +0000 |
| Subject: | note 55607 added to function.imagesetpixel | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93269@lists.php.net to get a copy of this message | ||
I was looking for a way to actually DELETE pixels or squares or parts of an image from an image
resource, and at first I thought imagesetpixel would do the trick. Unfortunately, it merely paints
over that one pixel, and as far as I knew, php didn't have any native way of deleting sections
out of your image - so this little method should take care of deleting rectangular parts of your
pictures!
function deleteRectangle(&$oldImage,$leftX,$leftY,$rightX,$rightY)
{
// Since php has no native way of delete parts of images
// We have to divide the image into four different parts and then copy them manually to a new
// image
$xSize = imagesx($oldImage);
$ySize = imagesy($oldImage);
// Divides the image into four sections to copy
$imagesection = array();
$imagesection[] = array(0,0,$leftX,$ySize);
$imagesection[] = array($leftX,0,$rightX+1,$leftY);
$imagesection[] = array($leftX,$rightY+1,$rightX+1,$ySize);
$imagesection[] = array($rightX+1,0,$xSize,$ySize);
// Create the new, copied image
$newImage = imagecreatetruecolor($xSize,$ySize);
// Vital for transparency
imagesavealpha($newImage,true);
// Fills the background a transparent color
$transparentBackground = imagecolorallocatealpha($newImage,255,255,255,127);
imagefill($newImage,0,0,$transparentBackground);
// Copies each of the four imagesections into their respective old positions
for($i = 0;$i<count($imagesection);$i++)
imagecopyresampled($newImage,$oldImage, $imagesection[$i][0],$imagesection[$i][1],
$imagesection[$i][0],$imagesection[$i][1], $imagesection[$i][2]-$imagesection[$i][0],
$imagesection[$i][3]-$imagesection[$i][1], $imagesection[$i][2]-$imagesection[$i][0],
$imagesection[$i][3]-$imagesection[$i][1]);
// Alternately you can cycle through each pixel in an image and see if that pixel is an the area
// but that could be more intensive
imagedestroy($oldImage);
// Sets the old image equal to the new, cleared image
$oldImage = $newImage;
}
It was made with a transparent background in mind, but you could easily change that by changeing
imagecreatetruecolor to imagecreate and deleting the code that deals with transparency. Hope it
helps!
----
Manual Page -- http://www.php.net/manual/en/function.imagesetpixel.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55607
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55607&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55607&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55607&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55607&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55607&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55607&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55607&report=yes
Search -- http://master.php.net/manage/user-notes.php