The following script works GREAT. It uploads an image as well as adds info to mysql about the picture and its location. The problem is that the function "CreateThumbs" actually loops through the entire uploads directory and generates a thumbnail EVERY TIME you upload a picture. I am VERY new to php, and I was just wonder what I need to do to get it to ONLY generate a thumbnail for the picture that is being uploaded and NOT every picture within the uploads directory. - This means that if there are thousands of pictures in the uploads directory, every time someone uploads a single pic, it will loop through and create a thumbnail of EVERY pic that exists in that directory.

How can i make it so it ONLY creates a thumbnail for the pic that is being uploaded?

<?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'];
      $safename = mysql_real_escape_string($name);
      $safesize = mysql_real_escape_string($size);
      $safeext = mysql_real_escape_string($ext);
      $safewhois = mysql_real_escape_string($whois);
      $safeemail = mysql_real_escape_string($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'];
                  $safename = mysql_real_escape_string($name);
                  $safesize = mysql_real_escape_string($size);
                  $safeext = mysql_real_escape_string($ext);
                  $safewhois = mysql_real_escape_string($whois);
                  $safeemail = mysql_real_escape_string($email);
                  $qry = "UPDATE pictures SET file='$safename', type='$safeext', size='$safesize', whois='$safewhois', date=NOW() where id=$id";
                  mysql_query($qry);
//HERE IS WHERE IT CREATES THE THUMBNAILS
                  function createThumbs($pathToImages, $pathToThumbs, $thumbWidth)
                  {
                      $dir = opendir($pathToImages);
                      while (false !== ($fname = readdir($dir))) {
                          $info = pathinfo($pathToImages . $fname);
                          if (strtolower($info['extension']) == 'jpg') {
                              $img = imagecreatefromjpeg("{$pathToImages}{$fname}");
                              $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, "{$pathToThumbs}{$fname}");
                              imagedestroy($tmp_img);
                              imagedestroy($img);
                          }
                      }
                      closedir($dir);
                  }
                  createThumbs("uploads/", "uploads/thumbs/", 87);
                  echo "1";
              } else {
                  move_uploaded_file($tempFile, "uploads/" . $name);
                  $qry = "INSERT INTO pictures(id, file, type, size, email, whois, date) VALUES ('', '$safename', '$safeext', '$safesize', '$safeemail', '$safewhois', NOW())";
                  mysql_query($qry, $con);
//HERE IS WHERE IT CREATES THE THUMBNAILS
                  function createThumbs($pathToImages, $pathToThumbs, $thumbWidth)
                  {
                      $dir = opendir($pathToImages);
                      while (false !== ($fname = readdir($dir))) {
                          $info = pathinfo($pathToImages . $fname);
                          if (strtolower($info['extension']) == 'jpg') {
                              $img = imagecreatefromjpeg("{$pathToImages}{$fname}");
                              $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, "{$pathToThumbs}{$fname}");
                              imagedestroy($tmp_img);
                              imagedestroy($img);
                          }
                      }
                      closedir($dir);
                  }
                  createThumbs("uploads/", "uploads/thumbs/", 87);
                  echo "1";
              }
          }
      }
  }
  function getExtension($image_name)
  {
      return substr($image_name, strrpos($image_name, '.') + 1);
  }
?>

I was thinking that I would need to somehow just have it search for $name (which I define at the beginning of my code -which is the name of the image e.g. image.jpg) instead of $pathToImages. I am just too new to understand how to do that.

Thanks for your time!

Recommended Answers

All 3 Replies

You can use this function. Pass the full path to the new image, the thumbdir and the width:

function createThumb($pathToImage, $pathToThumbs, $thumbWidth) {
    $info = pathinfo($pathToImage);
    if (strtolower($info['extension']) == 'jpg') {
      $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);
      $fname = $info['basename'];
      imagejpeg($tmp_img, "{$pathToThumbs}{$fname}");
      imagedestroy($tmp_img);
      imagedestroy($img);
    }
  }
}

Thanks pritaeas,

I got it working with this:

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);
                  echo "1";
              }

However, would you have any idea why the thumbnails are SUPER blurry and just have VERY bad quality?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.