我正在尝试制作带有圆角的第二张图片。一个完美的圆圈将是完美的。我尝试了这个Merge two images and round corner in PHP,但我无法使其发挥作用。我还在最终图像上添加了一些文字。
$numberOfImages = 2;
$background = imagecreatetruecolor(900, 900);
$firstUrl = '1.png';
// THIS IS THE ONE I WANT IN CIRCLE
$secondUrl = '2.jpeg';
$outputImage = $background;
$first = imagecreatefrompng($firstUrl);
$second = imagecreatefromjpeg($secondUrl);
imagecopymerge($outputImage,$first,0,0,0,0, 900, 900,100);
imagecopymerge($outputImage,$second,350,50,0,0, 200, 200,100);
// ADD TEXT
$white = imagecolorallocate($outputImage, 255, 255, 255);
$font_path = 'SOME_FONT';
$text = 'This is my test';
imagettftext($outputImage, 90, 0, 250, 700, $white, $font_path, $text);
// I SAVE IT
imagejpeg('test.jpg');
imagedestroy($outputImage);
编辑: 我得到了答案的开头。我用一只手拿着裁剪的照片,另一只手拿着我的合并图像(没有裁剪):
class CircleCrop
{
private $src_img;
private $src_w;
private $src_h;
private $dst_img;
private $dst_w;
private $dst_h;
public function __construct($img)
{
$this->src_img = $img;
$this->src_w = imagesx($img);
$this->src_h = imagesy($img);
$this->dst_w = imagesx($img);
$this->dst_h = imagesy($img);
}
public function __destruct()
{
if (is_resource($this->dst_img))
{
imagedestroy($this->dst_img);
}
}
public function display()
{
header("Content-type: image/png");
imagepng($this->dst_img);
return $this;
}
public function reset()
{
if (is_resource(($this->dst_img)))
{
imagedestroy($this->dst_img);
}
$this->dst_img = imagecreatetruecolor($this->dst_w, $this->dst_h);
imagecopy($this->dst_img, $this->src_img, 0, 0, 0, 0, $this->dst_w, $this->dst_h);
return $this;
}
public function size($dstWidth, $dstHeight)
{
$this->dst_w = $dstWidth;
$this->dst_h = $dstHeight;
return $this->reset();
}
public function crop()
{
// Intializes destination image
$this->reset();
// Create a black image with a transparent ellipse, and merge with destination
$mask = imagecreatetruecolor($this->dst_w, $this->dst_h);
$maskTransparent = imagecolorallocate($mask, 255, 0, 255);
imagecolortransparent($mask, $maskTransparent);
imagefilledellipse($mask, $this->dst_w / 2, $this->dst_h / 2, $this->dst_w, $this->dst_h, $maskTransparent);
imagecopymerge($this->dst_img, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h, 100);
// Fill each corners of destination image with transparency
$dstTransparent = imagecolorallocate($this->dst_img, 255, 0, 255);
imagefill($this->dst_img, 0, 0, $dstTransparent);
imagefill($this->dst_img, $this->dst_w - 1, 0, $dstTransparent);
imagefill($this->dst_img, 0, $this->dst_h - 1, $dstTransparent);
imagefill($this->dst_img, $this->dst_w - 1, $this->dst_h - 1, $dstTransparent);
imagecolortransparent($this->dst_img, $dstTransparent);
return $this;
}
}
$url = 'http://orig09.deviantart.net/a6b6/f/2011/200/f/e/transparente_by_eliseoeli-d40zcu1.png';
$img = imagecreatefrompng($url);
$crop = new CircleCrop($img);
$crop->crop()->display();
$background = imagecreatetruecolor(900, 900);
$firstUrl = 'http://img15.deviantart.net/498a/i/2012/263/3/a/background_for_fantastic_five_by_pastel_bunbun-d5fdwdb.png';
$secondUrl = 'http://orig09.deviantart.net/a6b6/f/2011/200/f/e/transparente_by_eliseoeli-d40zcu1.png';
$outputImage = $background;
$first = imagecreatefrompng($firstUrl);
$second = imagecreatefrompng($secondUrl);
imagecopymerge($outputImage,$first,0,0,0,0, 900, 900,100);
imagecopymerge($outputImage,$second,350,50,0,0, 200, 200,100);
imagejpeg($outputImage, 'test2.jpg');
imagedestroy($outputImage);