2

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)");
    }
}
?>
6
  • Can you view the source on the live server? Can you see your PHP code? Commented Apr 26, 2014 at 16:42
  • include_once("colors.inc.php"); path is fine ? Commented Apr 26, 2014 at 16:44
  • When I click view source none of the php code comes up(I believe this is supposed to happen) but when I look at the uploaded file containing the code it shows up. Thanks again for your help! Commented Apr 26, 2014 at 16:45
  • @samitha, yes the path is fine. Would there be something wrong in running this code over the webserver, I didn't think there would be one? Thanks again for your help! Commented Apr 26, 2014 at 16:48
  • @user3288629 - OK. Can you put a file on the remote server that just contains <?php phpinfo(); ?>, and run that? Do you see anything? Commented Apr 26, 2014 at 16:53

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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 ).

:)

2 Comments

How would I change the code to reflect the CURL library like you said. I am new to php so as much help as you can give would be great. Again thanks for taking the time to answer my question!
See again my answer, i edited with one solution of implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.