PHP生成海报iOS选择_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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值