I am having a problem with an upload/resize script.

I found this in one of the threads on the topic in our forum.
http://phpbuilder.com/board/showthread.php?t=10316743&highlight=image+upload+resize

$propertyname = $_POST['propertyname'];
$message1 = NULL; 
$message2 = NULL; 
$message3 = NULL; 
$idir = "../images/full/";   // Path To Images Directory 
$tdir = "../images/thumbnails/";   // Path To Thumbnails Directory 
$twidth = "125";   // Maximum Width For Thumbnail Images 
$theight = "125";   // Maximum Height For Thumbnail Images 
if(isset($_POST['propertyname'])) { 
    if ($_FILES['picture']['error'] == UPLOAD_ERR_OK) { 
		require_once('../Connections/RogerSolem.php'); 

	$query = "SELECT propertyname FROM rentals ORDER BY propertyname DESC LIMIT 1"; 
    $result = @mysql_query($query); 
    $row = mysql_fetch_array($result, MYSQL_NUM); 
    $propertyname = $row[0];  
    $extension = explode ('.', $_FILES['picture']['name']); 
    $ext = $extension[1]; 
    $uid = mysql_insert_id(); 
    $url = $uid.'.'.$ext; // Set $url To Equal The Filename For Later Use 
    $size =  ($_FILES['picture']['size'] / 1024); 
    if ($_FILES['picture']['size'] < 131072) { 
        if ($_FILES['picture']['type'] == "image/jpg" || $_FILES['picture']['type'] == "image/jpeg" || $_FILES['picture']['type'] == "image/pjpeg") { //Handle JPEG 
            $copy = copy($_FILES['picture']['tmp_name'], "$idir" . "$url");   // Move Image From Temporary Location To Permanent Location 
            if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location 
                $message1 .= 'Image uploaded successfully.';   // 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 = imagecreatetruecolor($newwidth, $newheight);   // Make New Image For Thumbnail 
                imagecopyresampled($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 
                $message2 .= 'Image resized successfully.<br><br>';   // Resize successful 
            } 
			else { 
                $message2 .= 'Unable to resize image.<br><br>';   // Error Message If Upload Failed 
            } 
            } else { 
            if ($_FILES['picture']['type'] == "image/gif") {  //Handle GIF 
                $copy = copy($_FILES['picture']['tmp_name'], "$idir" . "$url");   // Move Image From Temporary Location To Permanent Location 
                if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location 
                    $message1 .= 'Image uploaded successfully.';   // Was Able To Successfully Upload Image 
                    $simg = imagecreatefromgif("$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 = imagecreatetruecolor($newwidth, $newheight);   // Make New Image For Thumbnail 
                    imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
                    imagegif($dimg, "$tdir" . $url);   // Saving The Image 
                    imagedestroy($simg);   // Destroying The Temporary Image 
                    imagedestroy($dimg);   // Destroying The Other Temporary Image 
                    $message2 .= 'Image resized successfully.<br><br>';   // Resize successful 
                    } else { 
                    $message2 .= 'Unable to resize image.<br><br>';   // Error Message If Upload Failed 
                } 
                } else { 
                if($_FILES['picture']['type'] == "image/png" || $_FILES['picture']['type'] == "image/x-png") { //Handle PNG 
                    $copy = copy($_FILES['picture']['tmp_name'], "$idir" . "$url");   // Move Image From Temporary Location To Permanent Location 
                    if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location 
                        $message1 .= 'Image uploaded successfully.';   // Was Able To Successfully Upload Image 
                        $simg = imagecreatefrompng("$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 = imagecreatetruecolor($newwidth, $newheight);   // Make New Image For Thumbnail 
                        imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
                        imagepng($dimg, "$tdir" . $url);   // Saving The Image 
                        imagedestroy($simg);   // Destroying The Temporary Image 
                        imagedestroy($dimg);   // Destroying The Other Temporary Image 
                        $message2 .= 'Image resized successfully.<br><br>';   // Resize successful 
                        } else { 
                        $message2 .= 'Unable to resize image.<br><br>';   // Error Message If Upload Failed 
                    } 
                    } else { 
                    $message1 .= "<font color=\"#8DA900\">Picture Not Uploaded.</font> Incorrect file type <font color=\"#8DA900\">.$ext</font>. 
                    Please upload .jpeg, .gif , or .png files only.";   // Error Message If Filetype Is Wrong 
                } 
            } 
        }   
        } else { 
        $message3 .= "<font color=\"#8DA900\">Picture Not Uploaded.</font> File size <font color=\"#8DA900\">$size KB</font> 
        is too large. Size Limit: 128KB.<br><br>";   // Error Message If File is over the size limit
    } 	
} 
} 


$query3 = "UPDATE rentals SET image = '..images/full/$url' WHERE propertyname = '$propertyname'"; 
$result3 = @mysql_query($query3); 

$query6 = "UPDATE rentals SET thumbnail = '../images/thumbnails/$url' WHERE propertyname = '$propertyname'"; 
$result6 = @mysql_query($query6); 

    When I run the script I am not recieving any errors.

    The script is not uploading the file or submitting the url to the database.

    It did upload once but inserted the URL on the wrong row in my table. I got it to update a record in my table, I ran it again with a different image and a different record, the script inserted the same file on a different incorrect record (1st file). I think I have changed all of the site dependant variables from the original script. I thought I found why it was uploading to the incorrect record, made a chage, now it will not upload at all.

    GD is enabled, my form is enctype "multipart/data" and the folders are CHMOD 777.

    Issues:
    1. seems to be inserting a file 0.jpeg, I am not sure if this will autoincrement, (ex. 1.jpeg, 2.jpeg)

    1. The record is not being inserted into the database on the row I am asking it to update to.

    2. No longer uploading a file. (Maybe it is uploading a file but isnt incrementing, unable to overwrite 0.jpeg.

    I think one of the main issues is that I am running this script within the header of the page. When a user clicks submit on the form the script is run from the header of the page, it inserts a whole bunch of info into my database then this script runs to create the full size and thumbnail images.

    Let me know if you see anything wrong.

    Thanks in advance.

      EDIT: sorry began my reply before you posted additional info
      Your problem being?

      Here's a copy of my script for making thumbnails which you're welcome to use/fiddle with if you like:

      <?
      /**
       * MODULE: make_tn
       * creates a thumbnail image from the file passed as the 'image' parameter.
       * (this module is not implemented in an object oriented manner because such an implementation would
       * corrupt the image data).
       */
      
      header("Content-type: image/jpeg");
      
      $file_uri = $_GET['image'];
      $file_name = ($file_uri{0} == "/") ? substr($file_uri, 1) : $file_uri;
      $file_ext = strtolower(substr($file_name, strrpos($file_name, ".")+1));
      $new_h = 30;
      $new_w = 30;
      
      $src_img = null;
      
      if ($file_ext == "jpg")
      {
      	$src_img = imagecreatefromjpeg($file_name);
      }
      else if ($file_ext == "png")
      {
      	$src_img = imagecreatefrompng($file_name);
      }
      else if ($file_ext == "gif")
      {
      	$src_img = imagecreatefromgif($file_name);
      }
      else
      {
      	exit;
      }
      
      $old_x = imageSX($src_img);
      $old_y = imageSY($src_img);
      
      if ($old_x > $old_y)
      {
      	$thumb_w = $new_w;
      	$thumb_h = $old_y * ($new_h / $old_x);
      }
      if ($old_x < $old_y)
      {
      	$thumb_w = $old_x * ($new_w / $old_y);
      	$thumb_h = $new_h;
      }
      if ($old_x == $old_y)
      {
      	$thumb_w = $new_w;
      	$thumb_h = $new_h;
      }
      
      $dst_image = null;
      
      if ($gd2 == "")
      {
      	$dst_img = ImageCreate($thumb_w, $thumb_h);
      	imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
      }
      else
      {
      	$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
      	imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
      }
      
      imagejpeg($dst_img);
      
      imagedestroy($dst_img);
      imagedestroy($src_img);
      
      ?>
      

      You'll need to have PHP's GD library installed. (Might this be your existing problem?)

        Looks good, I can double it up, put 2's behind the variable names and have it run twice, once for thumbnails, once for my fullsize, (I dont want to try and display 1200x1024 images I am confident my client will be uploading). I think I will need to change your $GET to a $POST becuase of the way my form is set up. I will also need to save the file name in my database. Looking at the script if I run an update query with $file_uri set as the data for my field, should work.

        Where in this script do you define the default directories for the thumbs to be put in?

          Where in this script do you define the default directories for the thumbs to be put in?

          I don't. Like I said, I didn't realise what you were up to and my script probably won't be of much help. I use it as the src attribute of HTML IMG tags:

          <img src="http://domain.net/make_tn.php?image=/path/to/source-image.jpg" />

          It doesn't even let you chose the display size, nevermind do anything interesting with the resulting image. Its a bit crude really, but it suits my needs. Sorry.

          (Having said that, you may be able to use the resulting images if you refer to them using URIs; i.e. have them always dynamically generated rather than cached.)

            Write a Reply...