加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP / GD – 裁剪和调整图像大小

发布时间:2020-12-13 18:00:36 所属栏目:PHP教程 来源:网络整理
导读:我编写了一个函数,将图像裁剪为给定的宽高比,最后调整大小并将其输出为JPG: ?phpfunction Image($image,$crop = null,$size = null){ $image = ImageCreateFromString(file_get_contents($image)); if (is_resource($image) === true) { $x = 0; $y = 0; $w
我编写了一个函数,将图像裁剪为给定的宽高比,最后调整大小并将其输出为JPG:
<?php

function Image($image,$crop = null,$size = null)
{
    $image = ImageCreateFromString(file_get_contents($image));

    if (is_resource($image) === true)
    {
        $x = 0;
        $y = 0;
        $width = imagesx($image);
        $height = imagesy($image);

        /*
        CROP (Aspect Ratio) Section
        */

        if (is_null($crop) === true)
        {
            $crop = array($width,$height);
        }

        else
        {
            $crop = array_filter(explode(':',$crop));

            if (empty($crop) === true)
            {
                $crop = array($width,$height);
            }

            else
            {
                if ((empty($crop[0]) === true) || (is_numeric($crop[0]) === false))
                {
                    $crop[0] = $crop[1];
                }

                else if ((empty($crop[1]) === true) || (is_numeric($crop[1]) === false))
                {
                    $crop[1] = $crop[0];
                }
            }

            $ratio = array
            (
                0 => $width / $height,1 => $crop[0] / $crop[1],);

            if ($ratio[0] > $ratio[1])
            {
                $width = $height * $ratio[1];
                $x = (imagesx($image) - $width) / 2;
            }

            else if ($ratio[0] < $ratio[1])
            {
                $height = $width / $ratio[1];
                $y = (imagesy($image) - $height) / 2;
            }

            /*
            How can I skip (join) this operation
            with the one in the Resize Section?
            */

            $result = ImageCreateTrueColor($width,$height);

            if (is_resource($result) === true)
            {
                ImageSaveAlpha($result,true);
                ImageAlphaBlending($result,false);
                ImageFill($result,ImageColorAllocateAlpha($result,255,127));

                ImageCopyResampled($result,$image,$x,$y,$width,$height,$height);

                $image = $result;
            }
        }

        /*
        Resize Section
        */

        if (is_null($size) === true)
        {
            $size = array(imagesx($image),imagesy($image));
        }

        else
        {
            $size = array_filter(explode('x',$size));

            if (empty($size) === true)
            {
                $size = array(imagesx($image),imagesy($image));
            }

            else
            {
                if ((empty($size[0]) === true) || (is_numeric($size[0]) === false))
                {
                    $size[0] = round($size[1] * imagesx($image) / imagesy($image));
                }

                else if ((empty($size[1]) === true) || (is_numeric($size[1]) === false))
                {
                    $size[1] = round($size[0] * imagesy($image) / imagesx($image));
                }
            }
        }

        $result = ImageCreateTrueColor($size[0],$size[1]);

        if (is_resource($result) === true)
        {
            ImageSaveAlpha($result,true);
            ImageAlphaBlending($result,true);
            ImageFill($result,ImageColorAllocate($result,255));
            ImageCopyResampled($result,$size[0],$size[1],imagesx($image),imagesy($image));

            header('Content-Type: image/jpeg');

            ImageInterlace($result,true);
            ImageJPEG($result,null,90);
        }
    }

    return false;
}

?>

该功能按预期工作,但我正在创建一个非必需的GD图像资源,我该如何解决它?我试过加入两个电话,但我必须做一些错误的计算.

<?php

/*
Usage Examples
*/

Image('http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png','1:1','600x');
Image('http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png','2:1','2:','250x300');

?>

非常感谢任何帮助,谢谢.

您必须修改调整大小代码,而不是基于要开始的裁剪图像.由于您希望一次性进行裁剪和调整大小,因此您需要单独计算它.
<?php
function Image($image,$crop = ':',$size = null) {

    $image = ImageCreateFromString(file_get_contents($image));

    if (is_resource($image)) {

        $x = 0;
        $y = 0;
        $width = imagesx($image);
        $height = imagesy($image);

        // CROP (Aspect Ratio) Section
        $crop = array_filter(explode(':',$crop));

        if (empty($crop)) {

            $crop = [$width,$height];

        } else {

            $crop[0] = $crop[0] ?: $crop[1];
            $crop[1] = $crop[1] ?: $crop[0];

        }

        $ratio = [$width / $height,$crop[0] / $crop[1]];

        if ($ratio[0] > $ratio[1]) {

            $width = $height * $ratio[1];
            $x = (imagesx($image) - $width) / 2;

        } else {

            $height = $width / $ratio[1];
            $y = (imagesy($image) - $height) / 2;

        }


        // Resize Section    
        if (is_null($size)) {

            $size = [$width,$height];

        } else {

            $size = array_filter(explode('x',$size));

            if (empty($size)) {

                $size = [imagesx($image),imagesy($image)];

            } else {

                $size[0] = $size[0] ?: round($size[1] * $width / $height);
                $size[1] = $size[1] ?: round($size[0] * $height / $width);

            }
        }

        $result = ImageCreateTrueColor($size[0],$size[1]);

        if (is_resource($result)) {

            ImageSaveAlpha($result,$height);

            ImageInterlace($result,90);

        }
    }

    return false;
}

header('Content-Type: image/jpeg');
Image('http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png','600x');

?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读