php生成海报

Posted 打你一下

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php生成海报相关的知识,希望对你有一定的参考价值。

<?php

namespace lib;

/**
* Description of Poster
*
* @author lsf
*/
class Poster {

//put your code here
// header("Content-type:image/png");
// imagepng(p("agent")->getSbook($_GPC["id"], $_GPC["i"])); //输出图像到浏览器
// imagedestroy(p("agent")->getSbook($_GPC["id"], $_GPC["i"])); //释放资源


function mergeText($target, $data, $text) {
$font = ROOT_PATH . "/public/static/web/fonts/MSYHL.TTC";
$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;
}

function mergeImageJpg($target, $data, $imgurl) {

$img = imagecreatefromjpeg($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;
}

function mergeImagePng($target, $data, $imgurl) {
$img = imagecreatefrompng($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;
}

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;
if (!empty($data[‘size‘])) {
$data[‘size‘] = intval(str_replace(‘px‘, ‘‘, $data[‘size‘])) * 2;
}
if (!empty($data[‘src‘])) {
$data[‘src‘] = httpImage($data[‘src‘]);
}
return $data;
}

function hex2rgb($colour) {
if ($colour[0] == ‘#‘) {
$colour = substr($colour, 1);
}
if (strlen($colour) == 6) {
list($r, $g, $b) = array($colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5]);
} else if (strlen($colour) == 3) {
list($r, $g, $b) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]);
} else {
return false;
}
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
return array(‘red‘ => $r, ‘green‘ => $g, ‘blue‘ => $b);
}

/**
* 图片装换
* @param type $uniacid
* @param type $file
* @param type $img
* @param type $isDown
* @return type
*/
public function getTurnImg($uniacid, $file = "", $img,$imgName) {
$fileName = ROOT_PATH . ‘public‘ . DS . ‘uploads/‘ . $uniacid . "/" . $file . "/";
$dir = iconv("UTF-8", "GBK", $fileName);
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
$upload = "/uploads/" . $uniacid . "/" . $file . "/";
$imgFile = ROOT_PATH . ‘public‘ . DS . ‘uploads/‘ . $uniacid . "/" . $file . "/" . $imgName;
file_put_contents($imgFile, $img);
return array("upload" => $upload . $imgName, "filePath" => $imgFile);
}

 

}

 

前端生成海报

$poster = new Poster();
$bookTelement = EnshiWsToolAllState::bookTelement();
$target = imagecreatetruecolor($bookTelement[2]["width"] * 2, $bookTelement[2]["height"] * 2);
$color = imagecolorAllocate($target, 255, 255, 255);
imagefill($target, 0, 0, $color);
$imagesArr = explode(".", $basicsetup[‘images‘]);
if ($imagesArr[1] == "jpg") {
$bg = imagecreatefromjpeg(httpImage($basicsetup[‘images‘]));
} else {
$bg = imagecreatefrompng(httpImage($basicsetup[‘images‘]));
}
$w = imagesx($bg);
$h = imagesy($bg);
imagecopy($target, $bg, 0, 0, 0, 0, $w, $h);
imagedestroy($bg);
$data = json_decode(html_entity_decode($basicsetup[‘posterdata‘]), true);
foreach ($data as $d) {
$d = $poster->getRealData($d);
if (!empty($bookTelement[1][$d[‘type‘]])) {
if (!empty($member[$d[‘type‘]])) {
$imagesTypeArr = explode(".", $member[$d[‘type‘]]);
if (!empty($imagesTypeArr[1])) {
try {
if ($imagesTypeArr[1] == "jpg") {
$target = $poster->mergeImageJpg($target, $d, httpImage($member[$d[‘type‘]]));
} else {
$target = $poster->mergeImagePng($target, $d, httpImage($member[$d[‘type‘]]));
}
} catch (Exception $exc) {

}
} else {
$target = $poster->mergeImageJpg($target, $d, httpImage($member[$d[‘type‘]]));
}
}
} else {
if (!empty(strtotime($bookTelement[0][$d[‘type‘]]))) {
$time = date(‘Y-m-d‘, $member[$d[‘type‘]]);
$target = $poster->mergeText($target, $d, $time);
} else {
$target = $poster->mergeText($target, $d, $member[$d[‘type‘]]);
}
}
}
imagepng($target); //输出图像到浏览器
imagedestroy($target); //释放资源
exit();

 

以上是关于php生成海报的主要内容,如果未能解决你的问题,请参考以下文章

php GD库生成分享海报

php GD库生成分享海报

php生成海报

原生PHP 调用原生GD库 生成海报

原生PHP 调用原生GD库 生成海报

原生PHP 调用原生GD库 生成海报