1

I want to be able to generate image thumbnails without saving them to the server. So far, I've come up with this code, but I'm not sure what to do with $code.

$code = system("convert galleries/13_0.jpg -resize 400x270 /dev/stdout");

How would I go about plugging $code into the PHP/HTML to get the raw image code to display as a jpg?

3 Answers 3

1

I would advise you not to do this from the system as you have described.

PHP has libraries for doing this sort of thing.

http://www.php.net/manual/en/ref.image.php. http://php.net/manual/en/book.imagick.php.

And there are libraries the wrap these native functions for manipulating images. https://imagine.readthedocs.org/en/latest/

That way all your code is in PHP and you are not relying on the system to do anything (providing PHP has been compiled with the libraries as described. They are standard libraries available in most PHP builds and you can enable them if they are not included).

Edit: I'm advocating that you.

  • Open the source image.
  • Convert the source image
  • Return the source image

with imagine you would do it thus:

$imagine = new Imagine\Gd\Imagine();
$size    = new Imagine\Image\Box(400, 270);

$imagine->open('/path/to/large_image.jpg')
->resize($size)
->show('jpg');
Sign up to request clarification or add additional context in comments.

3 Comments

The PHP libraries like imagick rely on the system. While using libraries is considered good practice, in my experience it is often more efficient and more practical to use the system calls. For what the OP is trying to do it is much more efficient to do it with a system call.
Exactly, Alsadair. I also just realized the project I wanted to know this for will be moving from a Linux to a Windoze box I have no control of after development, so this (unfortunately) wouldn't work. If I used PHP libraries, would I have cross-OS support?
As I said - as long as you have access to the libraries as described. I cross platform develop too - and doing underlying system calls is no more portable than what I am advocating. GD is available in all versions of PHP. No-one has mentioned that this needs to be the most efficient solution. 95% of the time code doesn't need to be optimised as you describe. If you are worrying about optimisation here you are probably doing something wrong (i.e optimise somewhere else)
0

If the $code is the raw data, could you have another file getimage.php which would write the data out but set the header content type to image/JPEG? So your img src would be getimage.php?image=13_0.jpg for example?

Comments

0

I would save it to memory, then read it, then delete it. /dev/shm is a ramdrive on most Linux systems.

$tmp = '/dev/shm/'.uniqid('',true).'.jpg';
system("convert galleries/13_0.jpg -resize 400x270 $tmp");
header("Content-Type: image/jpeg");
header('Content-Length: '.filesize($tmp));
readfile($tmp);
unlink($tmp);

4 Comments

I like the idea of saving it to RAM, but would it be possible for me to do it without sending headers? This specific project is in Wordpress, so I can't send headers within a plugin. If I really have to, I can make a separate PHP file for this. It'd require a database look-up to ensure the image name is valid and would add another page request, but it'd probably be cache-able this way. I'll see if anyone else has any ideas, but this is looking the best so far.
Of course you don't need to send the headers if you don't want to.
The comment in my answer mention cross platform compatibility - and then in your answer talk about /dev/shm... Really? If you just do the conversion with a php library the resized image will already be in memory and you can simply return it to the browser. I've edited my answer.
Right, my solution will only work for Linux. I wrote it before cross-platform was mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.