Thumbnail file creation - a better way?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Thumbnail file creation - a better way?

Post by Jenk »

I've currently got a simple system of:

Code: Select all

<?php

$original = '/path/to/original.jpg';
$thumb = imagecreatetruecolor(100,100);
$size = getimagesize($original);
imagecopyresampled($thumb, $original, 0, 0, 0, 0, 100, 100, $size[0], $size[1]);

ob_start();
imagejpeg($thumb);
$content = ob_get_contents();
ob_end_clean();

file_put_contents('/path/to/thumbnail.jpg', $content);

?>
Is there a way to create a thumbnail like the above without the use of the output buffer?

Is there also a way to create a thumbnail before the original image is on disk? I.e. I have the file via $_FILES, but not on disk yet.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

imagejpeg() optionally takes a second argument of a filename that you can use to save the image.

imagecreatefromjpeg($_FILES['myUpload']['tmp_file']) works fine.

Pimp-my-website: http://www.phpgd.com/scripts.php?section=2 .. several ways to make thumbnails.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Cheers. :)
Post Reply