Page 1 of 1

Verify an Image

Posted: Thu Mar 12, 2009 7:17 pm
by cdoyle
Hi,

I'm trying to make it so my users can have an avatar, they aren't going to be uploading the image to my server. They'll just link to an image on their own server.

But I still want to verify that the URL they put in the text field is actually an image.
I've been searching and I believe I need to use getimagesize() to get the image type???
Is that correct?

So if I were to get the image from the URL the user supplied would it be something like this?

Code: Select all

 
if(isset($_POST['change']))
{
$change = getimagesize($_POST['change']);
}
 
I've been having a hard time understanding what I need to do next, to actually verify that the URL points to an image.
I've been reading this page, but most of it doesn't make any sense
http://us.php.net/manual/en/function.im ... ension.php

Thanks
Chris.

Re: Verify an Image

Posted: Thu Mar 12, 2009 7:23 pm
by requinix
Actually, all you need to do is check that $change isn't null. getimagesize will return an array for any image it can recognize: GIF, JPEG, PNG... and return null for any it doesn't.

Some forums will check that there's a .gif, .jp(e)g, or .png in the URL provided. Personally I don't like that practice: it's easy to circumvent and is merely a hindrance.

Re: Verify an Image

Posted: Thu Mar 12, 2009 7:28 pm
by cdoyle
oh wow, that's all I have to do!
That's great!

Thank you for your help!

Re: Verify an Image

Posted: Thu Mar 12, 2009 8:08 pm
by cdoyle
So since it brings back an array.

Would I do something like this, to enter the URL into the db?

Code: Select all

 
    case "change":
  if(isset($_POST['change']))
      {
          $change = getimagesize($_POST['change']);
         if ($change == null)
         {
             echo "Please enter a URL to a valid image type";
         }
         else
         {
            
          $updateavatar = $db->execute("UPDATE `players` SET `avatar`=? WHERE `id`=?", array($_POST['change'], $player->id));
         }
 
Or am I going about this wrong?

Re: Verify an Image

Posted: Thu Mar 12, 2009 8:31 pm
by cdoyle
The method I posted below works kinda, if it's a valid URL to an image it updates the db fine.

but if I put an invalid URL, and it returns a null. I get this message.

Warning: getimagesize(wrongtype.doc) [function.getimagesize]: failed to open stream: No such file or directory in /home/caraudi/public_html/CAC_Mafia_Test_Site/avatar.php on line 20

why does it do that?

Re: Verify an Image

Posted: Thu Mar 12, 2009 11:38 pm
by requinix
Probably because it failed to open a stream since there was no such file or directory.

This is one of those (very few) times when I would use error suppression.