Solved

Imagemagick directly to browser without saving image.

Posted on 2004-08-23
6
421 Views
Last Modified: 2013-12-12
I'd like to use imagemagick in php to generate an image - but I dont want to have to save it to the server first.

At the moment I'm using exec(); and saving the image - then displaying it.

I think you can change the mime type and stream the image directly to the browser.

Thanks

Dan
0
Comment
Question by:Nottingham
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11870089
This article expalins how to do what you need...

http://www.devarticles.com/c/a/PHP/PHP-Frontend-to-ImageMagick


Alan
0
 

Author Comment

by:Nottingham
ID: 11870225
I've read and re-read this article - in fact I based some of my code on it.

The thing is - it always makes a file:

See:

"The $cache variable should now point to the file containing the requested image. It was already cached or it was generated by convert. If the file exists, we can retrieve some information about that image to put in the http headers."

0
 
LVL 48

Expert Comment

by:hernst42
ID: 11871267
you can try this:
<?php
header('Content-Type: image/jpeg');
$fp = popen('/path/to/convert $options $srcfile -', 'r');
fpassthru($fp);

With the - as destination-filename you can make convert to print the output to stdout instead of writeing the content to a file. With fpassthru you redirect the output of the process to the Browser without stroring that value in a PHP-variable
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Nottingham
ID: 11878998
I tried:

$options="-resize 300";
$file="1.jpg";
header('Content-Type: image/jpeg');
$fp = popen('convert $options $file -', 'r');
fpassthru($fp);

And I received:

Version: ImageMagick 5.5.6 04/01/03 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 2003 ImageMagick Studio LLC

Usage: convert [options ...] file [ [options ...] file ...] [options ...] file

Where options include:
  -adjoin              join images into a single multi-image file
  -affine matrix       affine transform matrix
  -antialias           remove pixel-aliasing
  -append              append an image sequence
  -authenticate value  decrypt image with this password
  -average             average an image sequence
  -background color    background color
  -blue-primary point  chomaticity blue primary point
  -blur geometry       blur the image
  -border geometry     surround image with a border of color
  -bordercolor color   border color.........................................................

Note that this works fine:

exec('convert -resize 300 1.jpg 2.jpg');
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 250 total points
ID: 11879212
try $fp = popen("convert $options $file jpeg:-", 'r');

with the ' the variables will not be evaluated by php and thus you have to use " for the popen
0
 

Author Comment

by:Nottingham
ID: 11879234
Oh my!

Thank you so much!

It works perfectly.

That will do me for now - although I'll probably be back with a few more related questions

Thanks

Dan
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question