Home > Back-end >  PHP exec ImageMagick - magick convert not working ubuntu linux
PHP exec ImageMagick - magick convert not working ubuntu linux

Time:04-08

I'm using ubuntu and have installed ImageMagick

identify -version

gives:

Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff webp wmf x xml zlib

I tried:

exec('magick img.jpg ( -clone 0 -fill white -colorize 100 ) ( -clone 0 -color-threshold "gray(251)-gray(254)" ) -compose over -composite -quality 80% result.jpg', $output, $return_var);
echo '<pre>' , var_dump($output) , '</pre>';

also convert and magick convert instead of magick

then according to this answer I opened /etc/ImageMagick-6/policy.xml

and changed <policy domain="coder" rights="none" pattern="PDF" /> to <policy domain="coder" rights="read|write" pattern="PDF" />

but still doesn't work and $output always returns empty array

PS: I don't want to use the PHP extension, already did and it has some flaws

CodePudding user response:

Please do some testing in Imagemagick with PHP to see what your versions are. Please provide any messages that come back for each.

<?php
echo "<pre>";
system("type -a convert");  
echo "</pre>";
?> 

<?php
exec("magick -version",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>

Does this work?


<?php
exec('magick img.jpg \( -clone 0 -fill white -colorize 100 \) \( -clone 0 -color-threshold "gray(251)-gray(254)" \) -compose over -composite -quality 80% result.jpg 2>&1',$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>

What messages do you get?

CodePudding user response:

Convert writes its output to disk (result.jpg). The output argument of exec() if for any output to stdout. If you want the image data in$output specify an output file of -.

To quote the man page:

By default, the image format of 'file' is determined by its magic number. To specify a particular image format, precede the filename with an image format name and a colon (i.e. ps:image) or specify the image type as the filename suffix (i.e. image.ps). Specify 'file' as '-' for standard input or output.

  • Related