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!
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?
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.
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?
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.