Php-Imagemagick Image Display

PHP-Imagemagick image display

you can try to display the image by this way:

// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();

echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />";

PHP: Displaying ImageMagick image without saving to server like GD

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.

How to show an image with imagemagick?

You would have the code above on a seperate page called something like image.php ( note you will need to remove the header part ).

Then where you want it on the main page use:

<img src="image.php">

Output raw image from Imagick image in PHP

You just need to echo your imagick object:

$img = new Imagick($file);
header('Content-Type: image/'.$img->getImageFormat());
echo $img;

Generate images with ImageMagick without saving to file but still display them on website

You could use the special inline: format of images (which ImageMagick supports for reading files too). This format is a base64 encoding of binary data.

On the (Linux) commandline:

my_base64_png="$(
convert \
-background none \
-pointsize 60 \
-font Times-Roman \
-fill red \
-strokewidth 1 \
-stroke black \
label:\"google\" \
png:fd:1 \
| \
base64 -i - -o -)"

This command uses several special tricks ImageMagick and the shell have up in their sleeves:

  • using the format hint png: to tell it that the output should be in PNG format;
  • using stdout as its output channel (instead of a file) by specifiying fd:1;
  • piping the output directly into stdin of the base64 binary to encode the PNG;
  • storing the base64 encoded data in the environment variable my_base64_png.

Now in your HTML inlining the base64 image data (which should work in all modern, but doesn't work in older browsers):

 <IMG SRC="data:image/png;base64,
echo "$(my_base64_png)"
ALT="google" WIDTH=214 HEIGHT=57 VSPACE=5 HSPACE=5 B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0bGFiZWwAImdvb2dsZSJdcbX4AAAAAElFTkSuQmCC"
ALT="google" WIDTH=214 HEIGHT=57 VSPACE=5 HSPACE=5 BORDER=0 />

It shouldn't be difficult for you to translate this procedure into PHP. ;-)

As I said, ImageMagick can read this inline: format (not used here for reading). But for completeness' sake let me show you how:

  convert                                                                   \
'inline:image/png;data:,
iVBORw0KGgoAAAANSUhEUgAAAM4AAABJAQMAAABPZIvnAAAABGdBTUEAALGPC/xh
BQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAA
OpgAABdwnLpRPAAAAAZQTFRFAAAA/wAAG/+NIgAAAAF0Uk5TAEDm2GYAAAABYktH
RACIBR1IAAAACXBIWXMAAABIAAAASABGyWs+AAAB6ElEQVQ4y+3UQY7bIBQG4IeQ
yqYaLhANV+iyi9FwpS69iGyiLuZYpepF6A1YskC8/uCA7SgZtVI3lcoiivkIxu/9
MdH/8U+N6el2pk0oFyibWyr1Q3+PlO2NqJV+/BnRPMjcJ9zrfJ/U+zQ9oAvo+QGF
d+npPqFQn++TXElkrEpEJhAtlTBR6dNHUuzIMhFnEhxAmJDkKxlmt7ATXDDJYcaE
r4Txqtkl42VYSH+t9KrD9b5nxZeog/LWGVHprGInGWVQUTvjDWXca5KdsowqyGSc
DrZRlGlQUl4kQwpUjiSS9gI9VdECZhHFQ2I+UE2CHJQfkNxTNKCl0RkURqlLowJK
1h1p3sjc0CJD39D4BIqD7JvvpH/GAxl2/YSq9mtHSHknga7OKNOHKyEdaFC2Dh1w
9VSJemBeGuHgMuh24EynK03YM1Lr83OjUle38aVSfTblT424rl4LhdglsUag5RB5
uBJSJBIiELSzaAeIN0pUlEeZEMeClC4cBuH6mxOlgPjC3uLproUCWfy58WPN/MZR
86ghc888yNdD0Tj8eAucasl2I5LqX19I7EmEjaYjSb9R/G1SYfQA7ZBuT5H6WwDt
UAfK1BOJmh/eZnKLeKvZ/vA8qonCpj1h6djfbqvW620Tva36++MXUkNDlFREMVkA
AAAldEVYdGRhdGU6Y3JlYXRlADIwMTItMDgtMjJUMDg6Mzc6NDUrMDI6MDBTUnmt
AAAAJXRFWHRkYXRlOm1vZGlmeQAyMDEyLTA4LTIyVDA4OjM3OjQ1KzAyOjAwIg/B
EQAAAA50RVh0bGFiZWwAImdvb2dsZSJdcbX4AAAAAElFTkSuQmCC' \
my_decoded.png

ImageMagick doesn't require the image/png; part of the command (it even ignores it), because it can identify the format anyway through it's own builtin magic database -- but it doesn't hurt either...

Let me also point out that ImageMagick's commandline length is limited to 5000 characters, so reading inline: data will not work for larger pictures. (I don't know at the moment what the limitation for inlining image data in HTML is...)

Imagick not displaying to page with content headers set

Had an echo in the code which was causing this error. Deleting the extra echo was the fix.



Related Topics



Leave a reply



Submit