Merge two images with round corner for the 2nd in PHP
<p>I am trying to make the <strong>second image with round corner</strong>. A perfect circle would be perfect. I tried with this <a href="https://stackoverflow.com/questions/37096710/merge-two-images-and-round-corner-in-php">Merge two images and round corner in PHP</a> but I couldn't make it work. I also add some text on my final image.</p>

<pre><code>$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);
</code></pre>

<p><strong>EDIT :</strong> 
I got a beginning of an answer. I got a cropped pic in one hand and my merge image (not cropped) in an other hand :</p>

<pre><code>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-&gt;src_img = $img;
        $this-&gt;src_w = imagesx($img);
        $this-&gt;src_h = imagesy($img);
        $this-&gt;dst_w = imagesx($img);
        $this-&gt;dst_h = imagesy($img);
    }

    public function __destruct()
    {
        if (is_resource($this-&gt;dst_img))
        {
            imagedestroy($this-&gt;dst_img);
        }
    }

    public function display()
    {
        header("Content-type: image/png");
        imagepng($this-&gt;dst_img);
        return $this;
    }

    public function reset()
    {
        if (is_resource(($this-&gt;dst_img)))
        {
            imagedestroy($this-&gt;dst_img);
        }
        $this-&gt;dst_img = imagecreatetruecolor($this-&gt;dst_w, $this-&gt;dst_h);
        imagecopy($this-&gt;dst_img, $this-&gt;src_img, 0, 0, 0, 0, $this-&gt;dst_w, $this-&gt;dst_h);
        return $this;
    }

    public function size($dstWidth, $dstHeight)
    {
        $this-&gt;dst_w = $dstWidth;
        $this-&gt;dst_h = $dstHeight;
        return $this-&gt;reset();
    }

    public function crop()
    {
        // Intializes destination image
        $this-&gt;reset();

        // Create a black image with a transparent ellipse, and merge with destination
        $mask = imagecreatetruecolor($this-&gt;dst_w, $this-&gt;dst_h);
        $maskTransparent = imagecolorallocate($mask, 255, 0, 255);
        imagecolortransparent($mask, $maskTransparent);
        imagefilledellipse($mask, $this-&gt;dst_w / 2, $this-&gt;dst_h / 2, $this-&gt;dst_w, $this-&gt;dst_h, $maskTransparent);
        imagecopymerge($this-&gt;dst_img, $mask, 0, 0, 0, 0, $this-&gt;dst_w, $this-&gt;dst_h, 100);

        // Fill each corners of destination image with transparency
        $dstTransparent = imagecolorallocate($this-&gt;dst_img, 255, 0, 255);
        imagefill($this-&gt;dst_img, 0, 0, $dstTransparent);
        imagefill($this-&gt;dst_img, $this-&gt;dst_w - 1, 0, $dstTransparent);
        imagefill($this-&gt;dst_img, 0, $this-&gt;dst_h - 1, $dstTransparent);
        imagefill($this-&gt;dst_img, $this-&gt;dst_w - 1, $this-&gt;dst_h - 1, $dstTransparent);
        imagecolortransparent($this-&gt;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-&gt;crop()-&gt;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);
</code></pre>