Jump to content

Recommended Posts

I have a form that allows users to upload an image.  I am using GD to resize these to a more "appropriate" size after they are uploaded, and it is all working great.

 

Unfortunately, GD apparently cannot handle images over 1024px × 768px.

 

Is there a way that i can reject images that are over this dimension?  I know i could set the max_size_upload (not sure the exact wording of that...but this is besides the point) in my .htaccess file, but i am pretty sure that is actual filesize (in MB) rather than dimensions (in pixels).

 

Side note:  I also require that it is a .jpg file (just easier that way...at least for now)...so if the same script that rejects large images could also reject non jpg/jpeg files...that would be awesome.

 

I have Googled for this but have been unsuccessful thus-far...so i hope someone can get me pointed int eh right direction.  I already know how to use GD to get the image dimensions...but if GD crashes over 1024px × 768 then it doesn't do me much good using it to determine the dimensions...  Anyway, help is greatly appreciated.  :D

Would this do it?

 

<?php
$img_path = "/path/to/image.jpg";

$img = getimagesize($img_path);
list($height,$width) = $img;

if($height > 1024 || $width > 768)
{
echo "Your uploaded image is too large. The maximum allowed size is 1024x768 px. Your image was {$height}x{$width}px.";
unlink($img_path);
}
else if($img['mime']!='image/jpeg')
{
echo "The image must be a JPEG file.";
unlink($img_path);
}
else {
echo "Your image was within the allowed size and has been uploaded.";
}
?>

It seems that it would work, however isn't the getimagesize() function a part of the GD library...and thus won't that part still choke if the image is too large?...if not, then this is super easy...but i had avoided that function as i thought it would have the smae problem that the rest of GD apparently has over 1024px × 768px.

 

I will give it a try and see what happens with a very large image...i'll post a follow up in a couple minutes.

 

BTW:  I love your "most complex hello world implimentation ever" script in your signature!

okay...the script works...but i have a technical question...  I am checking this immediately upon form upload...so the actual file is not really stored yet on teh server in a permanent location.  Below is the first part of the code you supplied, bt look at my $img_path...it is working great for determining the dimensions (and filetype)...but i wonder if unlink() is the appropriate way to destroy this uploaded and unwanted file of if there is some other command that i should substitute in this place since the file is not "permanent" yet...

 

 
//CODE SNIPPET...SEE PREVIOUS POST FOR ENTIRE CODE

		//check that the size and filetype are ok
		$img_path = $_FILES['imageone']['tmp_name'];

		$img = getimagesize($img_path);
		list($height,$width) = $img;

		if($height > 1024 || $width > 768)
		{
			echo "Your uploaded image is too large. The maximum allowed size is 1024x768 px. Your image was {$height}x{$width}px.";
			unlink($img_path);
		}

// etc...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.