我如何在php中使用imagick? (调整大小和裁剪) [英] how do i use imagick in php? (resize & crop)

查看:517
本文介绍了我如何在php中使用imagick? (调整大小和裁剪)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用imagick进行缩略图裁剪,但有时裁剪的缩略图缺少图像的顶部(头发,眼睛).

I use imagick for thumbnail crop, but sometimes cropped thumbnails are missing top part of the images (hair, eyes).

我正在考虑调整图像大小然后裁剪.另外,我需要保持图像尺寸比例.

I was thinking to resize the image then crop it. Also, I need to keep the image size ratio.

以下是我用于裁剪的php脚本:

Below is the php script I use for crop:

$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">';

谢谢..

推荐答案

此任务并不容易,因为重要"部分可能并不总是位于同一位置.仍然,使用类似的东西

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">';

(已测试)

应该工作. cropImage参数(0和0)确定裁剪区域的左上角.因此,使用这些功能可以为您保留图像中保留的内容的不同结果.

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.

这篇关于我如何在php中使用imagick? (调整大小和裁剪)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆