php使用imagick扩展生成海报

文章描述:

imagick生成带微信头像的海报

head-img.jpeg是微信头像

1.jpg是海报图片

sdmk.png是头像边框图片

<?php
header( "Content-Type: image/png" );
//输出图片
$posterimg = CompositeImage();
echo $posterimg;
//输出图片
function CompositeImage() {
	$Qrcode = new Imagick("images/sdmk.png");
	//print_r($Qrcode);exit;
	$Qrcode->setImageResolution(0.1,0.3);
	//设置图片分辨率
	$QrcodeWH = $Qrcode->getImageGeometry();
	//获取源图片宽和高
	if ($QrcodeWH['width']>200) {
		$QrcodeW['width'] = 200;
		$QrcodeH['height'] = $QrcodeW['width']/$QrcodeWH['width']*$QrcodeWH['height'];
	} else {
		$QrcodeW['width'] = $QrcodeWH['width'];
		$QrcodeH['height'] = $QrcodeWH['height'];
	}
	$Qrcode->thumbnailImage( $QrcodeW['width'], $QrcodeWH['height'], true );
	//按照选定的比例进行缩放
	$wxnickpath = "images/head-img.jpeg";
	$weixin = new Imagick($wxnickpath);
	//$weixin = new Imagick("erweima.png");
	$weixin->setImageResolution(0.1,0.3);
	//$weixin->roundCorners(360,360); //圆角处理
	$wxWH = $weixin->getImageGeometry();
	if ($wxWH['width']>180) {
		$wxW['width'] = 180;
		$wxH['height'] = $wxW['width']/$wxWH['width']*$wxWH['height'];
	} else {
		$wxW['width'] = $wxWH['width'];
		$wxH['height'] = $wxWH['height'];
	}
	$weixin->thumbnailImage( $wxW['width'], $wxWH['height'], true );
	//等比例缩放
	//创建一个Imagick对象,同时获取要处理的背景图 /data/wenda/htdocs/upload
	$poster = new Imagick( "images/1.jpg" );
	$posterWH = $poster->getImageGeometry();
	$posterW['width'] = $posterWH['width'];
	$posterH['height'] = $posterWH['height'];
	// 按照缩略图大小创建一个有颜色的图片
	$canvas = new Imagick();
	$canvas->newImage( $posterW['width'], $posterH['height'], 'black', 'jpg' );
	//二维码 微信头像 背景 合成
	$poster->compositeImage($weixin,Imagick::COMPOSITE_OVER,285,412);
	//头像
	$poster->compositeImage($Qrcode,Imagick::COMPOSITE_OVER,275,402);
	//logo
	$canvas->compositeImage( $poster, imagick::COMPOSITE_OVER, 0, 0);
	$canvas->setImageCompressionQuality(60);
	//压缩质量
	//$canvas->writeImage( "upload/".time().".jpg" ); //生成图片
	return $canvas;
	//返回图片流信息
}

 

发布时间:2021/07/08

发表评论