简体   繁体   English

PHP代码在远程服务器上不起作用

[英]PHP Code not Working on Remote Server

I am having some trouble with a few lines of PHP code when I upload it to my domain over ftp. 通过ftp上传到我的域时,使用几行PH​​P代码遇到了麻烦。 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. 下面的行在XAMPP上的本地服务器上运行,但是当我通过托管客户端将其上载到远程服务器时,没有回显它应有的功能,而是得到了一个空白页,没有错误日志或消息显示。 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"));

?>

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: 在您的代码中,您可以改用call_user_func

$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. 如果不确定,请转到2。
  4. Else, be very, very careful. 否则,请非常非常小心。

eval is banned from lots of servers as a security precaution. 为了安全起见,许多服务器禁止使用eval。 Rewrite your code so that you don't use that function. 重写您的代码,以便您不使用该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM