使用GD调整图像大小并裁剪图像,同时保持纵横比

时间:2011-09-06 11:40:18

标签: php resize gd uploadify crop

我目前正在编写基于Uploadify的上传脚本。现在我调整给定图像的大小并为其中一个尺寸添加水印。这一切都运行良好,但我需要脚本来调整高度,然后裁剪宽度,以使宽高比不会搞砸。

到目前为止,这是我的代码:

if ($fileExtension == "jpg" || 
        $fileExtension == "jpeg" || 
        $fileExtension == "png" || 
        $fileExtension == "gif"){

        // GD variables:
        list($width, $height, $type) = GetImageSize($uploadedFile['tmp_name']);

        // Image sizes:
        $bigImage = array(800, 453);
        $mediumImage = array(410, 231);
        $listImage = array(120, 68);
        $thumbnail = array(90, 51);

        $sourceAspect = $width / $height;
        $bigAspect = $bigImage[0] / $bigImage[1];
        $mediumAspect = $mediumImage[0] / $mediumImage[1];
        $listAspect = $listImage[0] / $listImage[1];
        $thumbnailAspect = $thumbnail[0] / $thumbnail[1];

        // Image is PNG:
        if ($type == IMAGETYPE_PNG){
            $image = imagecreatefrompng($uploadedFile['tmp_name']);
            $valid = true;
        }

        // Image is JPEG:
        else if ($type == IMAGETYPE_JPEG){
            $image = imagecreatefromjpeg($uploadedFile['tmp_name']);
            $valid = true;
        }

        // Image is GIF:
        else if ($type == IMAGETYPE_GIF){
            $image = imagecreatefromgif($uploadedFile['tmp_name']);
            $valid = true;
        }

        // Format not allowed:
        else {
            $valid = false;
        }

        // Start creating images:
        if ($valid){

            // Get size:
            $imageSize = getimagesize($uploadedFile['tmp_name']);

            // Generate canvas:
            $bCanvas = imagecreatetruecolor($bigImage[0], $bigImage[1]);
            $mCanvas = imagecreatetruecolor($mediumImage[0], $mediumImage[1]);
            $lCanvas = imagecreatetruecolor($listImage[0], $listImage[1]);
            $tCanvas = imagecreatetruecolor($thumbnail[0], $thumbnail[1]);

            // Copy content:
            imagecopyresampled($bCanvas, $image, 0, 0, 0, 0, ($bigImage[0] * $sourceAspect), ($bigImage[1] / $sourceAspect), $imageSize[0], $imageSize[1]);
            imagecopyresampled($mCanvas, $image, 0, 0, 0, 0, $mediumImage[0], $mediumImage[1], $imageSize[0], $imageSize[1]);
            imagecopyresampled($lCanvas, $image, 0, 0, 0, 0, $listImage[0], $listImage[1], $imageSize[0], $imageSize[1]);
            imagecopyresampled($tCanvas, $image, 0, 0, 0, 0, $thumbnail[0], $thumbnail[1], $imageSize[0], $imageSize[1]);

            // Save images:
            $saveB = imagejpeg($bCanvas, $targetFile.'_big.jpg', 90);
            $saveM = imagejpeg($mCanvas, $targetFile.'_medium.jpg', 90);
            $saveT = imagejpeg($lCanvas, $targetFile.'_list.jpg', 90);
            $saveT = imagejpeg($tCanvas, $targetFile.'_thumb.jpg', 90);

            // Destroy images:
            imagedestroy($image);
            imagedestroy($bCanvas);
            imagedestroy($mCanvas);
            imagedestroy($lCanvas);
            imagedestroy($tCanvas);

            // Watermark images:
            $mark = imagecreatefrompng("logo.png");
            list($mwidth, $mheight) = getimagesize("logo.png");
            $img = imagecreatefromjpeg($targetFile.'_big.jpg');

            list($bwidth, $bheight) = getimagesize($targetFile.'_big.jpg');
            imagecopy($img, $mark, $bwidth-$mwidth-25, $bheight-$mheight-25, 0, 0, $mwidth, $mheight);
            imagejpeg($img, $targetFile.'_big.jpg', 100);
            imagedestroy($img);

        } else {
            echo "0";
        }

    } else {
        move_uploaded_file($tempFile,$targetFile.'.'.$fileExtension);
    }
如果有人可以帮我解决这个问题,我会很高兴。我一直在尝试几种方法,但它们似乎都没有正常工作。正如你在顶部看到的那样,我已经在变量“bigImage”,“mediumImage”,“listImage”和“thumbnail”中定义了我想要使用的画布大小。

提前致谢! // 乔纳森

1 个答案:

答案 0 :(得分:17)

调整图像大小的方法不止一种。我会把它拼出来给你:

  • 拉伸以适合 - 将图像调整为所需大小,忽略宽高比
  • Scale to fit - 调整图像大小以使一个尺寸(宽度或高度)具有所需尺寸,而另一个尺寸相同或更短,同时保持纵横比(可能需要一个额外步骤来填充较短边用纯色)
  • Crop to fit - 调整图像大小以使一个尺寸(宽度或高度)具有所需尺寸,而另一个尺寸相同或更长,同时保持纵横比(修剪外部区域需要一个额外步骤)
PS:这两篇文章都是我写的。