简体   繁体   English

如何在php中使用imagick? (调整大小和裁剪)

[英]how do i use imagick in php? (resize & crop)

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.. 谢谢..

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. cropImage参数(0和0)确定裁剪区域的左上角。 So playing with these gives you differnt results of what stays in the image. 因此,使用这些功能可以为您保留图像中保留的内容的不同结果。

Based on Martin's answer I made a more general function that resizes and crops Imagick image to fit given width and height (ie behaves exactly as CSS background-size: cover declaration): 根据Martin的回答,我做了一个更通用的功能,可以调整Imagick图像的大小并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);
}

Hope this may help somebody. 希望这可以对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM