Solved

Imagemagick directly to browser without saving image.

Posted on 2004-08-23
6
411 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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now