Page 1 of 1
Server Problem or Code Problem
Posted: Sat Apr 04, 2009 9:58 am
by The Omen
I've created a script that uploads files to my server.
At the moment it is just dealing with image files. I uploaded a few small ones and thought it was working. I then noticed it gave out an error when I tried to upload a photo.
I have tested to see if it was based on the image dimensions, but it wasn't. In the end I realised it was based on the image size. I have used Photoshop to have two versions of the same photo - one with reduced quality (smaller file size). The smaller one loads fine, but the larger one doesn't.
The code where it goes wrong is a basic select statement:
Code: Select all
switch ($image_type)
{
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
With larger files it drops through to the default option and gives me the "unsupported filetype" error.
I think it is a server problem but the server guy tells me it's my code. He has assured me that the php.ini has a memory limit of 64mb set.
Anyone have any ideas? I'm really stuck.
Re: Server Problem or Code Problem
Posted: Sat Apr 04, 2009 10:06 am
by ben.artiss
How are you defining the $image_type? Is it a jpeg you're working with possibly in internet explorer? I remember internet explorer was quirky with the mime-type (it's sometimes something like image/p-jpeg or something). Sorry I can't be any clearer, but other than that I can't really help without a little bit more code.

Re: Server Problem or Code Problem
Posted: Sat Apr 04, 2009 10:14 am
by The Omen
I'll try and break my code up and display it as clearly as I can (I don't want to post the whole page as it will probably be incredibly boring to look through!)
The page loads with a simple upload box:
Code: Select all
<h2>Upload and Resize an Image</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
<p>Upload Image: <input type="file" name="imgfile"><br>
<font size="1">Click browse to upload a local file</font><br>
<br>
<input type="submit" value="Upload Image">
</form>
It then picks up the image when it is submitted and calls a function:
Code: Select all
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$random_name = resize($imgfile, 100, "th_".$imgfile_name, $imgfile_name);
It then tries to work out the image size and prepares the image to make a thumbnail
Code: Select all
function resize($img, $thumb_width, $newfilename, $filename)
{
$max_width=$thumb_width;
//Check if GD extension is loaded
if (!extension_loaded('gd') && !extension_loaded('gd2'))
{
trigger_error("GD is not loaded", E_USER_WARNING);
return false;
}
//Get Image size info
list($width_orig, $height_orig, $image_type) = getimagesize($img);
switch ($image_type)
{
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
I'll post the whole code for the page below this in case that is more helpful.
Re: Server Problem or Code Problem
Posted: Sat Apr 04, 2009 10:16 am
by The Omen
The whole code takes an image, builds a thumbnail, saves the original and writes the links into an XML file.
Code: Select all
<html>
<head>
<title>EdSafe Upload Jpg</title>
<link type="text/css" rel="stylesheet" href="css/lightbox.css">
<?php
// Set enough memory aside to load larger images
//ini_set("memory_limit", "134217728");
//ini_set('gd.jpeg_ignore_warning', 1);
/* here we must specify the version of XML : i.e: 1.0 */
$xml = new DomDocument('1.0');
$xml->load('ceramics2.xml');
/*== FUNCTIONS ==*/
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
function resize($img, $thumb_width, $newfilename, $filename)
{
$max_width=$thumb_width;
//Check if GD extension is loaded
if (!extension_loaded('gd') && !extension_loaded('gd2'))
{
trigger_error("GD is not loaded", E_USER_WARNING);
return false;
}
//Get Image size info
list($width_orig, $height_orig, $image_type) = getimagesize($img);
switch ($image_type)
{
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
/*** calculate the aspect ratio ***/
$aspect_ratio = (float) $height_orig / $width_orig;
/*** calulate the thumbnail width based on the height ***/
$thumb_height = round($thumb_width * $aspect_ratio);
while($thumb_height>$max_width)
{
$thumb_width-=10;
$thumb_height = round($thumb_width * $aspect_ratio);
}
$newImg = imagecreatetruecolor($thumb_width, $thumb_height);
/* Check if this image is PNG or GIF, then set if Transparent*/
if(($image_type == 1) OR ($image_type==3))
{
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent);
}
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);
// Generate a random file name
$the_date = getdate(date("U"));
$random_name_th = "th_".$the_date[0].$filename;
$random_name = $the_date[0].$filename;
//Generate the file, and rename it to $newfilename
switch ($image_type)
{
case 1: imagegif($newImg,$random_name_th);
imagegif($im,$random_name);
break;
case 2: imagejpeg($newImg,$random_name_th);
imagejpeg($im,$random_name);
break;
case 3: imagepng($newImg,$random_name_th);
imagepng($im,$random_name);
break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}
return $random_name;
}
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$random_name = resize($imgfile, 100, "th_".$imgfile_name, $imgfile_name);
//Add the image to the XML document
$newLibraryImage = $xml->createElement('libraryimage');
//Create the name element
$newImageName = $xml->createElement('name');
$newImageNameNode = $xml->createTextNode ("");
$newImageName -> appendChild($newImageNameNode);
//Create the link element
$newImageLink = $xml->createElement('link');
$newImageLinkNode = $xml->createTextNode ($random_name);
$newImageLink -> appendChild($newImageLinkNode);
//Create the thumb element
$newThumbLink = $xml->createElement('thumb');
$newThumbLinkNode = $xml->createTextNode ('th_'.$random_name);
$newThumbLink -> appendChild($newThumbLinkNode);
$newLibraryImage -> appendChild($newImageName);
$newLibraryImage -> appendChild($newImageLink);
$newLibraryImage -> appendChild($newThumbLink);
$XMLlibrary = $xml->getElementsByTagName('library')->item(0);
$XMLlibrary -> appendChild ($newLibraryImage);
$xml->save("ceramics2.xml");
}
?>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="js/lightbox.js"></script>
</head>
<body bgcolor="#FFFFFF">
<table>
<tr>
<?php foreach($xml->getElementsBytagName('libraryimage') as $libraryimage):
/* find the thumb image */
$ithumb = $libraryimage->getElementsByTagName('thumb')->item(0)->firstChild->nodeValue;
$ilink = $libraryimage->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
?>
<td><a href="<?php echo($ilink) ?>" rel="lightbox[group]"><img src="<?php echo($ithumb) ?>"/></a></td>
<?php endforeach; ?>
</tr>
<tr>
<?php foreach($xml->getElementsBytagName('libraryimage') as $libraryimage):
/* find the title */
$iname = $libraryimage->getElementsByTagName('name')->item(0)->firstChild->nodeValue;
?>
<td><?php echo($iname) ?></td>
<?php endforeach; ?>
</tr>
</table>
<h2>Upload and Resize an Image</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
<p>Upload Image: <input type="file" name="imgfile"><br>
<font size="1">Click browse to upload a local file</font><br>
<br>
<input type="submit" value="Upload Image">
</form>
</body>
</html>
Re: Server Problem or Code Problem
Posted: Sat Apr 04, 2009 10:26 am
by ben.artiss
I'm not exactly sure on this (its been a bit since I did any upload work), but the part:
Code: Select all
//Get Image size info
list($width_orig, $height_orig, $image_type) = getimagesize($img);
switch ($image_type)
{
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
- do the cases definitely work? I.e. should they not be the name of the file type instead of numbers? I could well be wrong, but I know I accessed the file type from the $_FILES[] array. You did say it was working at some point though so I'm also a bit baffled sorry. Try outputting $image_type somewhere to see what you're getting maybe?
Re: Server Problem or Code Problem
Posted: Sat Apr 04, 2009 10:27 am
by The Omen
The code works now as long as the image is smaller than 50k.
The case statement is ok. The $image_type returns numbers for the type of image.
Re: Server Problem or Code Problem
Posted: Sat Apr 04, 2009 10:34 am
by ben.artiss
To answer your initial question I personally would put it down to server, but it appears you have a more extensive knowledge of the subject than myself and will find the answer way before I do! Keep hunting man, you'll find it. Sorry to waste your time

Re: Server Problem or Code Problem
Posted: Sat Apr 04, 2009 10:49 am
by The Omen
I actually think my problem may lie with the function:
getimagesize()
Need to find a way round using it.
Re: Server Problem or Code Problem
Posted: Sat Apr 04, 2009 11:29 am
by The Omen
Found a way of not using it and still get the problem.
Must be the server!!!