By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,558 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,558 IT Pros & Developers. It's quick & easy.

Error re-sizing image on upload php

P: 5
Currently the code works fine for uploading images. I can get the image size and extension as well. However, my error occurs in the switch statement. For some reason imagecreatefromjpeg, imagecreatetruecolor, imagecopyresampled, and imagejpeg do not work. This has been a problem in the development of this site from the beginning and I am nearing completing it and still cannot figure this out. Any help will be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. //define a maxim size for the uploaded images in Kb
  2.  define ("MAX_SIZE","1000"); 
  3.  
  4. //This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
  5.  function getExtension($str) 
  6.  {
  7.      $i = strrpos($str,".");
  8.      if (!$i) { return ""; }
  9.      $l = strlen($str) - $i;
  10.      $ext = substr($str,$i+1,$l);
  11.      return $ext;
  12.  }
  13.  
  14. //This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
  15. //and it will be changed to 1 if an errro occures.  
  16. //If the error occures the file will not be uploaded.
  17.  $errors=0;
  18. //checks if the form has been submitted
  19.  if(isset($_POST['Submit'])) 
  20.  {
  21.      //reads the name of the file the user submitted for uploading
  22.      $image=$_FILES['image']['name'];
  23.      //if it is not empty
  24.      if ($image) 
  25.      {
  26.          //get the original name of the file from the clients machine
  27.          $filename = stripslashes($_FILES['image']['name']);
  28.          //get the extension of the file in a lower case format
  29.           $extension = getExtension($filename);
  30.          $extension = strtolower($extension);
  31.          //if it is not a known extension, we will suppose it is an error and will not  upload the file,  
  32.         //otherwise we will do more tests
  33.          if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
  34.          {
  35.             //print error message
  36.              echo '<h1>Unknown extension</h1>';
  37.              $errors=1;
  38.          }
  39.          else
  40.          {
  41.             //get the size of the image in bytes
  42.              //$_FILES['image']['tmp_name'] is the temporary filename of the file
  43.              //in which the uploaded file was stored on the server
  44.              $size=filesize($_FILES['image']['tmp_name']);
  45.  
  46.             //compare the size with the maxim size we defined and print error if bigger
  47.             if ($size > MAX_SIZE*20480)
  48.             {
  49.                 echo '<h1>You have exceeded the size limit</h1>';
  50.                 $errors=1;
  51.             }
  52.  
  53.             //------------------give unique name to image------------------------------
  54.             //get users id to make unique image name
  55.             $id = $_SESSION['id'];
  56.  
  57.             //we will give an unique name, for example the time in unix time format
  58.             $image_name=$id.'id'.time().'.'.$extension;
  59.             //the new name will be containing the full path where will be stored (images folder)
  60.             $newname="membersPhotos/".$image_name;
  61.             $newnameThumb="membersPhotos/thumb".$image_name;
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.             //CURRENTLY NOT WORKING//------------------get image size and set new size------------------------
  69.             list($width, $height, $type, $attr) = getimagesize($_FILES['image']['tmp_name']);
  70.  
  71.             if($width==$height){$case=1;}
  72.             if($width>$height){$case=2;}
  73.             if($width<$height){$case=3;}
  74.  
  75.             //change this value to change the size of the image stored
  76.             $imgSize = 250;
  77.  
  78.             //checks shape of image to resize correctly
  79.             switch($case)
  80.             {
  81.                 //square
  82.                 case 1:
  83.  
  84.                     $newWidth = $imgSize;
  85.                     $newHeight = $imgSize;
  86.  
  87.                 break;
  88.  
  89.                 //lying rectangle
  90.                 case 2:
  91.  
  92.                     $ratio = $width/$height;
  93.                     $newWidth = $imgSize;
  94.                     $newHeight = round($newWidth/$ratio);
  95.  
  96.                 break;
  97.  
  98.                 //standing rectange
  99.                 case 3:
  100.  
  101.                     $ratio = $height/$width;
  102.                     $newHeight = $imgSize;
  103.                     $newWidth = round($newHeight/$ratio);
  104.  
  105.                 break;    
  106.             }
  107.  
  108.             echo 'width ' . $width;
  109.             echo ' height ' . $height;
  110.             echo ' ratio ' . $ratio;
  111.             echo ' new width ' . $newWidth;
  112.             echo ' new height ' . $newHeight;
  113.             echo ' extension ' . $extension;
  114.  
  115.             switch($extension)
  116.             {
  117.                 case 'jpg':
  118.  
  119.                     $img = imagecreatefromjpeg($_FILES['image']['tmp_name']);
  120.                     $thumb = imagecreatetruecolor($newWidth, $newHeight);
  121.                     imagecopyresampled($thumb,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);
  122.                     imagejpeg($thumb,$newnameThumb);
  123.  
  124.                 break;
  125.  
  126.                 case 'jpeg':
  127.  
  128.                     $img = imagecreatefromjpeg($_FILES['image']['tmp_name']);
  129.                     $thumb = imagecreatetruecolor($newWidth, $newHeight);
  130.                     imagecopyresampled($thumb,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);
  131.                     imagejpeg($thumb,$newnameThumb);
  132.  
  133.                 break;
  134.  
  135.                 case 'png':
  136.  
  137.                     $img = imagecreatefrompng($_FILES['image']['tmp_name']);
  138.                     $thumb = imagecreatetruecolor($newWidth, $newHeight);
  139.                     imagecopyresized($thumb,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);
  140.                     imagepng($thumb,$newnameThumb);
  141.  
  142.                 break;
  143.  
  144.                 case 'gif':
  145.  
  146.                     $img = imagecreatefromgif($_FILES['image']['tmp_name']);
  147.                     $thumb = imagecreatetruecolor($newWidth, $newHeight);
  148.                     imagecopyresized($thumb,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);
  149.                     imagegif($thumb,$newnameThumb);
  150.  
  151.                 break;    
  152.             }
  153.             //----------------------------------------------------------------------------
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.             //we verify if the image has been uploaded, and print error instead
  161.             $copied = copy($_FILES['image']['tmp_name'], $newname);
  162.             if (!$copied) 
  163.             {
  164.                 echo '<h1>Copy unsuccessful</h1>';
  165.                 $errors=1;
  166.             }
  167.         }
  168.     }
  169. }
  170.  
  171. //If no errors registred, print the success message
  172. if(isset($_POST['Submit']) && !$errors) 
  173. {
  174.     echo "<h1>File Uploaded Successfully </h1> <img height=\"200px\" src=\"../membersPhotos/" . $image_name . "\">";
  175.     //store image name (which is the location) in the db
  176.     $connect = mysql_connect("localhost", "root", "Ideas2012!", "whstl_db") or die (mysql_error());
  177.     mysql_select_db("whstl_db") or die(mysql_error());
  178.  
  179.     $query2 = "SELECT image_name FROM people_content WHERE id='$id'";
  180.     $result=mysql_query($query2) or die (mysql_error());
  181.     while($row=mysql_fetch_array($result))
  182.     {
  183.         //check if there is an old image saved that needs to be deleted
  184.         $image_name2=$row['image_name'];
  185.  
  186.         if(empty($image_name2))
  187.         {
  188.         }
  189.         else
  190.         {
  191.             //delete old image 
  192.             unlink('membersPhotos/' . $image_name2);
  193.         }
  194.     }
  195.  
  196.     $query ="UPDATE people_content SET image_name='$image_name' WHERE id='$id'";
  197.     $result = mysql_query($query) or die(mysql_error());
  198. }
  199. ?>
  200.  
I apologize if I posted something incorrectly, I am new to this.
Apr 3 '12 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 1,035
what kind off message are there in your errorlog?
They might give you more info about this...
Apr 7 '12 #2

P: 5
I'm connected to a mapped drive on another computer that is connected to the live server. The site is on a port for testing purposes so I only get the html 500 error. Is there another way to check my error log?
Apr 9 '12 #3

Expert 100+
P: 1,035
This:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.         print 1/0;
  3.         print_r(error_get_last());
  4. ?>
will output somehting like this:
Expand|Select|Wrap|Line Numbers
  1. Array ( [type] => 2 [message] => Division by zero [file] => ......./test_error.php [line] => 2 ) 
Apr 9 '12 #4

P: 5
I get a 500 error when I try adding that to the page for some reason.
Apr 10 '12 #5

Expert 100+
P: 1,035
Sorry, crystal ball is not working here.
I need to see code which causes this error.
Normally an error 500 indicated some syntax error....
Apr 11 '12 #6

P: 5
Well thank you for the help, I do appreciate it. If I get a way to see the error I will post it. Thanks again.
Apr 18 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.