dongmou1964 2017-04-10 08:56
浏览 166
已采纳

GD PHP旋转图像黑色边框

I try to resize and rotate an image with PHP (GD) but when image rotate, it add a black border on right.

Exemple image (before resize & rotate): Exemple

Exemple image (after resize & rotate): Exemple of black border add

Here is my code :

    $image = $_FILES["file"]["name"];
    $uploadedfile = $_FILES['file']['tmp_name'];

    if ($image) {

        $filename = stripslashes($_FILES['file']['name']);

        $i = strrpos($filename,".");
        $l = strlen($filename) - $i;
        $ext = substr($filename,$i+1,$l);

        $extension = strtolower($ext);

        if (($extension != "jpg") && ($extension != "jpeg") 
            && ($extension != "png") && ($extension != "gif")) {

            echo ' Unknown Image extension ';
            $errors=1;
        }

        else {
            $size=filesize($_FILES['file']['tmp_name']);

            if($extension=="jpg" || $extension=="jpeg" ) {
                $uploadedfile = $_FILES['file']['tmp_name'];
                $src = imagecreatefromjpeg($uploadedfile);
            }
            else if($extension=="png") {
                $uploadedfile = $_FILES['file']['tmp_name'];
                $src = imagecreatefrompng($uploadedfile);
            }
            else  {
                $src = imagecreatefromgif($uploadedfile);
            }

            $max_width = 175;
            $max_height = 100;

            $size=GetImageSize($uploadedfile);

            $width_ratio = ($size[0] / $max_width);
            $height_ratio = ($size[1] / $max_height);

            if($width_ratio >=$height_ratio) 
            {
                $ratio = $width_ratio;
            }
            else
            {
                $ratio = $height_ratio;
            }

            $new_width = ($size[0] / $ratio);
            $new_height = ($size[1] / $ratio);
            $tmp=imagecreatetruecolor($new_width,$new_height);

            if(function_exists("exif_read_data")){
                $exif = @exif_read_data($uploadedfile);

                $rotateImg = imagerotate($src,0,0);

                if(!empty($exif['Orientation'])) {
                    switch($exif['Orientation']) {
                    case 8:
                        $rotateImg = imagerotate($src,90,0);
                        break;
                    case 3:
                        $rotateImg = imagerotate($src,180,0);
                        break;
                    case 6:
                        $rotateImg = imagerotate($src,-90,0);
                        break;
                    } 
                }
            }

            imagecopyresampled($tmp,$rotateImg,0,0,0,0,($new_width-1),($new_height-1),$size[0],$size[1]);

            $filename = sha1(basename( $_FILES['file']['name']));
            $path = dirname(__FILE__)."/../files/pictures/".$filename;

            imagejpeg($tmp,$path,100);

            imagedestroy($src);
            imagedestroy($rotateImg);
            imagedestroy($tmp);


        }
    }

I tried some modification like the answers of this posts : - A border appears when rotating image with gd + php - PHP - Rotate image with GD gives black borders - Why when i rotate the image black borders appear? PHP GD

But nothing work.

Please guys helps me.

Thanks you

  • 写回答

2条回答 默认 最新

  • dongxu4580 2017-04-12 14:49
    关注

    The problem occurs if the EXIF data indicates rotation is necessary, because after the rotation the values of height and width are still treated the same. Since the image has been rotated these values should be switched with one another.

    The rotation needs to be done first, not last, so move the rotation code above all of the ratio calculations (to just after you declare $size). You'll need to keep track of a 90/-90 degree rotation taking place:

    $rotated = false;
    if (!empty($exif['Orientation'])) {
        switch ($exif['Orientation']) {
            case 8:
                $src = imagerotate($src, 90, 0);
                $rotated = true;
                break;
            case 3:
                $src = imagerotate($src, 180, 0);
                break;
            case 6:
                $src = imagerotate($src, -90, 0);
                $rotated = true;
                break;
        }
    }
    

    Now you need to 'flip' the values if the orientation has changed:

    if ($rotated) {
        $width = $size[1];
        $height = $size[0];
    } else {
        $width = $size[0];
        $height = $size[1];
    }
    

    In the rest of your code replace instances of $size[0] with $width and $size[1] with $height to fix the calculations.

    You could also ...($new_width-1),($new_height-1)... to just ...$new_width, $new_height... to make the resized image fit correctly, otherwise there is a 1px black border on the right and bottom of the resulting image.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛