Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to I rename the image?

Posted on 2009-04-16
12
Medium Priority
?
478 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:movieprodw
  • 7
  • 5
12 Comments
 
LVL 8

Expert Comment

by:aldanch
ID: 24160578
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
 
LVL 1

Author Comment

by:movieprodw
ID: 24160646
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
 
LVL 8

Expert Comment

by:aldanch
ID: 24160767
Make sure the directory you are saving the image has the correct permissions.

What error did you get?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:movieprodw
ID: 24161076
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
 
LVL 8

Expert Comment

by:aldanch
ID: 24161508
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
 
LVL 1

Author Comment

by:movieprodw
ID: 24161515
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
 
LVL 8

Expert Comment

by:aldanch
ID: 24161704
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
 
LVL 1

Author Comment

by:movieprodw
ID: 24161747
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
 
LVL 8

Expert Comment

by:aldanch
ID: 24161822
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
 
LVL 1

Author Comment

by:movieprodw
ID: 24161966
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
 
LVL 1

Author Comment

by:movieprodw
ID: 24163493
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
 
LVL 1

Accepted Solution

by:
movieprodw earned 0 total points
ID: 24164664
got it!
$remote_file = "uploads/".$newname;
imagejpeg($image_source,$remote_file,100);

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question