?
Solved

Imagemagick directly to browser without saving image.

Posted on 2004-08-23
6
Medium Priority
?
449 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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 1000 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

Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

764 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