©
This document uses PHP Chinese website manual Release
(PECL imagick 2.0.0)
Imagick::cropImage — 截图图片的一块区域
$width
, int $height
, int $x
, int $y
)截图图片的一块区域
width
截图的宽度
height
截图的高度
x
裁剪区域左上角的 X 轴坐标(以原图的左上角为原点)
y
裁剪区域左上角的 X 轴坐标(以原图的左上角为原点)
成功时返回 TRUE
。
错误时抛出 ImagickException。
Example #1 Imagick::cropImage()
<?php
function cropImage ( $imagePath , $startX , $startY , $width , $height ) {
$imagick = new \ Imagick ( realpath ( $imagePath ));
$imagick -> cropImage ( $width , $height , $startX , $startY );
header ( "Content-Type: image/jpg" );
echo $imagick -> getImageBlob ();
}
?>
[#1] oxxido at gmail dot com [2015-04-13 17:58:24]
I have a function that takes an image, resize and crop it, and save it as normal, then resize it again and crop it again to create the thumbnail. The numbers of the second crop were WAY off, and the calculations were perfect, the problem, was the second crop wasn't resetting the imagePage, so if you try to crop the same image twice, it will be a good idea to reset it first:
<?php
$thumb = new Imagick($file)
$thumb->resizeImage($r_w1,$r_h1,Imagick::FILTER_CATROM,0.9, false);
$thumb->cropImage($w1,$h1,$l1,$t1);
$thumb->writeImage($destinationPath.'/'.$fileName);
$thumb->resizeImage($r_w2,$r_h2,Imagick::FILTER_CATROM,0.9, false);
$thumb->setImagePage(0, 0, 0, 0);
$thumb->cropImage($w2,$h2,$l2,$t2);
$thumb->writeImage($destinationPath.'/'.$fileNameThumb);
?>
BTW, i needed perfect dimentions so i had to set the "bestfit" to false.
[#2] ElPadre [2010-11-05 08:55:40]
Actually, the Imagick::setImagePage(0,0,0,0) is also handy with jpgs and pngs, if you plan to do any more changes on the cropped image that involves positioning and/or gravity (I created a script that does crop, face blur and watermarking in one go, and had a hell of a time determining why the blurs and the watermark text never showed up...).
[#3] Christian Dehning [2010-04-09 03:57:52]
When cropping gif-images (I had no problems with jpg and png images), the canvas is not removed. Please run the following command on the cropped gif, to remove the blank space:
$im->setImagePage(0, 0, 0, 0);
[#4] vincent dot hoen at gmail dot com [2007-08-02 06:35:46]
There is an easiest way to crop an image :
$picture = new Imagick('animated_gif.gif');
foreach($picture as $frame){
$frame->cropImage($width, $height, $x, $y);
}