SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast cwb's Avatar
    Join Date
    May 2004
    Location
    Michigan
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help With Changing Image Name During Upload

    I found this image upload and resize script online and trying to modify it so that it renames the image. Right now it uploads the image using the original name. I tried the rename function but I can't get it to work:

    rename("pics/$image_orig", "pics/$newname.$image_type");

    Here is the original script:

    PHP Code:
    <?php session_start(); ob_start();

    $img_size='pics/'.$_FILES['image_orig']['name'];
     
    move_uploaded_file($_FILES['image_orig']['tmp_name'],$img_size);
     
    reduce_image($img_size,$_POST['resize_wid'],$img_size);
     
    function 
    reduce_image($file,$size,$file_path)

        
    $image_type=array('gif'=>1,'jpg'=>2,'png'=>3,'bmp'=>6);
        
    $uploadedfile $file;
        
        list(
    $width,$height,$type)=getimagesize($uploadedfile);

    //if($height > 100) { die("image is to tall! $height"); }
    //if($width > 100) { die("image is to wide! $width"); }

        
    if($width>$size)
        {  
        
            switch(
    $type)
            {
                case 
    $image_type['gif']: $src imagecreatefromgif($uploadedfile); break;
                case 
    $image_type['jpg']: $src imagecreatefromjpeg($uploadedfile); break;
                case 
    $image_type['png']: $src imagecreatefrompng($uploadedfile); break;
                
    //case $image_type['bmp']: $src = imagecreatefromwbmp($uploadedfile); break;
            
    }
        
            
    // For our purposes, I have resized the image to be  600 pixels wide, and maintain the original aspect 
            // ratio. This prevents the image from being "stretched"  or "squashed". If you prefer some max width other than
            // 600, simply change the $newwidth variable
            
            
    $newwidth=$size;    
            
            
    $newheight=($height/$width)*$size;
            
    $tmp=imagecreatetruecolor($newwidth,$newheight);
            
            
    // this line actually does the image resizing, copying from the original image into the $tmp image
            
            
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

            
            
    // now write the resized image to disk. I have assumed that you want the
            // resized, uploaded image file to reside in the ./images subdirectory.
            //$file_path = "uploadimages/".date('Y-d-s').$_FILES[$file]['name'];        
            
    switch($type)
            {
                case 
    $image_type['gif']: $ret=imagegif($tmp,$file_path,100); break;
                case 
    $image_type['jpg']: $ret=imagejpeg($tmp,$file_path,100); break;
                case 
    $image_type['png']: $ret=imagepng($tmp,$file_path); break;
                
    //case $image_type['bmp']: imagewbmp($tmp,$file_path); break;
            
    }
            
            
    $_SESSION['succ']=1;
            
    imagedestroy($src);
            
    imagedestroy($tmp);
        }
        else {
    //        $ret=move_uploaded_file($uploadedfile,$file_path);
            
    $_SESSION['succ']=0;
            }
        return 
    $ret
    }
        
    header("Location: upload.php");
    ?>

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,408
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Try changing this line:
    PHP Code:
    $img_size='pics/'.$_FILES['image_orig']['name']; 
    This is the path to the new saved image and it uses the original image name in ['name']

    PHP Code:
    $img_size='pics/'.$newname"; 
    I am not that good with GD but assume with this code you will need to keep the same type e.g originalname.jpg > newname.jpg
    Last edited by Rubble; Jan 6, 2009 at 15:20. Reason: Cleaned up text

  3. #3
    SitePoint Enthusiast cwb's Avatar
    Join Date
    May 2004
    Location
    Michigan
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for pointing me in the right direction. I was able to get it to work.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •