Error re-sizing image on upload php – PHP

  php

Q(Question):

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.


//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","1000");
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an errro occures.
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*20480)
{
echo '<h1>You have exceeded the size limit</h1>';
$errors=1;
}
//------------------give unique name to image------------------------------
//get users id to make unique image name
$id = $_SESSION['id'];
//we will give an unique name, for example the time in unix time format
$image_name=$id.'id'.time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="membersPhotos/".$image_name;
$newnameThumb="membersPhotos/thumb".$image_name;
//CURRENTLY NOT WORKING//------------------get image size and set new size------------------------
list($width, $height, $type, $attr) = getimagesize($_FILES['image']['tmp_name']);
if($width==$height){$case=1;}
if($width>$height){$case=2;}
if($width<$height){$case=3;}
//change this value to change the size of the image stored
$imgSize = 250;
//checks shape of image to resize correctly
switch($case)
{
//square
case 1:
$newWidth = $imgSize;
$newHeight = $imgSize;
break;
//lying rectangle
case 2:
$ratio = $width/$height;
$newWidth = $imgSize;
$newHeight = round($newWidth/$ratio);
break;
//standing rectange
case 3:
$ratio = $height/$width;
$newHeight = $imgSize;
$newWidth = round($newHeight/$ratio);
break;
}
echo 'width ' . $width;
echo ' height ' . $height;
echo ' ratio ' . $ratio;
echo ' new width ' . $newWidth;
echo ' new height ' . $newHeight;
echo ' extension ' . $extension;
switch($extension)
{
case 'jpg':
$img = imagecreatefromjpeg($_FILES['image']['tmp_name']);
$thumb = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($thumb,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);
imagejpeg($thumb,$newnameThumb);
break;
case 'jpeg':
$img = imagecreatefromjpeg($_FILES['image']['tmp_name']);
$thumb = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($thumb,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);
imagejpeg($thumb,$newnameThumb);
break;
case 'png':
$img = imagecreatefrompng($_FILES['image']['tmp_name']);
$thumb = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumb,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);
imagepng($thumb,$newnameThumb);
break;
case 'gif':
$img = imagecreatefromgif($_FILES['image']['tmp_name']);
$thumb = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumb,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);
imagegif($thumb,$newnameThumb);
break;
}
//----------------------------------------------------------------------------
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessful</h1>';
$errors=1;
}
}
}
}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully </h1> <img height=\"200px\" src=\"../membersPhotos/" . $image_name . "\">";
//store image name (which is the location) in the db
$connect = mysql_connect("localhost", "root", "Ideas2012!", "whstl_db") or die (mysql_error());
mysql_select_db("whstl_db") or die(mysql_error());
$query2 = "SELECT image_name FROM people_content WHERE id='$id'";
$result=mysql_query($query2) or die (mysql_error());
while($row=mysql_fetch_array($result))
{
//check if there is an old image saved that needs to be deleted
$image_name2=$row['image_name'];
if(empty($image_name2))
{
}
else
{
//delete old image
unlink('membersPhotos/' . $image_name2);
}
}
$query ="UPDATE people_content SET image_name='$image_name' WHERE id='$id'";
$result = mysql_query($query) or die(mysql_error());
}
?>

I apologize if I posted something incorrectly, I am new to this.

A(Answer):

what kind off message are there in your errorlog?
They might give you more info about this…

A(Answer):

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?

A(Answer):

This:

<?php
print 1/0;
print_r(error_get_last());
?>

will output somehting like this:

Array ( [type] => 2 [message] => Division by zero [file] => ......./test_error.php [line] => 2 )

A(Answer):

I get a 500 error when I try adding that to the page for some reason.

A(Answer):

Sorry, crystal ball is not working here.
I need to see code which causes this error.
Normally an error 500 indicated some syntax error….

A(Answer):

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.

LEAVE A COMMENT