How to I rename the image?

Hello,

I would like to rename the image using date("YmdHis")

Can you please show me how to do this.

Thank you,
Matt


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);
			}		
			else{
				$new_height = $max_upload_height;
				$new_width = round($max_upload_height*$proportions);
			}		
			
			
			$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);
		}

Open in new window

LVL 1
movieprodwAsked:
Who is Participating?
 
movieprodwConnect With a Mentor Author Commented:
got it!
$remote_file = "uploads/".$newname;
imagejpeg($image_source,$remote_file,100);

Open in new window

0
 
aldanchCommented:
You just want to rename the output file?

// bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )
 
$newname = date("YmdHis");
imagejpeg($new_image,"/path/to/$newname",100);

Open in new window

0
 
movieprodwAuthor Commented:
Correct I just want to stick a line in to make it so that it changes the saved filename to the datetime

I tried to plug that in but got an error
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
aldanchCommented:
Make sure the directory you are saving the image has the correct permissions.

What error did you get?
0
 
movieprodwAuthor Commented:
I tried this and it does not rename or resize the image anymore, it still uploads it with the original name though
			$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,80); old
			
			$newname = date("YmdHis"); //new
			imagejpeg($new_image,"/uploads/$newname",80); //new
			
			imagedestroy($new_image);
		}

Open in new window

0
 
aldanchCommented:
chmod 777 /uploads

-or-

make sure Apache has write access to that folder... e.g. "chmod www-data:www-data /uploads"

I assume this is a Linux box?

Please post the errors you are getting.
0
 
movieprodwAuthor Commented:
It is uploading into that folder, but its not renaming it or resizing it it is simply taking the original image and uploading it into the uploads folder
0
 
aldanchCommented:
The image gets uploaded via a form? That form get POSTed to your PHP script. Your PHP script saves the original image to the /uploads directory. You would add the resize/rename after the script saves the file.


$newname = date("YmdHis") .".jpg";
move_uploaded_file($_FILES['image_file']['tmp_name'], "/uploads/$newname");

Open in new window

0
 
movieprodwAuthor Commented:
Yes it is getting uploaded via form to itself

I have attached the top of the page fyi


<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
<?php
 
$propid = $_SESSION['propid'];
 
 
 
// upload the file
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["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
	{
		
  
		// some settings
		$max_upload_width = 2592;
		$max_upload_height = 1944;
		  
		// if user chosed properly then scale down the image according to user preferances
		if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
			$max_upload_width = $_REQUEST['max_width_box'];
		}    
		if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
			$max_upload_height = $_REQUEST['max_height_box'];
		}	
 
		
		// if uploaded image was JPG/JPEG
		if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){	
			$image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
		}		
		// if uploaded image was GIF
		if($_FILES["image_upload_box"]["type"] == "image/gif"){	
			$image_source = imagecreatefromgif($_FILES["image_upload_box"]["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["image_upload_box"]["type"] == "image/bmp"){	
			$image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
		}			
		// if uploaded image was PNG
		if($_FILES["image_upload_box"]["type"] == "image/x-png"){
			$image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
		}
		
 
		$remote_file = "uploads/".$_FILES["image_upload_box"]["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);
			}		
			else{
				$new_height = $max_upload_height;
				$new_width = round($max_upload_height*$proportions);
			}		
			
			
			$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,80);
			
			imagedestroy($new_image);
		}
		
		imagedestroy($image_source);
		
						$img_name = $file;
                        
                        $query = "SELECT `propid` FROM `images` WHERE `propid` = $propid";
						$result = mysql_query($query);
						if (mysql_num_rows($result) > 0) {
 
						$sql = "INSERT into images (propid,userid,img_name,mainImage) VALUES ('$propid','$userid','$img_name','N')";
                        $result = mysql_db_query($dbname, $sql) or die("Failed Query of " . $sql);  //do the query
                        
  							 } else {
     
						$sql = "INSERT into images (propid,userid,img_name,mainImage) VALUES ('$propid','$userid','$img_name','Y')";
                        $result = mysql_db_query($dbname, $sql) or die("Failed Query of " . $sql);  //do the query
                        
                        }
		header("Location: upload_images2.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
		exit;
	}
	else{
		header("Location: upload_images2.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
		exit;
	}
}
?>

Open in new window

0
 
aldanchCommented:
I don't see why this wouldn't work... around line 41.

// ORIGINAL
$remote_file = "uploads/".$_FILES["image_upload_box"]["name"];
imagejpeg($image_source,$remote_file,100);
 
// NEW
$newname = date("YmdHis") .".jpg";
$remote_file = "uploads/".$_FILES["image_upload_box"]["name"];
imagejpeg($image_source,$newname,100);

Open in new window

0
 
movieprodwAuthor Commented:
Yeah it is very odd, please see the code below.

Now its not uploading the image at all but its still inserting the info in the db


<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
<?php
 
$propid = $_SESSION['propid'];
 
 
 
// upload the file
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["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
	{
		
  
		// some settings
		$max_upload_width = 2592;
		$max_upload_height = 1944;
		  
		// if user chosed properly then scale down the image according to user preferances
		if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
			$max_upload_width = $_REQUEST['max_width_box'];
		}    
		if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
			$max_upload_height = $_REQUEST['max_height_box'];
		}	
 
		
		// if uploaded image was JPG/JPEG
		if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){	
			$image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
		}		
		// if uploaded image was GIF
		if($_FILES["image_upload_box"]["type"] == "image/gif"){	
			$image_source = imagecreatefromgif($_FILES["image_upload_box"]["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["image_upload_box"]["type"] == "image/bmp"){	
			$image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
		}			
		// if uploaded image was PNG
		if($_FILES["image_upload_box"]["type"] == "image/x-png"){
			$image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
		}
		
		// NEW CODE HERE
		// NEW CODE HERE
		// NEW CODE HERE
		
		$newname = date("YmdHis") .".jpg";
		$remote_file = "uploads/".$_FILES["image_upload_box"]["name"];
		imagejpeg($image_source,$newname,100);
		chmod($remote_file,0644);
	
		// END NEW CODE HERE
		// END NEW CODE HERE
		// END NEW CODE HERE
 
 
 
 
		// 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);
			}		
			else{
				$new_height = $max_upload_height;
				$new_width = round($max_upload_height*$proportions);
			}		
			
			
			$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,80);
			
			imagedestroy($new_image);
		}
		
		imagedestroy($image_source);
		
						$img_name = $file;
                        
                        $query = "SELECT `propid` FROM `images` WHERE `propid` = $propid";
						$result = mysql_query($query);
						if (mysql_num_rows($result) > 0) {
 
						$sql = "INSERT into images (propid,userid,img_name,mainImage) VALUES ('$propid','$userid','$img_name','N')";
                        $result = mysql_db_query($dbname, $sql) or die("Failed Query of " . $sql);  //do the query
                        
  							 } else {
     
						$sql = "INSERT into images (propid,userid,img_name,mainImage) VALUES ('$propid','$userid','$img_name','Y')";
                        $result = mysql_db_query($dbname, $sql) or die("Failed Query of " . $sql);  //do the query
                        
                        }
		header("Location: upload_images2.php?upload_message=Image Uploaded Successfully!&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
		exit;
	}
	else{
		header("Location: upload_images2.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
		exit;
	}
}
?>

Open in new window

0
 
movieprodwAuthor Commented:
It looks to me like it uploads the file into a directory uploads/ then it opens it again, resizes it and re-saves it.

So when we changed it where you said it was looking for the new name and not the original name that it was uploaded under.

So we need to change the name when it makes the last save I believe, I still have not gotten it to work
			$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,80);

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.