imagex/imagesy with $_FILE??

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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

imagex/imagesy with $_FILE??

Post by psychotomus »

I am having trouble getting the imagesx and imagesy with an image from memory.

always returning true no matter what... ;) any work-a-round. I don't feel like uploading the image then deleting if its to big.

Code: Select all

if(imagesx($_FILES["file"]["tmp_name"]) > AVATAR_MAX_WIDTH || imagesy($_FILES["file"]["tmp_name"]) > AVATAR_MAX_HEIGHT)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: imagex/imagesy with $_FILE??

Post by McInfo »

psychotomus wrote:I don't feel like uploading the image then deleting if [it's too] big.
By the time $_FILES is populated, the file has already been uploaded.

Notice that these functions expect an image resource, not a file name string.
PHP Manual wrote:int imagesx ( resource $image )
int imagesy ( resource $image )
If the dimensions of the image matter, do some research to find a reasonable maximum file size for an image with the format and number of pixels you expect. To validate an uploaded image, first test the file to confirm that it does not exceed the maximum file size. Then create an image resource from the file and test the dimensions.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: imagex/imagesy with $_FILE??

Post by McInfo »

I was just reminded of getimagesize(), which is preferable to creating an image resource in this situation.
Post Reply