Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image upload and rename

Status
Not open for further replies.
Jul 28, 2005
358
FR
Hi all,

I think I am having one of those Friday moments, and just can't work out what to do here.

I have a form which posts image files up to a script. I want to be able to rename these files so they are tied to a certain record in a database. The format of the new file name will be like this: v101.jpg where v is a value set by me, 1 will be a identifier to the user taken from the database and 01 will be the number of photo. The user actually uploads 5 images so for user 1 there will be these files. The _ is just a seperator to split the user and image name from each other.

v1_01.jpg
v1_02.jpg
v1_03.jpg
v1_04.jpg
v1_05.jpg

I have tried to do the rename but it just doesn't work.

Here is the code I use to upload the image (this does it with the original file name:

Code:
//uploads submitted pictures to folders and creates thumbnail
$picturearray[0]=$_FILES['picture1'];
$picturearray[1]=$_FILES['picture2'];
$picturearray[2]=$_FILES['picture3'];
$picturearray[3]=$_FILES['picture4'];
$picturearray[4]=$_files['picture5'];
$piccount=0;
while ($piccount <=4){
	
$idir = "files/"; // Path To Images Directory
$tdir = "files/thumbs/"; // Path To Thumbnails Directory
$twidth = "125"; // Maximum Width For Thumbnail Images
$theight = "100"; // Maximum Height For Thumbnail Images
$url = $picturearray [$piccount]['name'];
// Set $url To Equal The Filename For Later Use
if ($picturearray [$piccount]['type'] == "image/jpg" || $picturearray [$piccount]['type']
  == "image/jpeg" || $picturearray [$piccount]['type'] == "image/pjpeg")
  {
  $file_ext = strrchr($picturearray [$piccount]['name'], '.');
  // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
  $copy = copy($picturearray [$piccount]['tmp_name'], "$idir".$picturearray [$piccount]['name'
  ]); // Move Image From Temporary Location To Permanent Location
  if ($copy)
    {
    //If The Script Was Able To Copy The Image To Its Permanent Location
    print'Image uploaded successfully.<br />';
    // Was Able To Successfully Upload Image
    $simg = imagecreatefromjpeg("$idir".$url);
    // Make A New Temporary Image To Create The Thumbanil From
    $currwidth = imagesx($simg); 
	// Current Image Width
    $currheight = imagesy($simg); 
	// Current Image Height
    if ($currheight > $currwidth)
      {
      //If Height Is Greater Than Width
      $zoom = $twidth / $currheight; 
	  // Length Ratio For Width
      $newheight = $theight; 
	  // Height Is Equal To Max Height
      $newwidth = $currwidth * $zoom; 
	  // Creates The New Width
      }
    else
      {
      // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
      $zoom = $twidth / $currwidth; // Length Ratio For Height
      $newwidth = $twidth; // Width Is Equal To Max Width
      $newheight = $currheight * $zoom; // Creates The New Height
      }
    $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
    imagetruecolortopalette($simg, false, 256); // Create New Color Pallete
    $palsize = ImageColorsTotal($simg);
    for ($i = 0; $i < $palsize; $i++)
      {
      // Counting Colors In The Image
      $colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
      ImageColorAllocate($dimg, $colors['red'], $colors['green'],
        $colors['blue']);
      // Tell The Server What Colors This Image Will Use
      }
    imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight,
      $currwidth, $currheight);
      // Copy Resized Image To The New Image (So We Can Save It)
    imagejpeg($dimg, "$tdir".$url); // Saving The Image
    imagedestroy($simg); // Destroying The Temporary Image
    imagedestroy($dimg); // Destroying The Other Temporary Image
    print'Image thumbnail created successfully.'; // Resize successful
    }
  else
    {
    print'<font color="#FF0000">ERROR: Unable to upload image.</font>';
    // Error Message If Upload Failed
    }
  }
else
  {
  print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg.
      Yours is '; // Error Message If Filetype Is Wrong
  print $file_ext; // Show The Invalid File's Extention
  print'.</font>';
}
$piccount++;
}

As you will notice I also create a thumbnail so I would need this to have the new name too.

I'm sure that when someone points out how to do this I am going to be hitting myself, but I just can't work it out.

Any ideas?

Richard
 
It's all right now. I knew it must be a Friday thing.

Sorted it now.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top