473,378 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Error re-sizing image on upload php

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
6 2114
Luuk
1,047 Expert 1GB
what kind off message are there in your errorlog?
They might give you more info about this...
Apr 7 '12 #2
PawelJ
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
Luuk
1,047 Expert 1GB
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
PawelJ
5
I get a 500 error when I try adding that to the page for some reason.
Apr 10 '12 #5
Luuk
1,047 Expert 1GB
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
PawelJ
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

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

Similar topics

5
by: aaj | last post by:
Hi all I have written a small app on my machine that I have passed on to a colleague for testing. The problem is, as soon as he steps through in debug mode and reaches try { conn.Open();...
1
by: Sektor van Skijlen | last post by:
couldn't compile regular expression pattern: quantifier operand invalid while executing "expect -nobrace >|#S {} -re {* } { lappend response_line $expect_out(buffer); exp_continue }" invoked from...
49
by: Mal | last post by:
Hi, As I gain knowledge through a lot of trial, error, and usenet posts.. I have a potentially odd question. I am using a commercial access application. It is a front-end / back...
3
by: M Fisher | last post by:
I have an Access XP Database. On one of the forms I have a button that opens and Excel Spreadsheet as follows: Dim xl As Excel.Application Set xl = New Excel.Application...
4
by: redryderridesagain | last post by:
My macro is writing to a table(T) and reading a query (Q) based on that table (VBA/Visual Basic 6.3). I cannot write T and read Q in the same execution of the macro, however, if I skip the writing...
18
by: Daniel Rudy | last post by:
When I try to compile the program below, I get these errors: strata:/home/dcrudy/c/exercise 1131 $$$ ->cc -g -oex7-2 ex7-2.c ex7-2.c: In function `conv_julian': ex7-2.c:102: syntax error before...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
14
by: tom t/LA | last post by:
Here is a function to convert a CSV file to a Javascript array. Uses idealized file reading functions based on the std C library, since there is no Javascript standard. Not fully tested. ...
2
by: Ronald S. Cook | last post by:
Hi, Some users on our domain can run our WCF application no problem. Some get an unhandled exception error re: SOAP security negotiation. I'm wanting the service to not be secure.. I just want...
2
by: Mark05 | last post by:
#include<conio.h> #include<iostream> using namespace std; int main() { double ManfCode,ProdCode; int Checkdigit, SecondManf, FourthManf; int FirstProd,ThirdProd,FifthProd; int...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.