0

Hi everyone,

I have been working on an image gallery to allow people to view/upload their photos. I have everything working pretty well except that I want to modify my upload script so that a thumbnail is generated of each picture that is uploaded and stored in a separate directory with the same name as the original picture. So there would be an "Uploads" directory where all full size images are stored, and then within that directory there would be a "Thumbs" directory where all the thumbnails will be stored (with the same name as the full size image). My script adds the full size image to a table in MySQL, but I DO NOT need the thumbnail in the table(since it will have the same name as the full size image). Here is my script at this point:

<?php
  session_id($_POST['current_email']);
  session_start();
  if (!empty($_FILES)) {
      $con = mysql_connect("localhost", "xxx", "xxx") or die("cannot connect");
      mysql_select_db("xxx", $con) or die("cannot select DB");
      $tempFile = $_FILES["Filedata"]["tmp_name"];
      $name = $_FILES["Filedata"]["name"];
      $targetPath = "uploads/";
      $targetFile = str_replace('//', '/', $targetPath) . $_FILES["Filedata"]['name'];
      $size = $_FILES["Filedata"]["size"];
      $oext = getExtension($name);
      $ext = strtolower($oext);
      $whois = $_SERVER['REMOTE_ADDR'];
      $email = $_POST['current_email'];
      if ($ext == "jpg" || $ext == "jpeg" || $ext == "bmp" || $ext == "gif") {
          if ($size < 1024 * 1024) {
              if (file_exists("uploads/" . $name)) {
                  move_uploaded_file($tempFile, "uploads/" . $name);
                  $qry = "select id from pictures where file='$name' and type='$ext'";
                  $res = mysql_fetch_array(mysql_query($qry));
                  $id = $res['id'];
                  $qry = "UPDATE pictures SET file='$name', type='$ext', size='$size', whois='$whois', date=NOW() where id=$id";
                  mysql_query($qry);
                  echo "1";
              } else {
                  move_uploaded_file($tempFile, "uploads/" . $name);
                  $qry = "INSERT INTO pictures(id, file, type, size, email, whois, date) VALUES ('', '$name', '$ext', '$size', '$email', '$whois', NOW())";
                  //Start buffering
                  ob_start();
                  //print the result
                  print_r($qry);
                  //get the result from buffer
                  $output = ob_get_contents();
                  //close buffer
                  ob_end_clean();
                  //open a file
                  $h = fopen('log.txt', 'w+');
                  //write the output text
                  fwrite($h, $output);
                  //close file
                  fclose($h);
                  mysql_query($qry, $con);
                  echo "1";
              }
          }

      }
  }
  function getExtension($image_name)
  {
      return substr($image_name, strrpos($image_name, '.') + 1);
  }
?>

I have googled a few examples, but i am just unsure on how to make it work within my script. And being new to php, something like this takes me days to figure out :P Any help is greatly appreciated!

2
Contributors
3
Replies
6
Views
7 Years
Discussion Span
Last Post by dschuett
0

Hi,

I think I know what you are asking for. You will need to use the GD Library to create a thumbnail of an image.

It should be easy to upload the image.

Creating a thumbnail
You should check out this script: http://snipplr.com/view/753/create-a-thumbnail-maintaining-aspect-ratio-using-gd/. It can create a thumbnail maintaining the aspect ratio so it isn't stretched.

This is another example, this time in a tutorial format: http://www.phpro.org/tutorials/Imagick.html. It can resize an image keeping the aspect ratio.

0

Hi,

I think I know what you are asking for. You will need to use the GD Library to create a thumbnail of an image.

It should be easy to upload the image.

Creating a thumbnail
You should check out this script: http://snipplr.com/view/753/create-a-thumbnail-maintaining-aspect-ratio-using-gd/. It can create a thumbnail maintaining the aspect ratio so it isn't stretched.

This is another example, this time in a tutorial format: http://www.phpro.org/tutorials/Imagick.html. It can resize an image keeping the aspect ratio.

Thanks so much for the links!

the problem is that I am VERY new to php, and my upload script that I provided in my first post, believe it or not, took me DAYS to get it working. I guess I (for the most part) understand the scripts you posted, but I just don't know how to implement them into my existing script without breaking it...(that is the bad part about being a newbie).

Three keys here:
1. full image name/info gets written to the database(just as it does in my existing script) but the thumbnail does NOT need to get written to the database.
2. thumbnail should have the same name as the full size image and moved to the thumbs folder.
3. if thumbnail already exists, just overwrite it.

0

I got it working with this function:

function create_thumb($pathToImage, $pathToThumb, $thumbWidth)
		    {
		    $img = imagecreatefromjpeg($pathToImage);
		    $width = imagesx($img);
		    $height = imagesy($img);
		    $new_width = $thumbWidth;
		    $new_height = floor($height * ($thumbWidth / $width));
		    $tmp_img = imagecreatetruecolor($new_width, $new_height);
		    imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		    imagejpeg($tmp_img, $pathToThumb);
	      	    imagedestroy($tmp_img);
		    imagedestroy($img);
		    }
		    create_thumb('uploads/' . $name, 'uploads/thumbs/' . $name, 100);
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.