Question

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"));

?>
Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top