前言:關(guān)于php生成自定義海報,網(wǎng)上的教程一搜一大片,但是當(dāng)你真正用到的時候,似乎沒有一個好使的,這樣致使生成海報一直是我的一個難點(diǎn),我也懶得時間去搞這些東西,然后時間一拖就拖了兩年,今年客戶有類似的需求,想著今天就整理整理吧
- 生成海報首先要明白他是怎么生成的,其實(shí)很簡單,就是用畫布把多張圖片組合起來,然后在和背景圖組合起來就生成海報了,所以您有必要了解一下必知的畫布函數(shù)
- ①
imagecreatetruecolor創(chuàng)建背景畫布 - ②
imagecreatefromstring創(chuàng)建一個圖像資源從字符串中的圖像流。
擴(kuò)展
imagecreate — 新建一個基于調(diào)色板的圖像imagecreatetruecolor — 新建一個真彩色圖像
imagecreatefromstring — 創(chuàng)建一個圖像資源從字符串中的圖像流。
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl' . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr' . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r' . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg=='; $data = base64_decode($data); $im = imagecreatefromstring($data); if ($im !== false) { header('Content-Type: image/png'); imagepng($im); } else { echo 'An error occured.'; }
- ③
imagecopy將圖片繪制到畫布上http://www.5idev.com/p-php_imagecopy_imagecopyresized.shtml - ④
imagedestroy釋放與關(guān)聯(lián)圖片的內(nèi)存 - ⑤
imagesximagesy獲取頭像的寬高 -
imagecopyresized拷貝圖片 -
imagecolorallocate()設(shè)置圖像顏色,多用于繪制文字的顏色 -
imagettftext繪制文字 -
imagepng輸出圖像
- 然后第二步我們就需要用到一個插件
phpqrcode
關(guān)于phpqrcode其實(shí)就是php生成二維碼的一個插件,方便快捷
require_once IA_ROOT . '/framework/library/qrcode/phpqrcode.php';
首先我們先用phpqrcode做一個生成二維碼的方法接口
public function createQrcode($mid = 0, $posterid = 0)
{
global $_W;
global $_GPC;
$path = IA_ROOT . '/addons/ewei_shopv2/data/qrcode/' . $_W['uniacid'] . '/';
if (!is_dir($path)) {
load()->func('file');
mkdirs($path);
}
$url = mobileUrl('', array('mid' => $mid), true);
if (!empty($posterid)) {
$url .= '&posterid=' . $posterid;
}
$file = 'shop_qrcode_' . $posterid . '_' . $mid . '.png';
$qrcode_file = $path . $file;
if (!is_file($qrcode_file)) {
require_once IA_ROOT . '/framework/library/qrcode/phpqrcode.php';
QRcode::png($url, $qrcode_file, QR_ECLEVEL_L, 4);
}
return $_W['siteroot'] . 'addons/ewei_shopv2/data/qrcode/' . $_W['uniacid'] . '/' . $file;
}
第二步調(diào)用上面的接口生成一個可以掃描的二維碼,然后下面做背景圖,字合成,然后也是先寫一個接口,方面咱們后期直接復(fù)制使用
-
創(chuàng)建圖片(圖片流,比如base64轉(zhuǎn)換)
public function createImage($imgurl)
{
load()->func('communication');
$resp = ihttp_request($imgurl);
if ($resp['code'] == 200 && !empty($resp['content'])) {
return imagecreatefromstring($resp['content']);
}
$i = 0;
while ($i < 3) {
$resp = ihttp_request($imgurl);
if ($resp['code'] == 200 && !empty($resp['content'])) {
return imagecreatefromstring($resp['content']);
}
++$i;
}
return '';
}
-
處理圖像px
public function getRealData($data)
{
$data['left'] = intval(str_replace('px', '', $data['left'])) * 2;
$data['top'] = intval(str_replace('px', '', $data['top'])) * 2;
$data['width'] = intval(str_replace('px', '', $data['width'])) * 2;
$data['height'] = intval(str_replace('px', '', $data['height'])) * 2;
$data['size'] = intval(str_replace('px', '', $data['size'])) * 2;
$data['src'] = tomedia($data['src']);
return $data;
}
-
拷貝圖片和制作文字的接口
public function mergeImage($target, $data, $imgurl)
{
$img = $this->createImage($imgurl);
$w = imagesx($img);
$h = imagesy($img);
imagecopyresized($target, $img, $data['left'], $data['top'], 0, 0, $data['width'], $data['height'], $w, $h);
imagedestroy($img);
return $target;
}
public function mergeText($target, $data, $text)
{
$font = IA_ROOT . '/addons/ewei_shopv2/static/fonts/msyh.ttf';
$colors = $this->hex2rgb($data['color']);
$color = imagecolorallocate($target, $colors['red'], $colors['green'], $colors['blue']);
imagettftext($target, $data['size'], 0, $data['left'], $data['top'] + $data['size'], $color, $font, $text);
return $target;
}
-
上傳圖片
public function uploadImage($img)
{
load()->func('communication');
$account = m('common')->getAccount();
$access_token = $account->fetch_token();
$url = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $access_token . '&type=image';
$ch1 = curl_init();
$data = array('media' => '@' . $img);
if (version_compare(PHP_VERSION, '5.5.0', '>')) {
$data = array('media' => curl_file_create($img));
}
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
$content = @json_decode(curl_exec($ch1), true);
if (!is_array($content)) {
$content = array('media_id' => '');
}
curl_close($ch1);
return $content['media_id'];
}
總,然后調(diào)用接口
public function createPoster($poster, $member, $qr, $upload = true)
{
//$poster 海報信息 $qr 二維碼
global $_W;
$path = IA_ROOT . '/addons/ewei_shopv2/data/poster/' . $_W['uniacid'] . '/';
if (!is_dir($path)) {
load()->func('file');
mkdirs($path);
}
$md5 = md5(json_encode(array('siteroot' => $_W['siteroot'], 'openid' => $member['openid'], 'goodsid' => $qr['goodsid'], 'bg' => $poster['bg'], 'data' => $poster['data'], 'version' => 1)));
$file = $md5 . '.png';
if (!is_file($path . $file) || $qr['qrimg'] != $qr['current_qrimg']) {
//突破256M的限制
set_time_limit(0);
@ini_set('memory_limit', '256M');
$target = imagecreatetruecolor(640, 1008);
$bg = $this->createImage(tomedia($poster['bg']));
imagecopy($target, $bg, 0, 0, 0, 0, 640, 1008);
imagedestroy($bg);
$data = json_decode(str_replace('"', '\'', $poster['data']), true);
foreach ($data as $d) {
$d = $this->getRealData($d);
if ($d['type'] == 'head') {
$avatar = preg_replace('/\\/0$/i', '/96', $member['avatar']);
$target = $this->mergeImage($target, $d, $avatar);
}
else if ($d['type'] == 'img') {
$target = $this->mergeImage($target, $d, $d['src']);
}
else if ($d['type'] == 'qr') {
$target = $this->mergeImage($target, $d, tomedia($qr['current_qrimg']));
}
else if ($d['type'] == 'nickname') {
$target = $this->mergeText($target, $d, $member['nickname']);
}
else {
if (!empty($goods)) {
if ($d['type'] == 'title') {
$title_width = (int) ($d['width'] / $d['size'] / 1.2);
$width_left = 0;
while ($width_left < strlen($goods['title'])) {
$title = mb_substr($goods['title'], $width_left, $title_width, 'utf-8');
$width_left += $title_width;
$d['top'] += $d['size'] * 2;
$target = $this->mergeText($target, $d, $title);
}
}
else if ($d['type'] == 'thumb') {
$thumb = !empty($goods['commission_thumb']) ? tomedia($goods['commission_thumb']) : tomedia($goods['thumb']);
$target = $this->mergeImage($target, $d, $thumb);
}
else if ($d['type'] == 'marketprice') {
$target = $this->mergeText($target, $d, $goods['marketprice']);
}
else {
if ($d['type'] == 'productprice') {
$target = $this->mergeText($target, $d, $goods['productprice']);
}
}
}
}
}
imagepng($target, $path . $file);
imagedestroy($target);
if ($qr['qrimg'] != $qr['current_qrimg']) {
pdo_update('ewei_shop_poster_qr', array('qrimg' => $qr['current_qrimg']), array('id' => $qr['id']));
}
}
$img = $_W['siteroot'] . 'addons/ewei_shopv2/data/poster/' . $_W['uniacid'] . '/' . $file;
if (!$upload) {
return $img;
}
if ($qr['qrimg'] != $qr['current_qrimg'] || empty($qr['mediaid']) || empty($qr['createtime']) || $qr['createtime'] + 3600 * 24 * 3 - 7200 < time()) {
$mediaid = $this->uploadImage($path . $file);
$qr['mediaid'] = $mediaid;
pdo_update('ewei_shop_poster_qr', array('mediaid' => $mediaid, 'createtime' => time()), array('id' => $qr['id']));
}
return array('img' => $img, 'mediaid' => $qr['mediaid']);
}