0

我需要为此函数添加一些错误检查,以确保 imagegif() 和 imagecolorset() 函数成功。失败的常见原因是 spot2.gif 文件不可写的权限问题。

但是,当我将 spot2.gif 更改为只读时,应该触发“其他”条件的条件,回显的 javascript 警报不会触发。

function set_theme_color_spot2($hex)
{
    $info = hexToRGB($hex);
    $token = "../wp-content/themes/".get_option('template')."/styles/".get_option('myTheme')."/spot2.gif";
    $token2 = "../wp-content/themes/".get_option('template')."/styles/".get_option('myTheme')."/spot2-template.gif";
    if (file_exists($token2) && is_writable($token)) 
    {
        $img = imagecreatefromgif("../wp-content/themes/".get_option('template')."/styles/".get_option('myTheme')."/spot2-template.gif");
        $color = imagecolorallocate($img, $info["red"], $info["green"], $info["blue"]);
        imagecolorset($img, 0, $info["red"], $info["green"], $info["blue"]);
        imagegif($img, $token);
        $themecolor = get_option('myTheme').'_color4';
        update_option($themecolor, $hex);
    }
    else
    {   
        echo "<script type='text/javascript'>alert('The template colors could not be changed because your current file permissions are too restrictive. Make sure the files in the styles directory are set to 644');</script>";
    }
}
4

2 回答 2

0

为什么不在您的 spot2.gif 上调用“is_writable”,然后再尝试访问它?

http://php.net/manual/de/function.is-writable.php

于 2010-10-14T16:20:19.327 回答
0

如果你有一个有效的图像句柄,那么imagecolorset()应该不会失败。它只是在某处的表格中设置一些值。另一方面,imagegif()可以并且将会失败,因为它必须处理文件系统。不幸的是,它只返回真/假,而不是任何有用的诊断细节(如果因为磁盘空间不足、权限被拒绝写入文件、权限被拒绝访问目录而知道它是否失败会很有用,没有这样的文件/目录等...)。

因此,只需执行以下操作:

if (!imagegif($handle, 'testfile.gif')) {
    die("Unable to write gif out");
}

包含您希望保存的任何诊断信息。您也可以尝试使用error_get_last(),但不能保证 GD 会在失败时填充任何有用的东西。

于 2010-10-14T16:30:43.250 回答