Jump to content

[SOLVED] Renaming a file


graham23s

Recommended Posts

Hi Guys,

 

in 1 of my functions here:

 

     ############################################# 
     # Thumbnail code                            
     #############################################   
     function make_smaller_image($filename,$uploaddirectory,$var_loggedinuser) { 
     
        ## Using GD
     
        ## Find out the files extension 
        $extension = explode(".", $filename); 
        $extension = $extension[count($extension)-1]; 
        
        ## Make all filenames lowercase
        $extension = strtolower($extension);
        
        
          ## Generate image accrodingly  
          if($extension == "jpg" || $extension == "jpeg" || $extension == "pjpeg")     
           
          $image = imagecreatefromjpeg($uploaddirectory); 
               
          elseif($extension == "png") 
           
          $image = imagecreatefrompng($uploaddirectory); 
               
          elseif($extension == "gif") 
           
          $image = imagecreatefromgif($uploaddirectory); 
               
      ## get it's height and width 
	  $imagex = imagesx($image);
	  $imagey = imagesy($image);

		if($imagey != 0) {
			/* 
			* lets calculate the aspect ratio and the height
			* and width for the scaled image.
			*/

			$ratio = $imagex / $imagey;
			if($ratio > 1) 
			{

				$new_imgthumbx = 150;
				$new_imgthumby = 150 / $ratio;
                    
                    } else {

				$new_imgthumbx = (float) 150 * $ratio;
				$new_imgthumby = 150;

			}


			$destinationimage = imagecreatetruecolor($new_imgthumbx,$new_imgthumby);

			## create the scaled instance 
			imagecopyresampled($destinationimage,$image,0,0,0,0,$new_imgthumbx,$new_imgthumby,$imagex,$imagey);

			## prefix
			$prefix = "thumb";

			## rename the thumbnail 
                $newimagename = $var_loggedinuser. "-$prefix-" .time(); 

			## make the file depending on arguments
			if($extension == "jpg" || $extension == "jpeg" || $extension == "pjpeg") {				
		      $createdthumb = imagejpeg($destinationimage,"thumbs/$newimagename.$extension");				
			}

			if($extension == 'gif') {				
		      $createdthumb = imagegif($destinationimage,"thumbs/$newimagename.$extension");				
			}

			if($extension == 'png') {				
		      $createdthumb = imagepng($destinationimage,"thumbs/$newimagename.$extension");				
			}

			## return the image
			return("$newimagename.$extension");

          } // end main if
     }

 

at the end i have to put $newimagename.$extension

 

the first part is the renaming then i add the approriate extension, is there a way to rename it withough having to add the extension myself at the return?

 

i name them like: $newimagename = $var_loggedinuser. "-$prefix-" .time();

 

graham23s-thumb-112233545.jpg

 

thanks for any help

 

Graham

Link to comment
Share on other sites

<?php
############################################# 
     # Thumbnail code                            
     #############################################   
     function make_smaller_image($filename,$uploaddirectory,$var_loggedinuser) { 
     
        ## Using GD
     
        ## Find out the files extension 
        $extension = explode(".", $filename); 
        $extension = $extension[count($extension)-1]; 
        
        ## Make all filenames lowercase
        $extension = strtolower($extension);
        
        
          ## Generate image accrodingly  
          if($extension == "jpg" || $extension == "jpeg" || $extension == "pjpeg")     
           {
          $image = imagecreatefromjpeg($uploaddirectory); 
            }   
          elseif($extension == "png") 
           {
          $image = imagecreatefrompng($uploaddirectory); 
            }   
          elseif($extension == "gif") 
           {
          $image = imagecreatefromgif($uploaddirectory); 
            }   
      ## get it's height and width 
	  $imagex = imagesx($image);
	  $imagey = imagesy($image);

		if($imagey != 0) {
			/* 
			* lets calculate the aspect ratio and the height
			* and width for the scaled image.
			*/

			$ratio = $imagex / $imagey;
			if($ratio > 1) 
			{

				$new_imgthumbx = 150;
				$new_imgthumby = 150 / $ratio;
                    
                    } else {

				$new_imgthumbx = (float) 150 * $ratio;
				$new_imgthumby = 150;

			}


			$destinationimage = imagecreatetruecolor($new_imgthumbx,$new_imgthumby);

			## create the scaled instance 
			imagecopyresampled($destinationimage,$image,0,0,0,0,$new_imgthumbx,$new_imgthumby,$imagex,$imagey);

			## prefix
			$prefix = "thumb";

			## rename the thumbnail 
                $newimagename = $var_loggedinuser. "-$prefix-" .time(); 

			## make the file depending on arguments
			if($extension == "jpg" || $extension == "jpeg" || $extension == "pjpeg") {				
		      $createdthumb = imagejpeg($destinationimage,"thumbs/$newimagename.$extension");				
			}

			if($extension == 'gif') {				
		      $createdthumb = imagegif($destinationimage,"thumbs/$newimagename.$extension");				
			}

			if($extension == 'png') {				
		      $createdthumb = imagepng($destinationimage,"thumbs/$newimagename.$extension");				
			}

			## return the image
			return("$newimagename.$extension");

          } // end main if
     }
?>

 

Also, what is that you want to do with the extension? If the code works, then just use it, else tell me the problem with renaming the file yourself.

Link to comment
Share on other sites

Hi Mate,

 

its this bit really

 

return("$newimagename.$extension");

 

once the file has been named i have to concatenate it with the extension, was wondering if there was a way it would just come back as 1 file rather than me joining:

 

$newimagename.$extension");[/

 

thanks mate

 

Graham

Link to comment
Share on other sites

Hi Mate,

 

its this bit really

 

return("$newimagename.$extension");

 

once the file has been named i have to concatenate it with the extension, was wondering if there was a way it would just come back as 1 file rather than me joining:

 

$newimagename.$extension");[/

 

thanks mate

 

Graham

 

Whats wrong with joining them? Its how you put them back as one anyways.

 

("$newimagename"."$extension");[/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.