Restict Upload size of images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
firefox
Forum Newbie
Posts: 6
Joined: Sat Jun 03, 2006 3:52 am

Restict Upload size of images

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
firefox
Forum Newbie
Posts: 6
Joined: Sat Jun 03, 2006 3:52 am

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
firefox
Forum Newbie
Posts: 6
Joined: Sat Jun 03, 2006 3:52 am

Post by firefox »

Hi,

Wow that seems simple enough, thank you for your help

All the best from Alan
Post Reply