Save Full Size & Thumbnail of Picture

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

Save Full Size & Thumbnail of Picture

Post by SheDesigns »

I don't know what I'm doing wrong here.. :banghead:

The intention is to save a copy to Images (it does this fine), it also creates the thumbnail fine.. but it won't save the thumbnail. Yes, the directory is set to 777.

The loop needs to stay before each entry can have up to 6 pictures attached to it.

Code: Select all

<?php
header('Content-type: image/jpeg'); 
while(list($key,$value) = each($_FILES['images']['name']))
        {
            if(!empty($value))
            {
                $filename = $value;
                    $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
                    $streetaddress=str_replace(" ","",$streetaddress);
                    $newfilename = $streetaddress."_".$filename;
 
                    $add = "photos/$newfilename";
                        
                    copy($_FILES['images']['tmp_name'][$key], $add);
                    chmod("$add",0777);
                            
                    $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);                   
                    imagejpeg($image_p, null, 100);
                    
                    $adding = "photos/thumbs/$newfilename";
                    copy($_FILES['image']['tmp_name'][$key], $adding);
                    chmod("$adding",0777);
                    
            }
        }
 
        ?>
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Save Full Size & Thumbnail of Picture

Post by mmj »

If I'm correct you should remove lines 31, 32, 33 and change line 29 to

Code: Select all

imagejpeg($image_p, "photos/thumbs/$newfilename", 100);
also the 0777 chmods are overkill, do something like 0644 (which it should default to, if I'm not mistaken).

also you should be using move_uploaded_file (not copy), it moves the file and does some security checks.
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Re: Save Full Size & Thumbnail of Picture

Post by SheDesigns »

Changed it..

Code: Select all

                    $image_p = imagecreatetruecolor($width, $height);
                    $image = imagecreatefromjpeg($add);
                    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);                   
                    imagejpeg($image_p, "photos/thumbs/$newfilename", 100);
                    
                    //$adding = "photos/thumbs/$newfilename";
                    //copy($_FILES['$add']['$image_p'][$key], $adding);
                    //chmod("$adding",0777);
But now it won't even create the JPG, it simply prints the URL instead.
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Re: Save Full Size & Thumbnail of Picture

Post by SheDesigns »

:oops: Now it's making the thumbs correctly... but still printing the URL. Any clue why this is??
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Save Full Size & Thumbnail of Picture

Post by mmj »

http://www.php.net/imagejpeg

If the 2nd parameter is set then it will save it, otherwise it will output it.

If you want to still output it then just add this to the end of the script:

Code: Select all

readfile("photos/thumbs/$newfilename");
Post Reply