Rename & Resize during upload???

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
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Rename & Resize during upload???

Post by jmansa »

I'm going crazy... I'm trying to make an upload script which both resizes and renames the file... I can resize the fiel with no problem like this:

Code: Select all

if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
    
    // file needs to be jpg,gif,bmp,x-png and 4 MB max
    if (($_FILES["uploaded"]["type"] == "image/jpeg" || $_FILES["uploaded"]["type"] == "image/pjpeg" || $_FILES["uploaded"]["type"] == "image/gif" || $_FILES["uploaded"]["type"] == "image/x-png") && ($_FILES["uploaded"]["size"] < 4000000))
    {
 
        // some settings
        $max_upload_width = 480;
        $max_upload_height = 270;
          
        // if uploaded image was JPG/JPEG
        if($_FILES["uploaded"]["type"] == "image/jpeg" || $_FILES["uploaded"]["type"] == "image/pjpeg"){    
            $image_source = imagecreatefromjpeg($_FILES["uploaded"]["tmp_name"]);
        }       
        // if uploaded image was GIF
        if($_FILES["uploaded"]["type"] == "image/gif"){ 
            $image_source = imagecreatefromgif($_FILES["uploaded"]["tmp_name"]);
        }   
        // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)   
        // if uploaded image was BMP
        if($_FILES["uploaded"]["type"] == "image/bmp"){ 
            $image_source = imagecreatefromwbmp($_FILES["uploaded"]["tmp_name"]);
        }           
        // if uploaded image was PNG
        if($_FILES["uploaded"]["type"] == "image/x-png"){
            $image_source = imagecreatefrompng($_FILES["uploaded"]["tmp_name"]);
        }
        
        $remote_file = "../../../uploads/sponsors/".$_FILES["uploaded"]["name"];
        imagejpeg($image_source,$remote_file,100);
        chmod($remote_file,0644);
 
        // get width and height of original image
        list($image_width, $image_height) = getimagesize($remote_file);
    
        if($image_width>$max_upload_width || $image_height >$max_upload_height){
            //$proportions = $image_width/$image_height;
            
            if($image_width>$image_height){
                $new_width = $max_upload_width;
                //$new_height = round($max_upload_width/$proportions);
                $new_height = $max_upload_height;
            }       
            else{
                $new_height = $max_upload_height;
                //$new_width = round($max_upload_height*$proportions);
                $new_width = $max_upload_width;
            }       
            
            
            $new_image = imagecreatetruecolor($new_width , $new_height);
            $image_source = imagecreatefromjpeg($remote_file);
            
            imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
            imagejpeg($new_image,$remote_file,100);
            
            imagedestroy($new_image);
        }
        
        imagedestroy($image_source);
        
        $image = $_FILES["uploaded"]["name"];
And I can remane the file like this:

Code: Select all

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 
 
$ext = findexts ($_FILES['uploaded']['name']) ; 
 
$ran = time();
 
$ran2 = $ran.".";
 
$ext = strtolower($ext);
 
$target = "../../../uploads/sponsors/";
$target = $target . $ran2.$ext;
 
$final_image = $ran2.$ext;
But I can't figure out how to combine this... I have been trying for days now, and really needs some help...
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Rename & Resize during upload???

Post by Kieran Huggins »

is there an error?
Post Reply