如何在PHP

时间:2016-07-17 03:27:27

标签: php facebook

我试图将facebook个人资料图片插入到另一个图片中,我尝试了这种方法,但没有工作,我在PHP方面不太好,我需要你的帮助。

查看我的代码,我认为它有一个错误:

<?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); 
        }
    ?>

2 个答案:

答案 0 :(得分:0)

这是我的源代码

<?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);
?>

第一个:您将在Facebook上下载个人资料图片。 请确保您的directorywrite permission 777 or something similar

第二种:您将获得2张图片backgroundprofile images

的内容

决赛:合并2张图片。

您可以在

中修改个人资料图片的position
$marge_right = 10;
$marge_bottom = 10;
or imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom

了解详情:imagecopy function

我的结果:

enter image description here

答案 1 :(得分:0)

始终查看manual

这是我在User Contributed Notes的帮助下编写的代码以及我在手册中找到的示例 我测试了它,在这里工作完美是一个证据

PS:您不需要保存Facebook用户图像,只需保存它的ID并在此处使用此链接即可 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);

?>

<强>更新

我认为在html和css中执行此操作会更快更轻松地检查here