Page 1 of 1

Save Full Size & Thumbnail of Picture

Posted: Fri Nov 21, 2008 12:39 pm
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);
                    
            }
        }
 
        ?>

Re: Save Full Size & Thumbnail of Picture

Posted: Fri Nov 21, 2008 12:49 pm
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.

Re: Save Full Size & Thumbnail of Picture

Posted: Fri Nov 21, 2008 1:09 pm
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.

Re: Save Full Size & Thumbnail of Picture

Posted: Fri Nov 21, 2008 1:39 pm
by SheDesigns
:oops: Now it's making the thumbs correctly... but still printing the URL. Any clue why this is??

Re: Save Full Size & Thumbnail of Picture

Posted: Fri Nov 21, 2008 2:29 pm
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");