Read image from 3rd party site.

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
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Read image from 3rd party site.

Post by JellyFish »

Hey. I was wondering if the server restricts itself form loading images from other domains/websites and then reading them. And if not then how is this usually done?

How can I load, for example, Google's logo and read every pixel color in the image and output a string of RGB color values, with php?

Thanks for reading, 'gotta hit the sack now. :D
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Read image from 3rd party site.

Post by onion2k »

You can grab an image in the same way you'd fetch any other web based resource, eg fopen(), file_get_contents(), cURL, etc. Once the image is saved on your server you can open it with imagecreatefromjpeg() (or whatever format it is) and loop through the pixels using imagecolorat() to determine what they are.

Strictly speaking I believe imagecreatefromjpeg() can open the image from the external site if you have URL wrappers switched on, but that's probably a bad idea because you'd be fetching the image every time the script ran rather than only fetching it once then saving a local copy..

You should also realise that you'll be opening the image exactly as a browser would open it. If the site has anti-hotlinking code you won't magically get the right image. cURL can defeat hotlinking stuff though if you set the referrer properly.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Read image from 3rd party site.

Post by JellyFish »

I see.

Does the imagecopyresampled, imagesx and imagesy functions not work on resources created by imagecreatefromjpg? Because I'm getting an error saying:

Warning: imagesx(): supplied argument is not a valid Image resource in /home/www/eleven.freehostia.com/test.php on line 48

Warning: imagesy(): supplied argument is not a valid Image resource in /home/www/eleven.freehostia.com/test.php on line 48

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/www/eleven.freehostia.com/test.php on line 48
This is my code:

Code: Select all

 
$maxImgWidth = 128;
$maxImgHeight = 128;
 
$src = $_GET['url'] ? $_GET['url'] : "http://google.com/images/logo.gif";
            
$srcImg = @imagecreatefromgif($src) or @imagecreatefrompng($src) or @imagecreatefromjpeg($src) or die("Unable to create image from src.");
$desImg = imagecreatetruecolor($maxImgWidth, $maxImgHeight);
 
imagecopyresampled($desImg, $srcImg, 0, 0, 0, 0, 128, 128, imagesx($srcImg), imagesy($srcImg));
 
 
?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Read image from 3rd party site.

Post by onion2k »

You're using @ to suppress errors. If you don't want to see the errors I can only assume you're happy with the code not working.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Read image from 3rd party site.

Post by JellyFish »

onion2k wrote:You're using @ to suppress errors.
Yes, this is because I don't want to see errors. Rather what I'd expect is for each function (imagecreatefromgif, imagecreatefrompng and imagecreatefromjpeg) to be 'tried' and if all else fails, die. But I do think that this line is the problem (I know what your saying, doh!).

I guess you can't just do this.

So my question is: how would I use the correct function for the image if I won't be able to know the image's file type?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Read image from 3rd party site.

Post by onion2k »

JellyFish wrote:
onion2k wrote:You're using @ to suppress errors.
Yes, this is because I don't want to see errors.
You do want to see the errors though. Otherwise you're going to have a very hard time fixing the script.
Rather what I'd expect is for each function (imagecreatefromgif, imagecreatefrompng and imagecreatefromjpeg) to be 'tried' and if all else fails, die. But I do think that this line is the problem (I know what your saying, doh!).
Which is what'll be happening. One of them is loading the image, but not correctly. Consequently the subsequently image functions are failing.
I guess you can't just do this.

So my question is: how would I use the correct function for the image if I won't be able to know the image's file type?
Find out what sort of image the file is before you try to open it. getimagesize() will tell you.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Read image from 3rd party site.

Post by JellyFish »

Thanks I think I have gotten what I was looking for now. I'm using getimagesize to get the file type then using a switch statement.

I don't know if I'll come across anymore errors if I do I'll post something.

Thanks again. Cheers.
Post Reply