php代碼:
// ==================== 合成圖片 begin ====================
$bg_width = 480;
$bg_height = 384;
$bg = imagecreatetruecolor($bg_width, $bg_height);
$white = imagecolorallocate($bg, 255, 255, 255);
// imagecolortransparent($bg, $white);
imagefill($bg, 0, 0, $white);
// 字體文件
$font_file = 'SourceHanSansCN-Light.otf';
$font_size = 14;
// 繪制作者
$title = ‘測(cè)試';
$box = imagettfbbox($font_size, 0, $font_file, $title); // 取得使用 TrueType 字體的文本的范圍 x-y
$title_width = abs($box[2] - $box[0]); // 右下角x - 左下角x = 計(jì)算出title占用的寬度
$color = imagecolorallocate($bg, 192,192,192); // 灰色
imagettftext($bg, $font_size, 0, 10, 26, $color, $font_file, $title);
// 繪制發(fā)布時(shí)間
$title = date("Y-m-d H:i", time());
imagettftext($bg, $font_size, 0, $title_width+20, 26, $color, $font_file, $title);
// 繪制一行描述
$title = "景區(qū)-推薦 九寨溝:重建將探索“世遺”搶救...";
$color = imagecolorallocate($bg, 56, 56, 56); // 灰色
imagettftext($bg, $font_size+1, 0, 10, 66, $color, $font_file, $title);
// 繪制第一張圖
// $bg = imagecreatefromstring ( file_get_contents($bg) ); //open picture source
$item_imagessize = getimagesize('0208_b.png');
$item_width = $item_imagessize[0];
$item_height = $item_imagessize[1];
// $thumb1 = imagecreatefrompng('0208_b.png');
$precent = $item_height / $item_width;
$thumb1 = imagecreatefromstring ( file_get_contents('0208_b.png') ); //open picture source
//x軸430 y軸670 寬175高175
imagecopyresampled ( $bg, $thumb1, 10, 86, 0, 0, $bg_width-20, ((($bg_width-20)*$item_height)/$item_width), $item_width, $item_height ); // 這里是背景圖與二維碼的合成
$result_png = "share_pic.png"; // file name
$file = $result_png; // 繪制完成的圖片
// header('content-type:image/png');
imagepng ( $bg, $file ); //output picture
imagedestroy($bg);
imagedestroy($thumb1);
最終生成的圖片: