Solved

Imagemagick directly to browser without saving image.

Posted on 2004-08-23
6
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

718 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