Page 1 of 1
Read image from 3rd party site.
Posted: Wed Apr 23, 2008 3:24 am
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.

Re: Read image from 3rd party site.
Posted: Wed Apr 23, 2008 4:20 am
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.
Re: Read image from 3rd party site.
Posted: Wed Apr 23, 2008 5:06 am
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));
?
Re: Read image from 3rd party site.
Posted: Wed Apr 23, 2008 5:25 am
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.
Re: Read image from 3rd party site.
Posted: Wed Apr 23, 2008 5:35 am
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?
Re: Read image from 3rd party site.
Posted: Wed Apr 23, 2008 5:48 am
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.
Re: Read image from 3rd party site.
Posted: Wed Apr 23, 2008 6:17 am
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.