PHP 生成模糊图片

            
            $filename = "Uploads/card/card_poster.png";
            //生成白色背景图 用于图片合成
            $blank_bg_w = 375; // 背景图片宽度
            $blank_bg_h = 553; // 背景图片高度
            $blank_background = imagecreatetruecolor($blank_bg_w,$blank_bg_h); // 背景图片
            $bgColor = imagecolorallocate($blank_background, 240, 240, 240); // 为真彩色画布创建白色背景
            imagefill($blank_background, 0, 0, $bgColor);

            //顶部模糊背景
            list($width, $height) = getimagesize($userInfo["user_header_img"]);
            $top_blur_w = 343;
            $top_blur_h = 251;
            $top_blur = imagecreatetruecolor($top_blur_w, $top_blur_h);
            $source = imagecreatefromstring(file_get_contents($userInfo["user_header_img"]));
            imagecopyresized($top_blur, $source, 0, 0, 0, 0, $top_blur_w, $top_blur_h, $width, $height);
            imagefilter($top_blur, IMG_FILTER_SELECTIVE_BLUR);

            //模糊程度
            $blurFactor = 3;
            $blurFactor = round($blurFactor);
            $originalWidth = imagesx($top_blur);
            $originalHeight = imagesy($top_blur);
            $smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor));
            $smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor));
            $prevImage = $top_blur;
            $prevWidth = $originalWidth;
            $prevHeight = $originalHeight;

            for ($i = 0; $i < $blurFactor; $i += 1) {
                $nextWidth = $smallestWidth * pow(2, $i);
                $nextHeight = $smallestHeight * pow(2, $i);
                $nextImage = imagecreatetruecolor($nextWidth, $nextHeight);
                imagecopyresized($nextImage, $prevImage, 0, 0, 0, 0,$nextWidth, $nextHeight, $prevWidth, $prevHeight);
                imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);
                $prevImage = $nextImage;
                $prevWidth = $nextWidth;
                $prevHeight = $nextHeight;
            }

            imagecopyresized($top_blur, $nextImage, 0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight);
            imagefilter($top_blur, IMG_FILTER_GAUSSIAN_BLUR);


            //合并图像
            imagecopymerge($blank_background, $top_blur, 16, 61, 0, 0, $top_blur_w, $top_blur_h,100);

            imagettftext($blank_background, 12, 0, 36, 220, $color3, $font, "电话");
//保存图片
            imagepng($blank_background,$filename);

            //销毁图像 释放与 image 关联的内存
            imagedestroy($blank_background);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值