10
How to Crop Images with PHP GD and Imagick
source link: https://www.devdungeon.com/content/how-crop-images-php-gd-and-imagick
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to Crop Images with PHP GD and Imagick
Submitted by NanoDano on Mon, 07/27/2015 - 17:47
Two common libraries for image manipulation with PHP are GD and Imagick. GD typically comes with most PHP setups. Here are some code snippets that demonstrate how to crop an image with both libraries.
$src_img = imagecreatefrompng('300x200.png');
if(!$src_img) {
die('Error when reading the source image.');
}
$thumbnail = imagecreatetruecolor(200, 200);
if(!$thumbnail) {
die('Error when creating the destination image.');
}
// Take 200x200 from 200x200 starting at 50,0
$result = imagecopyresampled($thumbnail, $src_img, 0, 0, 50, 0, 200, 200, 200, 200);
if(!$result) {
die('Error when generating the thumbnail.');
}
$result = imagejpeg($thumbnail, '200x200gd.png');
if(!$result) {
die('Error when saving the thumbnail.');
}
$result = imagedestroy($thumbnail);
if(!$result) {
die('Error when destroying the image.');
}
Imagick
$inFile = "300x200.png";
$outFile = "200x200_imagick.png";
$image = new Imagick($inFile);
$image->cropImage(200,200, 50,0);
$image->writeImage($outFile);
Tags:
Advertisement
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK