Tweak in Saving Images

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
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Tweak in Saving Images

Post by SheDesigns »

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).

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++;
            }}
 
 
}
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Re: Tweak in Saving Images

Post by SheDesigns »

Also, when I tried to call it from the temporary file... the picture saves just as a black block.

Code: Select all

                    $width = 300;
                    $height = 300;
                    list($width_orig, $height_orig) = getimagesize($_FILES['images']['tmp_name'][$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($_FILES['images']['tmp_name'][$key]);
:cry:
Post Reply