Question

I seem to be having a problem with a few lines of code in the sense that it works on my local server on xampp but when I upload over FTP to my domain it not longer works. I have attached the code snippet as well as the file it includes. What the domain does is just completely disregard the code and proceed as if it wasn't there. Any help is much appreciated in advance!

code snippet

<?php
include_once("colors.inc.php");
$ex=new GetMostCommonColors();
$ex->image="http://maps.googleapis.com/maps/api/staticmap?center=25.437484181498,-89.594942854837&zoom=50&size=600x300&maptype=roadmap&markers=color:blue&sensor=false";

$colors=$ex->Get_Color();
$how_many=12;
$colors_key=array_keys($colors);
?>

colors.inc.php

<?php

class GetMostCommonColors
{

    var $image;


    function Get_Color()
    {
        if (isset($this->image))
        {
            $PREVIEW_WIDTH    = 150;  /
            $PREVIEW_HEIGHT   = 150;
            $size = GetImageSize($this->image);
            $scale=1;
            if ($size[0]>0)
            $scale = min($PREVIEW_WIDTH/$size[0], $PREVIEW_HEIGHT/$size[1]);
            if ($scale < 1)
            {
                $width = floor($scale*$size[0]);
                $height = floor($scale*$size[1]);
            }
            else
            {
                $width = $size[0];
                $height = $size[1];
            }
            $image_resized = imagecreatetruecolor($width, $height);
            if ($size[2]==1)
            $image_orig=imagecreatefromgif($this->image);
            if ($size[2]==2)
            $image_orig=imagecreatefromjpeg($this->image);
            if ($size[2]==3)
            $image_orig=imagecreatefrompng($this->image);
            imagecopyresampled($image_resized, $image_orig, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); 
            $im = $image_resized;
            $imgWidth = imagesx($im);
            $imgHeight = imagesy($im);
            for ($y=0; $y < $imgHeight; $y++)
            {
                for ($x=0; $x < $imgWidth; $x++)
                {
                    $index = imagecolorat($im,$x,$y);
                    $Colors = imagecolorsforindex($im,$index);
                    $Colors['red']=intval((($Colors['red'])+15)/32)*32;    
                    $Colors['green']=intval((($Colors['green'])+15)/32)*32;
                    $Colors['blue']=intval((($Colors['blue'])+15)/32)*32;
                    if ($Colors['red']>=256)
                    $Colors['red']=240;
                    if ($Colors['green']>=256)
                    $Colors['green']=240;
                    if ($Colors['blue']>=256)
                    $Colors['blue']=240;
                    $hexarray[]=substr("0".dechex($Colors['red']),-2).substr("0".dechex($Colors['green']),-2).substr("0".dechex($Colors['blue']),-2);
                }
            }
            $hexarray=array_count_values($hexarray);
            natsort($hexarray);
            $hexarray=array_reverse($hexarray,true);
            return $hexarray;

        }
        else die("You must enter a filename! (\$image parameter)");
    }
}
?>
Was it helpful?

Solution

I agree with @Rodrigo Brun but would also encourage you to see if your hosting client has support for the GD library in PHP. You can check by creating a page in your directory with the contents <?php phpinfo(); ?> and see if it supports GD. Sometimes this is turned off and can be easily fixed by editing your php.ini file. I had a similar problem and this solved it so best of luck and hope this helped!

OTHER TIPS

Most web servers by default, not permit open files from url ( Remote files ), using basic functions, like, file_get_contents("http://...") or getimagesize("http://..."). This is a security problem, is not recommended to use this methods to get properties of remote images or files. Try the Curl library, it's more safe, and builded for this.

But in your local server , generally your php is enabled to open remote files, via basic functions.

So, you can modify your php.ini ( searching for "allow_url_fopen" , and setting the value to "1"), or, you can use direct in your php code (first line),

ini_set("allow_url_fopen",1);

this function is to "replace" the config in php.ini, but without change the original php.ini file , only in execution time.

Resuming: two answers to your question are avaible, CURL or INI_SET (allow_url_fopen).

Hope I have helped.

[EDITED] -----------

Hi, the following code adapted to your requirements, using curl library to get Width, height and Mime-type of image ( in a INTEGER and STRING values ).

Just add this 3 functions into your class "{}". after var $image

function getImageBytes(){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->image);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $info = curl_exec($ch);
    curl_close($ch);
    return $info;
}

function getSize(){
    $bytes = $this->getImageBytes();
    $img = imagecreatefromstring($bytes);

    $out = array();
    $out[0] = imagesx($img);
    $out[1] = imagesy($img);

    $f = finfo_open();
    $mime = finfo_buffer($f, $bytes, FILEINFO_MIME_TYPE);
    $out[2] = $this->getIntMimeType($mime);
    $out[3] = $mime;

    return $out;
}

function getIntMimeType($m){
    switch ($m) {
        case "image/gif":
            return 1;           
        case "image/jpeg":
            return 2;
        case "image/png":
            return 3;
        default:
            return 0;
    }
}

Now, for correct result, change this line

$size = GetImageSize($this->image);

To new function

$size = $this->getSize();

It's not necessary to pass parameters in new function. The $this->image variable it's used in "getImageBytes" function.

The function getIntMimeType($m) is for your conditions $size[2]==1... Just a translator of string to integer.

Value of index 3 in $size now is the mime-type in string (ex. image/png or image/gif ).

:)

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