tags:

views:

29

answers:

2

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

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

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

Select0r
That makes perfect sense to me. I've updated my question and added the is_writable check in the conditional block. However, its still executing even when spot2.gif is manually set to read only...Any ideas?
Scott B
A: 

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.

Marc B