Image Upload

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
justjon
Forum Newbie
Posts: 5
Joined: Tue Mar 30, 2004 10:36 am

Image Upload

Post 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.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply