Solved

Imagemagick directly to browser without saving image.

Posted on 2004-08-23
6
429 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

821 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