$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);
PHP 生成模糊图片
最新推荐文章于 2023-01-16 16:53:05 发布