Page 1 of 1

Restict Upload size of images

Posted: Sat Jun 03, 2006 3:55 am
by firefox
Good Morning Team,

I have a problem in resticting the upload size for images, can anuone give any help please....
This is what I have so far for uploading images...

function ResizeConserveAspectRatio($source, $destination = NULL, $long_dimension = 100)
{
$w = $long_dimension;
$h = $long_dimension;
$source = @imagecreatefromstring(
@file_get_contents($source))
or die('Not a valid image format.');
$x = imagesx($source);
$y = imagesy($source);
if($w && ($x < $y)) $w = round(($h / $y) * $x); // maintain aspect ratio
else $h = round(($w / $x) * $y); // maintain aspect ratio
$slate = @imagecreatetruecolor($w, $h) or die('Invalid image dimmensions');
imagecopyresampled($slate, $source, 0, 0, 0, 0, $w, $h, $x, $y);
if(!$destination) header('Content-type: image/jpg');
@imagejpeg($slate, $destination, 100) or die('Directory permission problem');
imagedestroy($slate);
imagedestroy($source);
if(!$destination) exit; // return details about the thumbnail
return array($w,$h,'width="'.$w.'" height="'.$h.'"','mime'=>'image/jpg');
}

if (isset($_POST["op"]) && ($_POST["op"]=="photo"))
{
$fieldname = 'p_photo'; # as per your form
$PhotoNameTimeStamp = date("jnyHis");
$uploads_dir = 'uploads/';
$thumbs_dir = 'uploads/thumbs/';

$upload_size = 500; #pixels
$thumb_size = 75; # pixels

$filename = "" . $PhotoNameTimeStamp . ".jpg";

$thumb = $PhotoNameTimeStamp.'_t.jpg';
ResizeConserveAspectRatio($p_photo, $uploads_dir.$filename, $upload_size);
ResizeConserveAspectRatio($p_photo, $thumbs_dir.$thumb, $thumb_size);


echo "<img src=\"$uploads_dir/$filename\"><img src=\"$thumbs_dir/$thumb\">";
exit();

Best regards from Alan

Posted: Sat Jun 03, 2006 4:52 am
by s.dot
Please tell us what you're expecting, what you think the script does, what errors its producing, and if it's effective in doing what you expect it to do.

From a quick glance, removing the @ signs could help with error reporting.

Posted: Sat Jun 03, 2006 5:04 am
by firefox
Hi,

The form part is not there, but basicaly, you upload an image it resizes the image using GD and maintains the asspect ratio, gives a new mname to the file, and stores a large image and thumbnail in the resprctive uploads directory...and it works, all I want to do is to make sure the uploaded file is not over 70k in size...

I hope this helps...

Best regards from Alan

Posted: Sat Jun 03, 2006 5:08 am
by s.dot
You could use the filesize function

Code: Select all

$maxSize = 70*1024;  //70 kb, i believe

if(filesize($locationOfImage > $maxSize)){
   
   //  alert the end user?  delete the image?  something in here 

} else {

   //  file is less than or equal to 70kb

}
hope that's what you're looking for =]

[edit]You could do this before you store the file on your system with getting the filesize of $_FILES['form']['tmp_name'] or afterwards by getting the filesize of the image on your server.

Posted: Sat Jun 03, 2006 5:19 am
by firefox
Hi,

Wow that seems simple enough, thank you for your help

All the best from Alan