我如何在 php 中使用 imagick?(调整大小和裁剪)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18312243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do i use imagick in php? (resize & crop)
提问by newworroo
I use imagick for thumbnail crop, but sometimes cropped thumbnails are missing top part of the images (hair, eyes).
我使用 imagick 进行缩略图裁剪,但有时裁剪后的缩略图会丢失图像的顶部(头发、眼睛)。
I was thinking to resize the image then crop it. Also, I need to keep the image size ratio.
我正在考虑调整图像大小然后裁剪它。另外,我需要保持图像大小比例。
Below is the php script I use for crop:
下面是我用于裁剪的 php 脚本:
$im = new imagick( "img/20130815233205-8.jpg" );
$im->cropThumbnailImage( 80, 80 );
$im->writeImage( "thumb/th_80x80_test.jpg" );
echo '<img src="thumb/th_80x80_test.jpg">';
Thanks..
谢谢..
回答by Martin Müller
This task is not easy as the "important" part may not always be at the same place. Still, using something like this
这项任务并不容易,因为“重要”部分可能并不总是在同一个地方。仍然,使用这样的东西
$im = new imagick("c:\temp\523764_169105429888246_1540489537_n.jpg");
$imageprops = $im->getImageGeometry();
$width = $imageprops['width'];
$height = $imageprops['height'];
if($width > $height){
$newHeight = 80;
$newWidth = (80 / $height) * $width;
}else{
$newWidth = 80;
$newHeight = (80 / $width) * $height;
}
$im->resizeImage($newWidth,$newHeight, imagick::FILTER_LANCZOS, 0.9, true);
$im->cropImage (80,80,0,0);
$im->writeImage( "D:\xampp\htdocs\th_80x80_test.jpg" );
echo '<img src="th_80x80_test.jpg">';
(tested)
(已测试)
should work. The cropImage parameters (0 and 0) determine the upper left corner of the cropping area. So playing with these gives you differnt results of what stays in the image.
应该管用。cropImage 参数(0 和 0)确定裁剪区域的左上角。因此,使用这些可以为您提供图像中保留内容的不同结果。
回答by Stanislav Agapov
Based on Martin's answerI made a more general function that resizes and crops Imagick
image to fit given width and height (i.e. behaves exactly as CSS background-size: cover
declaration):
基于Martin 的回答,我创建了一个更通用的函数,可以调整和裁剪Imagick
图像以适应给定的宽度和高度(即行为与 CSSbackground-size: cover
声明完全相同):
/**
* Resizes and crops $image to fit provided $width and $height.
*
* @param \Imagick $image
* Image to change.
* @param int $width
* New desired width.
* @param int $height
* New desired height.
*/
function image_cover(Imagick $image, $width, $height) {
$ratio = $width / $height;
// Original image dimensions.
$old_width = $image->getImageWidth();
$old_height = $image->getImageHeight();
$old_ratio = $old_width / $old_height;
// Determine new image dimensions to scale to.
// Also determine cropping coordinates.
if ($ratio > $old_ratio) {
$new_width = $width;
$new_height = $width / $old_width * $old_height;
$crop_x = 0;
$crop_y = intval(($new_height - $height) / 2);
}
else {
$new_width = $height / $old_height * $old_width;
$new_height = $height;
$crop_x = intval(($new_width - $width) / 2);
$crop_y = 0;
}
// Scale image to fit minimal of provided dimensions.
$image->resizeImage($new_width, $new_height, imagick::FILTER_LANCZOS, 0.9, true);
// Now crop image to exactly fit provided dimensions.
$image->cropImage($new_width, $new_height, $crop_x, $crop_y);
}
Hope this may help somebody.
希望这可以帮助某人。