_imagine) {
$this->_imagine = Image::getImagine();
}
return $this->_imagine;
}
/**
* @return RGB
*/
protected function getPalette()
{
if (!$this->_palette) {
$this->_palette = new RGB();
}
return $this->_palette;
}
/**
* @param ImageInterface $bg
* @param int $x
* @param int $y
* @param string $imagePath
* @param int $w
* @param int $h
*/
protected function fillImage($bg, $x, $y, $imagePath, $w, $h)
{
$imagine = $this->getImagine();
$imageImagine = $imagine->open($imagePath);
$imageImagine->resize(new Box($w, $h));
$bg->paste($imageImagine, new Point($x, $y));
}
// 四张竖向模板
public function actionCreate()
{
$imagine = $this->getImagine();
$palette = $this->getPalette();
$fileName = "/temp/make-test-test-test.jpg";
// 图片1
$onePath = Yii::getAlias('@webroot' . '/temp/1.jpg');
$oneInfo = getimagesize($onePath);
$oneWidth = 375 / 2;
$oneHeight = 375 / 2 / $oneInfo[0] * $oneInfo[1];
$oneWidth = (int)$oneWidth;
$oneHeight = (int)$oneHeight;
// 图片2
$twoPath = Yii::getAlias('@webroot' . '/temp/2.jpg');
$twoInfo = getimagesize($twoPath);
$twoWidth = 375 - $oneWidth;
$twoHeight = $twoWidth / $twoInfo[0] * $twoInfo[1];
$twoWidth = (int)$twoWidth;
$twoHeight = (int)$twoHeight;
// 图片3
$threePath = Yii::getAlias('@webroot' . '/temp/3.jpg');
$threeInfo = getimagesize($threePath);
$threeWidth = 375 - $oneWidth;
$threeHeight = $oneHeight - $twoHeight;
$threeWidth = (int)$threeWidth;
$threeHeight = (int)$threeHeight;
// 图片4
$fourPath = Yii::getAlias('@webroot' . '/temp/4.jpg');
$fourInfo = getimagesize($fourPath);
$fourWidth = 375;
$fourHeight = 375 / $fourInfo[0] * $fourInfo[1];
$fourWidth = (int)$fourWidth;
$fourHeight = (int)$fourHeight;
// 二维码
$codePath = Yii::getAlias('@webroot' . '/temp/code.jpg');
$codeInfo = getimagesize($codePath);
$codeWidth = 100;
$codeHeight = 100 / $codeInfo[0] * $codeInfo[1];
$codeWidth = (int)$codeWidth;
$codeHeight = (int)$codeHeight;
// 参数定义
$width = 375;
$totalHeight = 0;
$totalHeight = $totalHeight + $oneHeight + $fourHeight + $codeHeight;
$bg = $imagine->create(new Box($width, $totalHeight), $palette->color('#fff'));
$this->fillImage($bg, 0, 0, $onePath, $oneWidth, $oneHeight);
$this->fillImage($bg, $oneWidth, 0, $twoPath, $twoWidth, $twoHeight);
$this->fillImage($bg, $oneWidth, $twoHeight, $threePath, $threeWidth, $threeHeight);
$this->fillImage($bg, 0, $oneHeight, $fourPath, $fourWidth, $fourHeight);
$this->fillImage($bg, 0, $oneHeight + $fourHeight, $codePath, $codeWidth, $codeHeight);
$bg->draw()->text(
'哈哈哈',
new \Imagine\Gd\Font(Yii::getAlias('@webroot/font/msyh.ttc'), '18', $palette->color('#333')),
new Point($codeWidth + 10, $oneHeight + $fourHeight + 10)
);
$bg->save(Yii::getAlias('@webroot' . $fileName), [
'jpeg_quality' => 200,
]);
}
// 图片切圆,拼到小程序码上
public function actionTest()
{
// 头像
$imgPath = Yii::getAlias('@webroot' . '/temp/header.jpg');
// 小程序码
$minCode = Yii::getAlias('@webroot' . '/temp/min_code.jpg');
// 头像的存储位置
$header = Yii::getAlias('@webroot' . '/temp/default_avatar.jpg');
// 合成的小程序存储位置
$codeSavePath = Yii::getAlias('@webroot' . '/temp/make-min-code.jpg');
// 切圆
$this->radiusImage($imgPath);
$imagine = $this->getImagine();
$minCodeImagine = $imagine->open($minCode);
$minCodeImagine->resize(new Box(1000, 1000));
$headerImagine = $imagine->open($header);
$headerImagine->resize(new Box(440, 440));
$minCodeImagine->paste($headerImagine, new Point(280, 280));
$minCodeImagine->save($codeSavePath, [
'jpeg_quality' => 200,
]);
}
protected function radiusImage($imgPath)
{
$extension = pathinfo($imgPath);
$savePath = Yii::getAlias('@webroot' . '/temp/default_avatar.' . $extension['extension']);
$srcImg = null;
switch ($extension['extension']) {
case 'jpg':
$srcImg = imagecreatefromjpeg($imgPath);
break;
case 'png':
$srcImg = imagecreatefrompng($imgPath);
break;
}
$info = getimagesize($imgPath);
$width = $info[0];
$height = $info[1];
$minLength = min($width, $height);
$img = imagecreatetruecolor($width, $height);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$radius = $minLength / 2; //圆半径
for ($x = 0; $x < $minLength; $x++) {
for ($y = 0; $y < $minLength; $y++) {
$rgbColor = imagecolorat($srcImg, $x, $y);
if (((($x - $radius) * ($x - $radius) + ($y - $radius) * ($y - $radius)) <= ($radius * $radius))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
$result = imagepng($img, $savePath);
imagedestroy($img);
return $result ? $savePath : false;
}
}