简体   繁体   中英

PHP: Displaying ImageMagick image without saving to server like GD

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM