2014-04-26 127 views
2

我似乎遇到了几行代码的问题,因为它在xampp上的本地服务器上工作,但是当我通过FTP上传到我的域时,它不再作品。我附上了代码片段以及它包含的文件。域名所做的只是完全忽视了代码,并继续进行,就好像它不在那里一样。任何帮助提前非常感谢!PHP代码在本地服务器上工作,但在域上不工作

代码片段

<?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)"); 
    } 
} 
?> 
+0

你可以在实时服务器上查看源代码吗?你能看到你的PHP代码? – andrewsi

+0

include_once(“colors.inc.php”);路径很好? – underscore

+0

当我点击查看源代码时,没有任何php代码出现(我相信这应该会发生),但是当我查看包含代码的上传文件时,它显示出来了。再次感谢你的帮助! – user3288629

回答

2

我同意@Rodrigo布伦而且会鼓励你看看你的托管客户端在PHP GD库支持。您可以通过在目录中创建一个页面,其内容为<?php phpinfo(); ?>并查看它是否支持GD。有时会关闭此功能,并可以通过编辑php.ini文件轻松修复。我有一个类似的问题,这解决了它如此好运,希望这有助于!

1

大多数网页默认的服务器,不允许从URL打开文件(远程文件),使用基本功能,如,的file_get_contents( “http:// ...”)或getimagesize(“http:// ...”)。这是一个安全问题,不建议使用此方法来获取远程图像或文件的属性。尝试卷曲库,它更安全,并为此建立。

但是在你的本地服务器上,通常你的php可以通过基本功能打开远程文件。

所以,你可以修改你的php.ini(搜索“allow_url_fopen选项”,并将其值设置为“1”),或者,你可以用在你的PHP代码(第一行)直接,

ini_set("allow_url_fopen",1); 

这个功能是“替换”php.ini中的配置,但不改变原始的php.ini文件,只在执行时。

恢复:您的问题的两个答案是可用的,CURL或INI_SET(allow_url_fopen)。

希望我帮上忙了。

将帖子-----------

嗨,下面的代码适用于您的需求,使用curl库得到宽度,高度和MIME类型的图像(在INTEGER和STRING值)。

只需将这3个功能添加到您的班级“{}”中即可。 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; 
    } 
} 

现在后,对于正确的结果,改变这一行

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

要新功能

$size = $this->getSize(); 

这是没有必要通过新的功能参数。它用在“getImageBytes”函数中的$this->image变量。

函数getIntMimeType($m)是为你的条件$size[2]==1 ...只是一个字符串转换为整数。

$size中索引3的值现在是字符串中的mime类型(例如image/png或image/gif)。

:)

+0

如何更改代码以反映您所说的CURL库。我是PHP新手,尽可能多的帮助,因为你可以给予很大的帮助。再次感谢您花时间回答我的问题! – user3288629

+0

再次看到我的答案,我编辑了一个解决方案的实现。 –

相关问题