I'd like crop an image in PHP and save the file. I know your supposed to use the GD library but i'm not sure how. Any ideas?
Thanks
I'd like crop an image in PHP and save the file. I know your supposed to use the GD library but i'm not sure how. Any ideas?
Thanks
You could use imagecopy
to crop a required part of an image. The command goes like this:
imagecopy ( resource $dst_im - the image object , resource $src_im - destination image , int $dst_x - x coordinate in the destination image (use 0) , int $dst_y - y coordinate in the destination image (use 0) , int $src_x - x coordinate in the source image you want to crop , int $src_y - y coordinate in the source image you want to crop , int $src_w - crop width , int $src_h - crop height )
Code from PHP.net - a 80x40 px image is cropped from a source image
<?php
// Create image instances
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);
// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);
// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
?>
Use imagecreatetruecolor and imagecopyresampled.
Here's a example:
//getting the center coordinate (if you want to crop from the center)
$startY = ($oldHeight - $newHeight)/2;
$startX = ($oldWidth - $newWidth)/2;
//old image you can also use: imagecreatefrompng or imagecreatefromjpeg
$oldImage = imagecreatefromgif($srcimg);
//create new image
list($width, $height) = getimagesize($srcimg);
$percent = 0.25;
$new_width = $width * $percent;
$new_height = $height * $percent;
$newImage = imagecreatetruecolor($newWidth, $newHeight);
//put old image on top of new image
imagecopyresampled($newImage, $oldImage, 0,0 , $startX, $startY, $newWidth, $newHeight, $oldWidth, $oldHeight);
To crop an image using GD you need to use a combination of GD methods, and if you look at "Example #1" on PHP's documentation of the imagecopyresampled
method, it shows you how to crop and output an image, you would just need to add some code to that to capture and write the output to a file...
http://us2.php.net/manual/en/function.imagecopyresampled.php
There are also other options, including Image Magick which, if installed on your server, can be accessed directly using PHP's exec
method (or similar) or you can install the PHP Imagick extension, which yields higher quality images and, in my opinion, is a little more intuitive and flexible to work with.
Finally, I've used the open source PHPThumb class library, which has a pretty simple interface and can work with multiple options depending on what's on your server, including ImageMagick and GD.
I use this script in some projects and it's pretty easy to use: http://shiftingpixel.com/2008/03/03/smart-image-resizer/
The script requires PHP 5.1.0 (which is out since 2005-11-24 - time to upgrade if not yet at this version) and GD (which is rarely missing from good Web hosts).
Here is an example of it's use in your HTML:
<img src="/image.php/coffee-bean.jpg?width=200&height=200&image=/wp-content/uploads/2008/03/coffee-bean.jpg" alt="Coffee Bean" />