Imagepng function not quite working

2019-08-25 02:12发布

问题:

I get the following error message when from a script I'm using to create a thumbnail

Warning: imagepng() [function.imagepng]: Unable to open 'Manga/One Piece/asd/Thumbnail' for writing: Is a directory in /homez.380/mangasma/www/mangaUpload.php on line 220

thats the line causing the error

imagepng($dst_img,$dir);

and here is the method

function createthumb($source,$output,$new_w,$new_h)
{
    $dirpos=strrpos($output,"/");
    $dir= substr($output,0,$dirpos);
    if(!file_exists($dir))
    {
        mkdir($dir);
        chmod($dir,0775);
    }else
    {
        chmod($dir,0775);
    }
    $ext=explode(".",$source);
    $ext=$ext[count($ext)-1];
    $ext=strtolower($ext);

if (preg_match("/jpg|jpeg/",$ext)){$src_img=imagecreatefromjpeg($source);}
    if (preg_match("/png/",$ext)){$src_img=imagecreatefrompng($source);}
    if (preg_match("/gif/",$ext)){$src_img=imagecreatefromgif($source);}

    echo $src_img;

    $old_x=imageSX($src_img);
    $old_y=imageSY($src_img);
    if ($old_x > $old_y) 
    {
        $thumb_w=$new_w;
        $thumb_h=$old_y*($new_h/$old_x);
    }
if ($old_x < $old_y) 
    {
        $thumb_w=$old_x*($new_w/$old_y);
        $thumb_h=$new_h;
    }
    if ($old_x == $old_y) 
    {
        $thumb_w=$new_w;
        $thumb_h=$new_h;
    }
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

    if (preg_match("/png/",$ext))
    {   
        imagepng($dst_img,$dir);
    }
elseif (preg_match("/gif/",$ext))
    {
        imagegif($dst_img,$dir); 
    } elseif (preg_match("/jpg|jpeg/",$ext)) {
        imagejpeg($dst_img,$dir); 
    }
    imagedestroy($dst_img); 
    imagedestroy($src_img);
    return array("w"=>$thumb_w,"h"=>$thumb_h);
}

回答1:

$dir shouldn't be a folder but a path to a file (ie: $dir = 'Manga/One Piece/asd/Thumbnail/1.png'; )

For example, just before imagepng($dst_img,$dir); (and imagegif and imagejpeg), you can add a line :

$dir .= '/test.png';

As you're using a function, you can/should pass the output filename as a parameter



标签: php gd