Trim error in PHP Imagick?

I tried to crop the animated gif, and in the output I get an image of the same size, but cropped.

A lot of empty space is filled with canvas.

For example, I had an animated gif 600x100, but asked for cropping 100x100, at the output I get a 600x100 image with a cropped image and empty space.

Does anyone know a solution to this problem?

$gif = new Imagick($s['src']);

foreach($gif as $frame){
  $frame->cropImage($s['params']['w'], $s['params']['h'], $s['params']['x'], $s['params']['y']);            
}   

$gif->writeImages($s['dest_path'] .'/'. $fullname,true);
+3
source share
2 answers

I had the same problem as you, and I found that the solution uses the coalesceimages function.

Here is a working example for cropping and resizing an animated gif in php using Imagick:

<?php
// $width and $height are the "big image" proportions
if($width > $height) {
    $x     = ceil(($width - $height) / 2 );
    $width = $height;
} elseif($height > $width) {
    $y      = ceil(($height - $width) / 2);
    $height = $width;
}

$image = new Imagick(HERE_YOU_PUT_BIG_IMAGE_PATH);
$image = $image->coalesceImages(); // the trick!
foreach ($image as $frame) {
    $frame->cropImage($width, $height, $x, $y); // You crop the big image first
    $frame->setImagePage(0, 0, 0, 0); // Remove canvas
}
$image = $image->coalesceImages(); // We do coalesceimages again because now we need to resize
foreach ($image as $frame) {
    $frame->resizeImage($newWidth, $newHeight,Imagick::FILTER_LANCZOS,1); // $newWidth and $newHeight are the proportions for the new image
}
$image->writeImages(CROPPED_AND_RESIZED_IMAGE_PATH_HERE, true);
?>

. , .

, $frame- > cropImage ($ width, $height, $x, $y); , .

IE $frame- > cropImage ($ s ['params'] ['w'], $s ['params'] ['h'], $s ['params'] ['x'], $ [ 'PARAMS'] [ '']);

, , :

$image = new Imagick(HERE_YOU_PUT_BIG_IMAGE_PATH);
$image = $image->coalesceImages(); // the trick!
foreach ($image as $frame) {
    $frame->cropImage($s['params']['w'], $s['params']['h'], $s['params']['x'], $s['params']['y']);
    $frame->setImagePage(0, 0, 0, 0); // Remove canvas
}

, !

Ps: :)

+6

ImageMagick "" , - . , ( - ...).

PHP cropImage, :

Christian Dehning - 09-Apr-2010 10:57
gif- ( jpg png), . gif, :

$im->setImagePage(0, 0, 0, 0);
+4

Source: https://habr.com/ru/post/1769907/


All Articles