Home >Backend Development >PHP Tutorial >gd - php图片旋转问题

gd - php图片旋转问题

WBOY
WBOYOriginal
2016-06-06 20:41:351858browse

<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链接

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn