Morning all, creating an ajax upload. problem being i want to error out when image is too large.
Here's my code
PHP Code:
<?php
if(isset($_FILES) && count($_FILES)!=0)
{
$directory = "./";
$maxWidth = 2500;
if(!file_exists($directory))
{
mkdir($directory, 0700); // create directory if not exists
}
if(!empty($_FILES['fileToUpload']['tmp_name'])) // if file gets posted to iframe
{
if(preg_match("#^image/((?:gif)|(?:jpg)|(?:jpeg)|(?:png))$#is", $_FILES['fileToUpload']['type'], $match)) // check for valid files
{
$extention = $match[1];
if(!empty($_POST['fileToUploadName'])) // if user has entered new file name
$filename = $directory.$_POST['fileToUploadName'].".".$extention;
else
$filename = $directory.$_FILES['fileToUpload']['name'].".".$extention;
list($width,$height)=getimagesize($_FILES['fileToUpload']['tmp_name']); // get height and width
if(!empty($_POST['width']) && !empty($_POST['height']))
{
if(preg_match("#^[0-9]*$#is", trim($_POST['width'])) && preg_match("#^[0-9]*$#is", trim($_POST['width']))) // namke sure input is numerical
{
if($_POST['height'] > $height || $_POST['width'] > $width)
{
die("Entering height or width values that are larger than the actual image will result in streched and poor quality images. Please alter values.");
}
$newWidth = $_POST['width'];
$newHeight = $_POST['height'];
}
else
die("Please enter a valid input for height and width");
}
elseif(!empty($_POST['width']) && empty($_POST['height']))
die("please enter a height if you would like to rezize image");
elseif(!empty($_POST['height']) && empty($_POST['width']))
die("please enter a width if you would like to rezize image");
elseif($width >= $maxWidth || $height >= $maxWidth)
{
$larger = max($width, $height);
$percent = floor($maxWidth/floor($larger/100));
$newHeight = (floor($height/100)*$percent);
$newWidth = (floor($width/100)*$percent);
}
// upload file
if(!file_exists($directory.$filename))
{
$uploadedfile = $_FILES['fileToUpload']['tmp_name'];
if($extention=="gif")
{
if(!$src = @imagecreatefromgif($uploadedfile))
{
die("Error: Could not create image, image too large");
}
}
elseif($extention=="png")
{
if(!$src = @imagecreatefrompng($uploadedfile))
{
die("Error: Could not create image, image too large");
}
}
else
{
if(!$src = @imagecreatefromjpeg($uploadedfile))
{
die("Error: Could not create image, image too large");
}
}
$tmp=imagecreatetruecolor($newWidth,$newHeight);
imagecopyresampled($tmp,$src,0,0,0,0,$newWidth,$newHeight,$width,$height);
if($extention=="gif")
{
if(imagegif($tmp,$filename,100))
{
imagedestroy($src);
imagedestroy($tmp);
die("upload complete"); // gif upload complete
}
else
{
die("upload failed"); // gif upload failed
}
}
elseif($extention=="png")
{
if(imagepng($tmp,$filename,9))
{
imagedestroy($src);
imagedestroy($tmp);
die("upload complete"); // png upload complete
}
else
{
die("upload failed"); // png upload failed
}
}
else
{
if(imagejpeg($tmp,$filename,100))
{
imagedestroy($src);
imagedestroy($tmp);
die("upload complete"); // jpg upload complete
}
else
{
die("upload failed"); // jpg upload failed
}
}
}
else
{
die("Error: Filename already exists");
}
}
else
{
die("Error: Please upload only valid image files (.jpg, .gif and .png)");
}
}
else
{
die("Error: Please enter file to upload");
}
}
?>
The steps i've took to error is:
PHP Code:
if($extention=="gif")
{
if(!$src = @imagecreatefromgif($uploadedfile))
{
die("Error: Could not create image, image too large");
}
}
elseif($extention=="png")
{
if(!$src = @imagecreatefrompng($uploadedfile))
{
die("Error: Could not create image, image too large");
}
}
else
{
if(!$src = @imagecreatefromjpeg($uploadedfile))
{
die("Error: Could not create image, image too large");
}
}
but just dies without error display.
Also does anyone know how using the GD lib i can reduce the size and maybe the quality to push the upload when the file is too large?