Connecting Tech Pros Worldwide Help | Site Map

Adding number to image upload name

Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#1: Jun 24 '09
The below is turning up an error message - Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Expand|Select|Wrap|Line Numbers
  1. $imagepath = ".$_SESSION['user_id']."."_"."$imagepath";
I'm trying to ad the user_id to an uploaded image name to make it unique and identifiable.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: Jun 24 '09

re: Adding number to image upload name


Quote:

Originally Posted by DavidPr View Post

Expand|Select|Wrap|Line Numbers
  1. $imagepath = ".$_SESSION['user_id']."."_"."$imagepath";
.

That makes no sense - Why start the string just to close it immediately?

Anyway, double-quoted strings parse any variables within them - so you don't need to concatenate them in.

Expand|Select|Wrap|Line Numbers
  1. $imagepath = "{$_SESSION['user_id']}_{$imagepath}";
  2. // or
  3. $imagepath = $_SESSION['user_id'] . '_' . $imagepath;
  4.  
Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#3: Jun 25 '09

re: Adding number to image upload name


OK, nothing I've tried thus far has even remotely worked.

I have to create a unique name for the image. I would like to use the user's user_id to do this. This way I can identify the photo as belonging to whomever.

The images aren't stored in the database, just the name and sizes. But the image_name in the database will be the same as the image names of the photos in the folders - pgallery/ and pgallery/thumbs, and will be used in a script to display the images.

Ideally the image name would look like this in the database and in the folders: 12_boat.jpg - where 12 is the person's user_id. But how to do this?

Here's the image script:

Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['submit'])){
  2.   if (isset ($_FILES['new_image'])){
  3.       $imagename = $_FILES['new_image']['name'];
  4.       $source = $_FILES['new_image']['tmp_name'];
  5.  
  6.       $target = "./../pgallery/".$imagename;
  7.       move_uploaded_file($source, $target);
  8.  
  9.       $imagepath = $imagename;
  10.  
  11.       $save = "./../pgallery/" . $imagepath; //This is the new file you saving
  12.       $file = "./../pgallery/" . $imagepath; //This is the original file
  13.  
  14.       list($width, $height) = getimagesize($file) ; 
  15.  
  16.       $b_width = 640;
  17.       $b_height = 480;
  18.  
  19.  
  20. if($width > $height) 
  21.       $diff = $width / $b_width;
  22.       $b_height = $height / $diff; 
  23.  
  24. }
  25. elseif($height >= $width) 
  26.       $diff = $height / $b_height;
  27.       $b_width = $width / $diff; 
  28. }
  29.  
  30.  
  31.  
  32.  
  33.       $tn = imagecreatetruecolor($b_width, $b_height) ; 
  34.       $image = imagecreatefromjpeg($file) ; 
  35.       imagecopyresampled($tn, $image, 0, 0, 0, 0, $b_width, $b_height, $width, $height) ; 
  36.  
  37.       imagejpeg($tn, $save, 100) ; 
  38.  
  39.       $save = "./../pgallery/thumbs/" . $imagepath; //This is the new file you saving
  40.       $file = "./../pgallery/" . $imagepath; //This is the original file
  41.  
  42.       list($b_width, $b_height) = getimagesize($file) ; 
  43.  
  44.       $t_width = 100; 
  45.       $t_height = 100;
  46.  
  47.  
  48. if($b_width > $b_height) 
  49.       $diff = $b_width / $t_width;
  50.       $t_height = $b_height / $diff; 
  51.  
  52. }
  53. elseif($b_height >= $b_width) 
  54.       $diff = $b_height / $t_height;
  55.       $t_width = $b_width / $diff; 
  56. }
  57.  
  58.  
  59.  
  60.  
  61.       $tn = imagecreatetruecolor($t_width, $t_height) ; 
  62.       $image = imagecreatefromjpeg($file) ; 
  63.       imagecopyresampled($tn, $image, 0, 0, 0, 0, $t_width, $t_height, $b_width, $b_height) ; 
  64.  
  65.       imagejpeg($tn, $save, 100) ; 
  66.  

What this script does is take a large image file and reduce it in size. It is this reduced image that is put in the pgallery folder and is used to create the thumbnail image that will go into the pgallery/thumbs folder.
Reply