帮助为该函数添加错误检查

发布于 2024-09-27 11:16:45 字数 1250 浏览 2 评论 0原文

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

但是,当我将 spot2.gif 更改为只读(应触发“else”条件)的条件时,回显的 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>";
    }
}

I need to add some error checking to this function to make sure that the imagegif() and imagecolorset() functions are successful. The usual cause for failure is a permissions issue where the spot2.gif file is not writable.

However, when I change the spot2.gif to read only, a condition that should trigger the "else" conditions, the echo'd javascript alert is not firing.

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>";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥有 2024-10-04 11:16:46

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

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

Why not call "is_writable" on your spot2.gif before trying to access it?

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

九歌凝 2024-10-04 11:16:46

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

所以,就这样做:

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

使用您想要保存的任何诊断信息。您也可以尝试使用 error_get_last(),但不能保证 GD 会填充该内容任何对失败有用的东西。

If you have a valid image handle, then imagecolorset() should not be able to fail. It's just setting some values in a table somewhere. On the other hand, imagegif() can and will fail, as it has to deal with the file system. Unfortunately, it only returns true/false, and not any diagnostic detail which would be useful (it would to be useful to know if it failed because you're out of disk space, permission denied writing to the file, permission denied accessing the directory, no such file/dir, etc...).

So, just do:

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

with whatever diagnostic information you'd care to have saved. You could also try using error_get_last(), but there's no guarantee that GD will populate that with anything useful on failure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文