2

This question has already been asked (see link below) but none of the answers work. So I have this ImageMagick script that I am using to tint PNGs and it works great but the problem is that it actually generates files on the server. What I want instead is exactly what GD does where it does the image manipulation and then displays it without actually saving an image.

Here is my ImageMagick code that I use to tint the image. This code does the converting and generates an extra file on the server which is the final image.

<?php

$source = "src.png";
$final = "FINAL.png";
$color = "#00FF00";

exec("convert $source  -threshold 100% +level-colors '$color',   $final");

?>

Here is a GD example code which does an image manipulation and displays the final image directly without saving extra images to the server:

<?php
header('Content-Type: image/png');

$source = "src.png";

$im = imagecreatefrompng($source);
imagefilter($im, IMG_FILTER_GRAYSCALE);
imagepng($im);
imagedestroy($im);

?>

So essentially I want the image manipulation that is done in the first example, but without saving extra images and displaying the output in the browser.

Links searched:

None of the solutions worked: Generate images with ImageMagick without saving to file but still display them on website

How can I convert my ImageMagick code to iMagick? PHP-Imagemagick image display

Community
  • 1
  • 1
Marin Petkov
  • 2,128
  • 4
  • 17
  • 23
  • You could try `-` as the output filename. Many command line apps use that as a signal to simply print their output, rather than saving to file. But remember... cli apps don't know they're being invoked with a web-based script. Outputting the raw binary contents of an image to a console does not magically make that image visible. – Marc B Oct 20 '12 at 16:14
  • So the `-` did exactly what you said, however where is the image being stored so that I can display it? – Marin Petkov Oct 20 '12 at 23:30
  • it'll be dumped to imagick's stdout, which would be returned to php. so switch to [passthru()](http://php.net/manual/en/function.passthru.php) instead of exec() – Marc B Oct 21 '12 at 15:07
  • oh wow that did the trick. Thank you so much! Are you going to post the answer for it so other people know or should I? – Marin Petkov Oct 21 '12 at 16:22
  • command line stuff won't know what to do with a url. PHP provides convenience handlers that you can pass in a url and have it treated like a local file, but that's only for php's own internals. command line bits are NOT part of php, you're just running them from within php, and therefore don't have access to those conveniences. download the file first, THEN convert it. – Marc B Oct 21 '12 at 16:41
  • Thanks for everything, it worked exactly how you explained. – Marin Petkov Oct 21 '12 at 16:59

1 Answers1

1

A direct example using your code for others to learn from. I use this same method on my shared Linux server on Godaddy.

<?php
    $source = "src.png";
    $color = "#00FF00";

    $cmd = "convert $source -threshold 100% +level-colors '$color',".
    " -unsharp 0.2x0.6+1.0 -quality 50 JPG:-";

    header("Content-type: image/jpeg");
    passthru($cmd, $retval); 
    exit();
?>
  • Note: - Are you sure you are using "-threshold 100% +level-colors '$color'," correctly? Threshold 100% will push an image to black. Which then +level-colors '#00FF00', will just make a solid green image. I am assuming you have simplified the code for this demonstration.

  • Note: - "+level-colors '$color'", does not work on Godaddy's servers. It works fine on my home server though. Possibly an outdated ImageMagick version installed on Godaddy's server.

GrokDD
  • 70
  • 4