Page 1 of 1

Resize images without saving to file?

Posted: 2007-07-17T16:15:57-07:00
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

Re: Resize images without saving to file?

Posted: 2007-07-18T12:23:58-07:00
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.

Re: Resize images without saving to file?

Posted: 2007-07-18T21:12:38-07:00
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

Re: Resize images without saving to file?

Posted: 2007-11-15T07:12:56-07:00
by journeyman
sorry to hijack the thread
but can the code be changed to convert an eps file to a jpg file
cheers
kev

Re: Resize images without saving to file?

Posted: 2007-11-15T08:40:52-07:00
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 ?

Re: Resize images without saving to file?

Posted: 2007-11-16T02:56:32-07:00
by journeyman
I want to read an eps and then display it as a jpg without saving it ?
cheers
kev

Re: Resize images without saving to file?

Posted: 2007-11-16T06:14:13-07:00
by Bonzo
Untested but may work

Code: Select all

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

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