1

I use this command to batch modify images for a site. They need to be this size to suit the theme.

Now I also want to use these images in Facebook Ads, but they have to be resized. The command I use (which works fine) is:

for i in `ls -1 *.jpg`; do convert $i -fuzz 10% -trim +repage -resize 980x1200 -background white -gravity center -extent 980x1200 $i; done

Now I need to make a PHP script that does the same, but also returns the image as response.

I came up with the following:

<?php

/* Create the object and read the image in */
$im = new Imagick("image.jpg");

/* Trim the image with 10% fuzz */
$im->trimImage(10);

/* Repage */
$im->setImagePage(0, 0, 0, 0);

/* Resize */
$im->resizeImage(1200,628,Imagick::FILTER_LANCZOS,0);

/* Add Borders*/
$im->setImageBackgroundColor('White');
$im->setGravity('Centre');
$im->setImageExtent(1200,628);

/* Output the image */
header("Content-Type: image/" . $im->getImageFormat());
echo $im;
?>

Unfortunately it doesn't seem to work. All it does is return a black rectangle (which looks like the right dimensions as used in the script).

3
  • What does print_r(Imagick::getVersion()); tell you? Commented Mar 30, 2017 at 22:22
  • You cannot simply echo $im. You should use echo $im->getimageblob();. Commented Mar 30, 2017 at 22:34
  • Both my echo and yours seem to work for me. Unfortunately it just returns a black rectangle in the correct dimensions. So I assume something is going wrong in the manipulations Commented Mar 31, 2017 at 18:24

2 Answers 2

1

First error I get when running this code:

PHP Warning: Imagick::setgravity() expects parameter 1 to be integer, string given in resize.php on line 18

Try using imagick::GRAVITY_CENTER instead.

Next issue, Imagick::resizeImage() and Imagick::setImageExtent() expect parameters in width, height order.

Finally, try setting a non-zero value like 1 for blur on Imagick::resizeImage() to resolve the black image issue.

I'm not sure how you are trying to get a border, but you may want to look at Imagick::borderImage().

I don't know if that will solve all your problems, but it should get you a lot closer!

<?php

/* Create the object and read the image in */
$im = new Imagick("image.jpg");

/* Trim the image with 10% fuzz */
$im->trimImage(10);

/* Repage */
$im->setImagePage(0, 0, 0, 0);

/* Resize */
$im->resizeImage(628, 1200, Imagick::FILTER_LANCZOS, 1);

/* Add Borders*/
$im->setImageBackgroundColor('White');
$im->setGravity(Imagick::GRAVITY_CENTER);
$im->setImageExtent(628, 1200);

/* Output the image */
header("Content-Type: image/" . $im->getImageFormat());
echo $im;
?>
Sign up to request clarification or add additional context in comments.

10 Comments

setGravity(Imagick::GRAVITY_CENTER) doesn't seem to change anything. It still errors on expecting an int
Imagick::GRAVITY_CENTER is an integer. If it's not, you'd be getting other errors.
And you never did answer the question about ImageMagick version.
ImageMagick 6.8.9-9 Q16 x86_64 2017-03-12
Actually, after restarting apache2, I get no errors. But it's still not working.
|
0

you can run commands in php using " shell_exec(your command) ". Try once this may works for you. Refer http://php.net/manual/en/function.shell-exec.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.