Generate images using ImageMagick without saving to a file, but still display them on a website

This is my ImageMagick code that works fine on my web server, creating a new image with the file name "colored_font.png" in the default directory ...

<?php $cmd = " -background none -pointsize 60 -font Times-Roman -fill red ". " -strokewidth 1 -stroke black label:\"google\" "; exec("convert $cmd coloured_font.png"); ?> 

But now I run ImageMagick on Windows, which will not create image files in the default folder, but ImageMagick applications work fine (I tested by creating a thumbnail). So now my idea is to display "colored_font.png" on the browser screen without saving it anywhere ... so please someone can help me create and display the image on the screen without saving it.

+4
source share
3 answers

You can use a special inline: image format inline: (which ImageMagick also supports for reading files). This format is base64 encoded binary data.

At the command line (Linux):

 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 a few special ImageMagick tricks and a sleeve sleeve:

  • using the png: format hint to indicate that the output should be in PNG format;
  • using stdout as its output channel (instead of a file) by specifying fd:1 ;
  • line output directly to stdin of base64 binary for PNG encoding;
  • saving base64 encoded data in the my_base64_png environment my_base64_png .

Now, in your HTML embedding of base64 image data (which should work in all modern ones, but does not work in older browsers ):

  <IMG SRC="data:image/png;base64, echo "$(my_base64_png)" ALT="google" WIDTH=214 HEIGHT=57 VSPACE=5 HSPACE=5 BORDER=0 /> 

or

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

You can easily translate this procedure into PHP .; -)

As I said, ImageMagick can read this inline: format (not used here for reading). But for completeness, 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 does not require image/png; parts of the command (she even ignores it), because she can identify the format in any case through her own built-in magic database, but this will not hurt either ...

Let me also point out that ImageMagick's command line length is limited to 5000 characters, so reading inline: data will not work for large images. (Currently I don’t know what is the restriction for embedding image data in HTML ...)

+6
source

Just replace the coloured_font.png with the coloured_font.php in the html file and create

coloured_font.php with this content:

 <?php header("Content-Type: image/png"); echo `convert -background none -pointsize 60 -font Times-Roman -fill red ". " -strokewidth 1 -stroke black label:\"google\" png:-`; 

and here it is! Try opening coloured_font.php in your browser!

+1
source

You will have to create a temporary file

Steps

  • Create a temporary file temp_image.jpg by running the imagemagick command in the shell
  • Send this as an http response

     $file = 'pathto/temp_image.jpg'; $type = 'image/jpeg'; // set appropriate type header('Content-Type:'.$type); // set content type header('Content-Length: ' . filesize($file)); readfile($file); 
  • Delete temp_image.jpg

Suppose this is in getImage.php , you can specify it as the source of the image tag

 <img src="pathto/getImage.php" /> 
0
source

All Articles