Re: cropping an image script
| From: | Tom Rogers | Date: | Thu, 13 Mar 2003 17:49:03 +0000 |
| Subject: | Re: cropping an image script | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-139565@lists.php.net to get a copy of this message | ||
Hi,
Friday, March 14, 2003, 2:07:28 AM, you wrote:
AR> In the following php script, the function takes the original file and crops
AR> to the destination file.
AR> I've ran the script using a .png file and a .jpg file.
AR> The .png works fine whereas the .jpeg file returns only part of the image
AR> along with a blue rectangle.
AR> For instance - where the original image is a 200 x 300 jpeg called:
AR> 1_data.jpeg
AR> and the destination iamge is 75x75 called:
AR> 2_data.jpeg
AR> and starting at the x,y coordinates of 0,0 - the script returns a 75x 75
AR> file but only a sliver of the .jpg image is within the 75x75 blue square.
AR> Any help will be of assistance.
AR> Thank you.
AR> Tony Ritter
AR> ........................................................
AR> The following is the php cropping script (with author credit):
AR> <?
AR> //
AR> // image crop fuction ..... Duck@obala.net
AR> // info at http://obala.net/en
AR> //
AR> // Yes the proses could be executed with imagecopyresized build-in function
AR> // but with this function you get the idea of how an image color set is
AR> constructed
AR> //
AR> // $cropX = source starting X
AR> // $cropY = source starting Y
AR> // $cropW = crop weigth
AR> // $cropH = crop heigh
AR> // $source = source filename
AR> // $destination = destination filename
AR> // $type = image type
AR> function
AR> im_crop($cropX,$cropY,$cropH,$cropW,$source,$destination,$type='jpeg') {
AR> // switch known types and open source image
AR> switch ($type) {
AR> case 'wbmp': $sim = ImageCreateFromWBMP($source); break;
AR> case 'gif': $sim = ImageCreateFromGIF($source); break;
AR> case 'png': $sim = ImageCreateFromPNG($source); break;
AR> default: $sim = ImageCreateFromJPEG($source); break;
AR> }
AR> // create the destination image
AR> $dim = imagecreate($cropW, $cropH);
AR> // pass trought pixles of source image
AR> for ( $i=$cropY; $i<($cropY+$cropH); $i++ ) {
AR> for ( $j=$cropX; $j<($cropX+$cropW); $j++ ) {
AR> // get RGB color info about a pixel in the source image
AR> $color = imagecolorsforindex($sim, imagecolorat($sim, $j, $i));
AR> // insert the color in the color index of the new image
AR> $index = ImageColorAllocate($dim, $color['red'],
AR> $color['green'], $color['blue']);
AR> // plots a pixel in the new image with that color
AR> imagesetpixel($dim, $j-$cropX, $i-$cropY, $index);
AR> }
AR> }
AR> // whe dont need the sorce image anymore
AR> imagedestroy($sim);
AR> // switch known types and save
AR> switch ($type) {
AR> case 'wbmp': ImageWBMP($dim, $destination); break;
AR> case 'gif': ImageGIF($dim, $destination); break;
AR> case 'png': ImagePNG($dim, $destination); break;
AR> default: ImageJPEG($dim, $destination); break;
AR> }
AR> // free the used space of the source image
AR> imagedestroy($dim);
AR> }
AR> // example
AR> im_crop(0,0,75,75,'1_data.jpeg','2_data.jpeg','jpeg');
?>>
With jpegs you need to use $im2 = imagecreatetruecolor($x,$y)
and just write the pixel back using imagesetpixel($im, $x, $y, $p);
like this:
$im =imagecreatefromjpeg($filename);
$dim = imagecreatetruecolor($cropW, $cropH);
for ( $i=$cropY; $i<($cropY+$cropH); $i++ ) {
for ( $j=$cropX; $j<($cropX+$cropW); $j++ ) {
$pixel = imagecolorat($im, $x, $y);
imagesetpixel($dim, $j-$cropX, $i-$cropY, $pixel);
}
}
As it is true colour no need to use indexes.
--
regards,
Tom