1

So I downloaded and edited a script off the internet to pull an image and find out the hex values it contains and their percentages:

The script is here:

<?php
$delta = 5;
$reduce_brightness = true;
$reduce_gradients = true;
$num_results = 5;

include_once("colors.inc.php");
$ex=new GetMostCommonColors();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Colour Verification</title>

</head>
<body>
    <div id="wrap">
    <img src="http://www.image.come/image.png" alt="test image" />
    <?php
        $colors=$ex->Get_Color("http://www.image.come/image.png", $num_results, $reduce_brightness, $reduce_gradients, $delta);
        $success = true;
        foreach ( $colors as $hex => $count ) {
            if ($hex !== 'e6af23') {$success = false; }
        if ($hex == 'e6af23' && $count > 0.05) {$success = true; break;}
        }

        if ($success == true) { echo "This is the correct colour. Success!"; } else { echo "This is NOT the correct colour. Failure!"; }


?>
    </div>

</body>
</html>

Here is a pastebin link to the file colors.inc.php

http://pastebin.com/phUe5Pad

Now the script works absolutely fine if I use an image that is on the server, eg use /image.png in the Get_Color function. However, if I try and use an image from another website including a http://www.site.com/image.png then the script no longer works and this error appears:

Warning: Invalid argument supplied for foreach() in ... on line 22

Is anyone able to see a way that I would be able to hotlink to images because this was the whole point of using the script!

Tenatious
  • 849
  • 4
  • 12
  • 32
  • Can't you just download the image first? – PeeHaa Aug 19 '12 at 15:13
  • The point is that it dynamically pulls an image from a server depending on a username entered and then checks that pulled image against the colours to see if it contains a certain one. Is there a way to make those grabbed images download in to a folder that I could then link to? – Tenatious Aug 19 '12 at 15:14

2 Answers2

1

You must download a file to the server and pass its full filename to the method Get_Color($img) as $img parameter.

So, you need to investigate another SO question: Download File to server from URL

Community
  • 1
  • 1
Vladimir Posvistelik
  • 3,843
  • 24
  • 28
  • I'm not sure if this will work in my case. The image I am attempting to save doesn't have a file extension because it's a generated image. Example: http://www.habbo.com/habbo-imaging/avatarimage?user=xxmattgxx&action=wav&direction=34&head_direction=34&gesture=sml&size=l.png So I can get the script to save a normal.png image to a folder, so that's working but how can I go about saving the above image as a .png? – Tenatious Aug 19 '12 at 15:55
  • Ended up going with this method of downloading each image to the server :) – Tenatious Aug 20 '12 at 12:43
0

The error indicates that the value returned by Get_Color is not a valid object that can be iterated on, probably not a collection. You need to know how the Get_Color works internally and what is returned when it doesn't get what it wants.

In the mean-time, you can download [with PHP] the image from the external url into your site, and into the required folder and read the image from there.

$image = "http://www.image.come/image.png";
download($image, 'folderName'); //your custom function
dnld_image_name = getNameOfImage();

$colors=$ex->Get_Color("/foldername/dnld_image_name.png");

By the way, did you confirm that the image url was correct?

codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • How can I go about using this for a file such as this: http://www.habbo.com/habbo-imaging/avatarimage?user=xxmattgxx&action=wav&direction=34&head_direction=34&gesture=sml&size=l where I need to save the image that is generated on that page. – Tenatious Aug 19 '12 at 16:06
  • here i think your response is binary so you will have to get the content as binary and save to file.u `fopen` and other file api should be able to help – codingbiz Aug 19 '12 at 22:30