techtheatre Posted May 9, 2007 Share Posted May 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/50715-solved-reject-image-upload-too-large-dimension-not-filesize/ Share on other sites More sharing options...
Daniel0 Posted May 9, 2007 Share Posted May 9, 2007 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."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50715-solved-reject-image-upload-too-large-dimension-not-filesize/#findComment-249348 Share on other sites More sharing options...
techtheatre Posted May 12, 2007 Author Share Posted May 12, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/50715-solved-reject-image-upload-too-large-dimension-not-filesize/#findComment-251085 Share on other sites More sharing options...
techtheatre Posted May 12, 2007 Author Share Posted May 12, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/50715-solved-reject-image-upload-too-large-dimension-not-filesize/#findComment-251087 Share on other sites More sharing options...
Daniel0 Posted May 12, 2007 Share Posted May 12, 2007 Unlink would delete it, but since it gets stored as a temporary file on the server it would get deleted eventually anyways. It's fine to delete it though. Quote Link to comment https://forums.phpfreaks.com/topic/50715-solved-reject-image-upload-too-large-dimension-not-filesize/#findComment-251127 Share on other sites More sharing options...
techtheatre Posted May 12, 2007 Author Share Posted May 12, 2007 Okay. i am happier deleting it anyway...just to keep the clutter away... Thank you VERY much for your help on this. It will make the image filtering process work much better. Quote Link to comment https://forums.phpfreaks.com/topic/50715-solved-reject-image-upload-too-large-dimension-not-filesize/#findComment-251134 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.