首頁 >後端開發 >php教程 >gd - php图片旋转问题

gd - php图片旋转问题

WBOY
WBOY原創
2016-06-06 20:41:351862瀏覽

<code>$fullFileName = $uploadImgDir."/".$filename;
    $thumbFileName = $filename.".thumb";
    if ($type =  pathinfo($fullFileName, PATHINFO_EXTENSION)) {
        switch ($type) {
            case 'jpg';
            $isJpeg = true;
            break;
            case 'gif';
            $isGif = true;
            break;
            case 'png';
            $isPng = true;
            break;
            default:
                return false;
        }
        // Load
        if(isset($isGif) && $isGif){
            $source = imagecreatefromgif($fullFileName);
            $rotate = imagerotate($source, $degrees, 0);
            imagegif($rotate,$fullFileName,100);
            return true;
        }elseif(isset($isJpeg) && $isJpeg){
            $source = imagecreatefromjpeg($fullFileName);
            $rotate = imagerotate($source, $degrees, 0);
            imagejpeg($rotate,$fullFileName,100);
            return true;
        }elseif(isset($isPng) && $isPng){
            //这个地方报错了
            **$source = imagecreatefrompng($fullFileName);
            $rotate = imagerotate($source, $degrees, 0);
            imagepng($rotate,$fullFileName,100);**
            return true;
        }else{
            return false;
        }
</code>

jgp的旋转没问题,png 报错了
imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data
求解决
或者求png图片旋转的好方法

回复内容:

<code>$fullFileName = $uploadImgDir."/".$filename;
    $thumbFileName = $filename.".thumb";
    if ($type =  pathinfo($fullFileName, PATHINFO_EXTENSION)) {
        switch ($type) {
            case 'jpg';
            $isJpeg = true;
            break;
            case 'gif';
            $isGif = true;
            break;
            case 'png';
            $isPng = true;
            break;
            default:
                return false;
        }
        // Load
        if(isset($isGif) && $isGif){
            $source = imagecreatefromgif($fullFileName);
            $rotate = imagerotate($source, $degrees, 0);
            imagegif($rotate,$fullFileName,100);
            return true;
        }elseif(isset($isJpeg) && $isJpeg){
            $source = imagecreatefromjpeg($fullFileName);
            $rotate = imagerotate($source, $degrees, 0);
            imagejpeg($rotate,$fullFileName,100);
            return true;
        }elseif(isset($isPng) && $isPng){
            //这个地方报错了
            **$source = imagecreatefrompng($fullFileName);
            $rotate = imagerotate($source, $degrees, 0);
            imagepng($rotate,$fullFileName,100);**
            return true;
        }else{
            return false;
        }
</code>

jgp的旋转没问题,png 报错了
imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data
求解决
或者求png图片旋转的好方法

imagepng($rotate,$fullFileName,100);的问题
把第三个参数去掉就行了。不知道具体原因。
第三个参数是保真度,png为什么不能100%保真。

可以参考 OSC链接

路由规则可以根据自己需求重写~~~~~~~~~~

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn