Page 1 of 1

Image Upload

Posted: Thu Jun 03, 2004 9:51 am
by justjon
Hello everybody,

I'm trying to design a website where a user can upload images which are then used at 2 different sizes throughout the site (thumbnail and full size). Does anyone know of the easiest way to do this with PHP? Would it be best to write a function to take care of this, use GD, or something like ImageMagick. Does anyone know what the best option for this sort of thing is?

Is the resizing done when the image is uploaded and then saved as 2 different files (1 big 1 small) or is the original image saved and the resizing done when the page is called to display it. The reason I ask, is that I have seen systems that let an administrator choose what size they want the images to be displayed at. This size can be changed many times without any loss of image quality (which would happen if the image was made very small and then very big) does this mean that the image is resized when the page is called?

Any help would be muuuuuuch appreciated.

Posted: Thu Jun 03, 2004 9:57 am
by Grim...
Try this:

Code: Select all

$pathonserver = "images/";
//where to put the stuff

$folder = "user/photoupload/";
//the folder to put it in

$userfilename = $file_name;

$fileonserver = $pathonserver.$folder.$file_name;

copy($file, $fileonserver);
unlink($file);

$src_name = $pathonserver.$folder.$file_name;
$dest_name = $pathonserver.$folder."tn_".$file_name;

$sizes = getimagesize($src_name);
$src = imagecreatefromjpeg($src_name);

$w = 150; //thumbnail sizes
$h = 114;

$dest = imagecreate($w,$h);

imagecopyresized($dest, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));

imagejpeg($dest, $dest_name, 80);

imagedestroy($src);
imagedestroy($dest);

Posted: Thu Jun 03, 2004 10:24 am
by pickle
Only 2 further things to suggest. Use [php_man]imagecopyresampled[/php_man] rather than [php_man]imagecopyresized[/php_man] and use [php_man]imagecreatetruecolor[/php_man] rather than [php_man]imagecreate[/php_man]. These two alternative functions make a much nicer looking thumbnail, although they only work if you have GD 2.01 or greater.