phpshellgraphicsmagick

Convert image with mogrify and output it to variable


I am trying to convert a bmp to jpg with mogrify.The jpg should then be store in a variable.

Here is my attempt thus far

$jpg_content = shell_exec("gm mogrify -format jpg ". escapeshellarg($image) . " && cat " . escapeshellarg($image)."2>&1");

However $jpg_content is null

Edit

Suppose that $image is /var/www/website.com/public_html/www/imgscript/tmp/0048699218

shell_exec("gm mogrify -format jpg ". escapeshellarg($image));
$jpg_content = file_get_contents($image.'.jpg');

After mogify is done then it should have created the jpg but its not the case here.The tmp directory is writable and thus its not a permission issue.


Solution

  • You can avoid writing to disk at all by telling GraphicsMagick to write a JPEG on its stdout and picking that up into a PHP variable like this:

    $content=shell_exec("gm convert image.bmp jpg:-");