如何在PHP中的另一个图像中插入Facebook个人资料图像

I'm trying to insert facebook profile image inside another image, i tried this method but didn't work, i'm not so good in PHP and i need your help, please.

Look my code, i think it has an error:

<?php

        create2image($_COOKIE["imagem"], "banner1.jpg")

        function create2image($profile, $background) {

            $dir = "folders";
            $img = md5(time()).'.jpg';
            $url = $profile;
            $ch = curl_init($url);
            $fp = fopen($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.$img, 'wb');
            curl_setopt($ch, CURLOPT_FILE, $fp);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_exec($ch);
            curl_close($ch);
            fclose($fp);

            $new = imagecreate(500, 250);

            $background = imagecreatefromjpeg($background);
            $profile = imagecreatefromjpeg("folders/{$img}");

            imagecopy($background, $profile, 0, 0, 0, 0, 170, 200);
            imagejpeg($new, $img); 
        }
    ?>

This is my source code

<?php
//Download images profile
function imagedownload($url,$saveto){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $raw=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($saveto)){
    unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);
    fclose($fp);
}
$url="http://scontent-frt3-1.xx.fbcdn.net/v/t1.0-0/cp0/e15/q65/p320x320/13700038_850487491753797_6227258625184891703_n.jpg?oh=793ecde8db1a8e65789534907d08b25e&oe=57F1DDFF";
$konum="/var/www/html/testGuzzle/test.jpg";
$yolla=imagedownload($url,$konum);


// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefromjpeg($konum);
$im = imagecreatefrompng('background.png');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

The first: You will download the profile images on facebook. Please make sure your directory have write permission 777 or something similar

The second: You will get content of 2 images background and profile images

The final: Merge 2 image.

You can edit position of profile image in

$marge_right = 10;
$marge_bottom = 10;
or imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom

Read more: imagecopy function

And my result:

enter image description here

always check the manual

here is a code i wrote with the help of User Contributed Notes and the example i found on the manual i tested it and working perfect here is a proof

PS: you don't need to save the facebook user image you can just save it's id and use this link over here graph.facebook.com/{FacebookId}/picture?type=large

enter image description here

<?php
// get image extension 
function imageExt($dest) {
    $fileType = strtolower(substr($dest, strlen($dest)-3));
    switch($fileType) {
        case('gif'):
            $dest = imagecreatefromgif($dest);
            break;

        case('png'):
            $dest = imagecreatefrompng($dest);
            break;

        default:
            $dest = imagecreatefromjpeg($dest);
    }
    return $dest;
}

// Create image instance
$dest = imageExt('movieshub/img/x.png');
// the facebook image is in jpeg so you don't need to get the type as destnation image
$src = imagecreatefromjpeg('http://graph.facebook.com/4/picture?type=large');

$Swidth = imageSX($src); // get source image width 
$Sheight = imageSY($src); //get source image height

// Copy and merge
// 50,50 x-coordinate , y-coordinate of destination point conside them as margins
imagecopymerge($dest, $src, 50, 50, 0, 0, $Swidth, $Sheight, 100); // 100 is for transparency 

// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);

?>

UPDATE

i think doing this in html and css is faster and easier check here