【问题标题】:how do i use imagick in php? (resize & crop)我如何在 php 中使用 imagick? (调整大小和裁剪)
【发布时间】:2013-08-21 03:47:27
【问题描述】:

我使用 imagick 进行缩略图裁剪,但有时裁剪后的缩略图会丢失图像的顶部(头发、眼睛)。

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

以下是我用于裁剪的 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">';

谢谢..

【问题讨论】:

  • 您遇到了什么错误?什么是预期的输出?什么版本的PHP? imagick 安装了吗?更多细节请...
  • 不,这与错误无关。 imagick 工作正常。上面的脚本只裁剪。我想先调整大小,然后我想裁剪它,所以我错过了第一步..
  • 嗯,先给imageResize打电话,然后……

标签: php resize crop imagick


【解决方案1】:

这项任务并不容易,因为“重要”部分可能并不总是在同一个地方。不过,使用这样的东西

$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)确定裁剪区域的左上角。所以玩这些会给你留下不同的结果。

【讨论】:

    【解决方案2】:

    基于Martin's answer,我制作了一个更通用的函数,可以调整和裁剪Imagick 图像以适应给定的宽度和高度(即行为与CSS background-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);
    }
    

    希望这可以帮助某人。

    【讨论】:

    • imagick::FILTER_LANCZOS 应该是\Imagick::FILTER_LANCZOS
    • 最后一行 $image-&gt;cropImage($new_width, $new_height, ... 应该是 $image-&gt;cropImage($width, $height, ...
    • 对我来说它适用于$image-&gt;cropImage($new_width, $new_height, 0,0);
    猜你喜欢
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2015-02-01
    • 2010-11-03
    • 2011-06-14
    • 2012-12-27
    • 1970-01-01
    相关资源
    最近更新 更多