Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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

like image 210
newworroo Avatar asked Aug 19 '13 11:08

newworroo


People also ask

How resize ImageMagick in PHP?

The Imagick::resizeImage() function is an inbuilt function in PHP which is used to scale an image to the desired dimensions. Parameters: This function accepts six parameters as mentioned above and described below: $columns: It specifies the width of the image. $rows: It specifies the height of the image.

How do I resize an image in ImageMagick?

To resize an image to specific dimensions, use the convert command with an input file, the -resize parameter, your preferred dimensions, and an output filename: convert original. png -resize 100x100 new.

What does PHP imagick do?

Imagick is a PHP extension to create and modify images using the ImageMagick library. There is also a version of Imagick available for HHVM. Although the two extensions are mostly compatible in their API, and they both call the ImageMagick library, the two extensions are completely separate code-bases.

Is imagick and ImageMagick same?

ImageMagick is a PHP utility, a command line tool for image manipulation. For further details, see this. Imagick is an API or a class that performs the function same as ImageMagick. It provides numerous functions for image manipulation in PHP.


2 Answers

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.

like image 113
Martin Müller Avatar answered Oct 10 '22 10:10

Martin Müller


Based on Martin's answer I 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):

/**
 * 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.

like image 34
Stanislav Agapov Avatar answered Oct 10 '22 08:10

Stanislav Agapov