Tweak in Saving Images
Posted: Sat Nov 22, 2008 10:05 am
The biggest image I want to save is 300 pixels wide or high max, and a 75 thumbnail (this already works). I don't want to save the original file, but that's how the original script was written.
I can't figure out what I really need to do to save the thumbnail and the image at the sizes I determine, without saving the original file. When I commented out what I thought I needed to, the code doesn't work at all.
I think the issue is with my getimagesize($images[$key]); what should I refer to it, to get the size from? Since no original image will be saved (I left the code in there for it, just commented out).
I can't figure out what I really need to do to save the thumbnail and the image at the sizes I determine, without saving the original file. When I commented out what I thought I needed to, the code doesn't work at all.
I think the issue is with my getimagesize($images[$key]); what should I refer to it, to get the size from? Since no original image will be saved (I left the code in there for it, just commented out).
Code: Select all
{
$i = $thecount;
while(list($key,$value) = each($_FILES['images']['name']))
{
if(!empty($value))
{
$filename = $value;
// Create filename based on day
$when = date("mdyhi");
$newfilename = $when."_".$i.".jpg";
//$add = "photos/$newfilename";
//copy($_FILES['images']['tmp_name'][$key], $add);
//chmod("$add",0777);
$width = 300;
$height = 300;
list($width_orig, $height_orig) = getimagesize($images[$key]);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($images);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$newfilename = "r_".$newfilename;
$add = "photos/$newfilename";
imagejpeg($image_p, $add, 100);
// This code works fine....just wanted to include entire script
$width = 75;
$height = 75;
list($width_orig, $height_orig) = getimagesize($add);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($add);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$newfilename = "s_".$newfilename;
$thumb = "photos/thumbs/$newfilename";
imagejpeg($image_p, $thumb, 100);
// Add Images to the database
$query = "insert into propertyimages values ('', '$add', '$thumb', '$i', '', '$propid');";
$result = mysql_query($query);
$i++;
}}
}