Verify an Image

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
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Verify an Image

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Verify an Image

Post 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.
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Verify an Image

Post by cdoyle »

oh wow, that's all I have to do!
That's great!

Thank you for your help!
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Verify an Image

Post 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?
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Verify an Image

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Verify an Image

Post 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.
Post Reply