Forum Moderators: coopster

Message Too Old, No Replies

Resize Image During Upload

         

matthewamzn

8:04 pm on Nov 12, 2006 (gmt 0)

10+ Year Member



I have a script where people upload images. I want to save some disk space by resizing all the images during upload (600 pixels wide). How can I resize them? I think this is the best part of the script to do it:

($ext1 == "jpeg" ¦ $ext1 == "jpg") {
$thumb = imagecreatetruecolor(60, 60);
$new = imagecreatefromjpeg("./u/$user_info[u_id]/$folder_info[f_id]/".$image_info[i_id].".".$image_info[ext]);
for($i=0; $i<256; $i++) {
imagecolorallocate($thumb, $i, $i, $i);
}
$size = getimagesize("./u/$user_info[u_id]/$folder_info[f_id]/".$image_info[i_id].".".$image_info[ext]);
imagecopyresampled($thumb, $new, 0, 0, 0, 0, 60, 60, $size[0], $size[1]);
imagejpeg($thumb, "./u/$user_info[u_id]/$folder_info[f_id]/thumbs/".$image_info[i_id].".".$image_info[ext]);
ImageDestroy($new);
ImageDestroy($thumb);
}

eelixduppy

3:31 am on Nov 13, 2006 (gmt 0)



Here's a function that I use to resize images for a particular project that I have. It works well. Just pick it apart and place it into your code ;)

function resize($filename)
{
// Set a maximum height and width
$width = 200;
$height = 200;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename.".jpg");

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename.".jpg");
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
if(imagejpeg($image_p, $filename."-tn.jpg", 100)) {
return 1;
} else {
return 0;
}
}

Good luck!

Psychopsia

4:43 am on Nov 13, 2006 (gmt 0)

10+ Year Member



Use imagedestroy [php.net] after create the image. :)

eelixduppy

4:56 am on Nov 13, 2006 (gmt 0)



Nice ;)