2

I am having some trouble with a few lines of PHP code when I upload it to my domain over ftp. The lines below work on my local server on XAMPP but when I upload it to the remote server through my hosting client, instead of echoing out what it is supposed to, I get a blank page with no error log or message displayed. Any help would be greatly appreciated and thank you for taking the time to answer my question.

Code that doesn't work:

<?php

function getDominantColour($image, $type='png')
{


    eval('$source = imagecreatefrom' . $type . '("' . $image . '");'); 
    $source_x = imagesx($source); 
    $source_y = imagesy($source); 
    $resized_x = 100; 
    $resized_y = 100; 
    $resized= imagecreatetruecolor($resized_x, $resized_y); 
    imagecopyresampled($resized, $source, 0, 0, 0, 0, $resized_x, $resized_y, $source_x, $source_y);


    $colours = array();
    $rgb = '';
    $index = array();
    for ($x=0; $x<100; $x++) 
    {
        for ($y=0; $y<100; $y++)
        {
            $rgb = imagecolorat($resized, $x, $y); 
            $index = imagecolorsforindex($resized, $rgb); 
            $key = 'R' . $index['red'] . 'G' . $index['green'] . 'B' . $index['blue']; 
            if (empty($colours[$key])) 
            {
                $colours[$key] = 1;
            } else { 
                $colours[$key]++;
            }
        }
    }
    arsort($colours, SORT_NUMERIC);
    return key($colours);
}


echo(getDominantColour("http://maps.googleapis.com/maps/api/staticmap?center=40.727404,-74.020862&zoom=50&size=600x300&maptype=roadmap&markers=color:blue&sensor=false"));

?>
1
  • And when you turn on error reporting, what happens then Commented Apr 27, 2014 at 18:47

2 Answers 2

2

eval is evil ( not always ). But in your example i would try to avoid it.

In your code you could use call_user_func instead:

$source = call_user_func( 'imagecreatefrom' . $type, $image );

As if on this question explained:

  1. Sometimes eval is the only/the right solution.
  2. For most cases one should try something else.
  3. If unsure, goto 2.
  4. Else, be very, very careful.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for your help. I changed it so that eval is no longer used but I also learned that the hosting client I have did not have support for the GD library turned on. These two put together fixed the problem. Thanks so much again for your time and help!
1

eval is banned from lots of servers as a security precaution. Rewrite your code so that you don't use that function.

5 Comments

Really ? eval function is disabled? been with a ton of hosts, never seen that one. Ahh, I see tons of Free Web Hosts disable this function now, interesting. Thanks.
How would you recommend that I change the code so that the eval function is not used. Thanks again for your time and help!
@user2484406 can't you just use a switch to decide which imagecreatefrom function to call depending on the type?
I changed that since the image will always be a png but it still doesn't work. Is there a specific command or function that wont work on a remote server or hosting client that I used? Thanks for your help!
Put error_reporting(E_ALL); on top of your script, see what error it gives you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.