Resize images without saving to file?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
cycletourist
Posts: 14
Joined: 2007-06-25T16:08:06-07:00

Resize images without saving to file?

Post by cycletourist »

I'm trying to figure out if I can use ImageMagick (from the command line - and actually, using the Php exec command) to create a resized version of an image in memory only, without saving it to a file.

Using the GD library in Php I can create images in memory and serve them within an html img tag. Can I do the same with ImageMagick? Can I create dynamic thumbnails (without using something like MagickWand)?

If so, ... any examples?

Thanks in Advance,
Chuck
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Resize images without saving to file?

Post by Bonzo »

Just a random bit of code I have:

Code: Select all

exec("convert photo7.jpg -fill rgb\(255,0,0\) -opaque rgb\(170,170,170\) JPG:-");
As far as I know this part of the code JPG:- should display the image without saving it although it didn't work for me and I have not realy looked into it.
cycletourist
Posts: 14
Joined: 2007-06-25T16:08:06-07:00

Re: Resize images without saving to file?

Post by cycletourist »

Okay, ... between the two of us, I think I've got it. Although "JPG:-" is right there in the manual, I simply did not understand, .... but that was the key for me (thanks!).

The reason that your example is not working is that the exec command requires an extra input if you want the output of the command returned; i.e., exec($cmd, $output, $retval). Your script should be:

Code: Select all

exec("convert photo7.jpg -fill rgb\(255,0,0\) -opaque rgb\(170,170,170\) JPG:-", $output, $retval);
The image data should now be in $output (note: $output and $retval are optional).

That won't actually work, though, as $output is an array filled with every line of output from the command (minus line feeds, so invalid data). Using shell_exec, the return is a string, but I haven't figured out how to get that string displayed as an image yet (not even when using the Php GD lib functions - imagecreatefromstring and imagejpeg ??).

This, however, does work, but I would rather have the output returned as a string, (using shell_exec, for example), so I could check the retval before sending the Content-type header:

Code: Select all

// $photo is the path to the source file:
$THUMB_SZ  = 125;
$THUMB_PRESZ  = $THUMB_SZ * 2;
$QUALITY = 87;
$cmd = "convert.exe -size $THUMB_PRESZ".'x'."$THUMB_PRESZ \"$photo\"" .
" -resize $THUMB_SZ".'x'."$THUMB_SZ" .
" -strip" .
" -unsharp 0.2x0.6+1.0" .
" -quality $QUALITY JPG:-";

header("Content-type: image/jpeg");
passthru($cmd, $retval);
From the Php manual - "The passthru function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser."

If I figure out how to display the image after using another of the program execution functions so I can verify the output before sending the image header, I'll post back.

Chuck
journeyman

Re: Resize images without saving to file?

Post by journeyman »

sorry to hijack the thread
but can the code be changed to convert an eps file to a jpg file
cheers
kev
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Resize images without saving to file?

Post by Bonzo »

Not quite understanding you journeyman:

Do you want to read an eps and then display it as a jpg without saving it ?

Or just change an eps file to a jpg file and save it ?
journeyman

Re: Resize images without saving to file?

Post by journeyman »

I want to read an eps and then display it as a jpg without saving it ?
cheers
kev
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Resize images without saving to file?

Post by Bonzo »

Untested but may work

Code: Select all

<?php
$cmd = "convert input.eps JPG:-";

header("Content-type: image/jpeg");
passthru($cmd, $retval);
?>
Post Reply