/**
* @param $params 海报图片保存
* @return
* @author 王亚烽
* @since 2019-04-29 09:20:05
*/
public function poster_picture_save()
{
try {
ini_set("memory_limit", "128M");
$background_image ='11';
$goods_code = 'g1';
$user_code = '11';
if (empty($goods_code)) {
$php_errormsg = array(
'statusCode' => '100',
'message' => '商品编码不能为空'
);
echo json_encode($php_errormsg);
exit;
}
$goods_result = $this->goods_model->get_goods_details($goods_code);
if (!$goods_result) {
$php_errormsg = array(
'statusCode' => '102',
'message' => '数据异常'
);
echo json_encode($php_errormsg);
exit;
}
//海报图
$poster_images = $this->goods_resource_model->get_resource(array(
'goods_code' => $goods_code,
'r_type' => 1,
'b_type' => 3
));
if (!isset($poster_images) || empty($poster_images)) {
$php_errormsg = array(
'statusCode' => '100',
'message' => '无海报图'
);
echo json_encode($php_errormsg);
exit;
}
if (empty($background_image)) {
//去查默认背景
$pic = $poster_images[0]['url'];
} else {
//获取背景图
// $pic = "/upload/position/code_png/19.png";
$pic = 'http://qnimg.ruwii.com/201909020916102965.jpg';
}
//sku数组
if (isset($goods_result->qrt_code)) {
$skus = $this->qrt_goods_sku_model->get_qrt_goods_skus($goods_result->qrt_code);
} else {
if ($goods_result->is_time_limited_activity==2){
$skus = $this->time_limited_activity_goods_sku_model->
get_goods_skus($goods_result->time_limited_activity_code);
}else{
$skus = $this->goods_sku_model->get_goods_sku(array('tgs.goods_code' => $goods_code));
}
}
if (!isset($skus) || empty($skus)) {
$php_errormsg = array(
'statusCode' => '100',
'message' => '无sku'
);
echo json_encode($php_errormsg);
exit;
}
//计算特殊数值
$total_surplus = 0;
$min_price = $max_price = $skus[0]['price'];
foreach ($skus as $key => $val) {
$skus[$key]['price'] = $val['price'];
$total_surplus += $val['num'];
if ($val['price'] < $min_price) {
$min_price = $val['price'];
}
if ($val['price'] > $max_price) {
$max_price = $val['price'];
}
}
// if ($min_price == $max_price) {
$interval_price = $min_price;
// }
// else {
// $interval_price = $min_price . '-' . $max_price;
// }
//获取商品
$gData = [
//背景图
'pic' => $pic,
//商品头图
'left_pic' => $goods_result->position_pic_url,
//当前价
'price_pic' => $this->config->item('poster_path') . "code_png/19.png",
//标准价
'original_pic' => $this->config->item('poster_path') . "code_png/20.png",
//渐变
'gradual_pic' => $this->config->item('poster_path') . "code_png/999.png",
//logo
// 'logo_pic' => $this->config->item('poster_path') . "code_png/logo.jpg",
//商品title
'title' => $goods_result->short_title,
//商品价格
'price' => floatval($interval_price),
//标准价
'original_price' => floatval($goods_result->price_origin),
'coupon_price' => 100
];
/**
*开始生成海报
**/
include 'qrcode.php';
$url ='http://'.$_SERVER['SERVER_NAME'] .'/mihe/#/Detail' . "?id=" . $goods_code . "&user_code=" . $user_code; //二维码内容
$errorCorrectionLevel = 'L';//容错级别
$matrixPointSize = 6;//生成图片大小
$qr_filename = "/" . date("YmdHis", time()) . rand(1, 4000) . ".png";
$qrpath = $this->config->item('poster_path') . date("Ymd", time());
//创建目录
if (!is_dir($qrpath)) {
if (!@mkdir($qrpath, 0777, TRUE)) {
echo json_encode(array('statusCode' => '100', 'message' => 'create_dir_error'));
exit;
}
chmod($qrpath, 0777);
}
//生成二维码
QRcode::png($url, $qrpath . $qr_filename, $errorCorrectionLevel, $matrixPointSize, 2);
$logo = $this->config->item('poster_path') . "code_png/logo.png";
if ($logo !== FALSE) {
//$qrpath . $qr_filename 二维码图片地址
$QR = imagecreatefromstring(file_get_contents($qrpath . $qr_filename));
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);//二维码图片宽度
$QR_height = imagesy($QR);//二维码图片高度
$logo_width = imagesx($logo);//logo图片宽度
$logo_height = imagesy($logo);//logo图片高度
$logo_qr_width = $QR_width / 5; //logo图片在二维码图片中宽度大小
$scale = $logo_width / $logo_qr_width;
$logo_qr_height = $logo_height / $scale; //logo图片在二维码图片中高度大小
$from_width = ($QR_width - $logo_qr_width) / 2;
//重新组合图片并调整大小
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
$logo_qr_height, $logo_width, $logo_height);
}
$filename = $qrpath . $qr_filename;
imagepng($QR, $filename); // 保存最终生成的二维码到本地
$poster_filename = date("YmdHis", time()) . rand(1, 4000) . ".jpeg";
$result = $this->createSharePng($gData, $qrpath . $qr_filename, $poster_filename);
if ($result) {
//存入数据库
$this->user_posters_model->insert(array('posters_url' =>
$this->config->item('img_url') . $poster_filename, 'user_code' => $user_code));
$params = array(
'statusCode' => '200',
'message' => '生成海报成功',
'position_img' => 'http://'.$_SERVER['SERVER_NAME'] .'/mihe/mihemall/upload/position/'. $poster_filename
);
var_dump($params);
// echo json_encode($params);
exit;
} else {
$php_errormsg = array(
'statusCode' => '102',
'message' => '海报生成失败'
);
echo json_encode($php_errormsg);
exit;
}
} catch
(Exception $e) {
print $e->getMessage();
exit();
}
}
function createSharePng($gData, $codeName = "", $fileName = '')
{
//创建画布
$im = imagecreatetruecolor(1100, 2000);
//填充画布背景色
$color = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $color);
//字体文件
$font_file = $this->config->item('poster_path') . "/code_png/MSYH.TTF";
//设定字体的颜色
$font_color_2 = ImageColorAllocate($im, 255, 255, 255);
$font_color_3 = ImageColorAllocate($im, 129, 129, 129);
$font_color_red = ImageColorAllocate($im, 244, 0, 86);
//商品图片
list($g_w, $g_h) = getimagesize($gData['pic']);
$goodImg = $this->createImageFromFile($gData['pic']);
imagecopyresized($im, $goodImg, 0, 0, 0, 0, 1100, 2000, $g_w, $g_h);
//渐变图片
list($l_w, $l_h) = getimagesize($gData['gradual_pic']);
$gradualImg = $this->createImageFromFile($gData['gradual_pic']);
imagecopyresized($im, $gradualImg, 0, 0, 0, 0, 1100, 2000, $l_w, $l_h);
//左边商品图片
list($l_w, $l_h) = getimagesize($gData['left_pic']);
$leftImg = $this->createImageFromFile($gData['left_pic']);
imagecopyresized($im, $leftImg, 50, 1680, 0, 0, 220, 220, $l_w, $l_h);
//二维码
list($code_w, $code_h) = getimagesize($codeName);
$codeImg = $this->createImageFromFile($codeName);
imagecopyresized($im, $codeImg, 840, 1710, 0, 0, 190, 190, $code_w, $code_h);
//商品描述
$theTitle = $this->cn_row_substr($gData['title'], 2, 17);
imagettftext($im, 35, 0, 50, 1520, $font_color_2, $font_file, $theTitle[1]);
imagettftext($im, 35, 0, 50, 1580, $font_color_2, $font_file, $theTitle[2]);
//底价
imagettftext($im, 33, 0, 300, 1800, $font_color_2, $font_file, "底价");
imagettftext($im, 55, 0, 400, 1810, $font_color_red, $font_file, "¥" . $gData["price"]);
//专柜价
imagettftext($im, 26, 0, 302, 1880, $font_color_3, $font_file, "专柜价");
$i = strlen($gData["original_price"]);
$flag = '—';
for ($x = 1; $x <= $i; $x++) {
$flag .= '—';
}
imagettftext($im, 28, 0, 420, 1877, $font_color_3, $font_file, $flag);
imagettftext($im, 36, 0, 420, 1884, $font_color_3, $font_file, "¥" . $gData["original_price"]);
//输出图片
if ($fileName) {
$result = imagejpeg($im, $this->config->item('poster_path') . $fileName);
//释放空间
imagedestroy($im);
imagedestroy($goodImg);
imagedestroy($codeImg);
return $result;
} else {
Header("Content-Type: image/png");
$result = imagepng($im);
//释放空间
imagedestroy($im);
imagedestroy($goodImg);
imagedestroy($codeImg);
return $result;
}
}
/**
* 从图片文件创建Image资源
* @param $file 图片文件,支持url
* @return bool|resource 成功返回图片image资源,失败返回false
*/
function createImageFromFile($file)
{
if (preg_match('/http(s)?:\/\//', $file)) {
$fileSuffix = $this->getNetworkImgType($file);
} else {
$fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
}
if (!$fileSuffix) return false;
switch ($fileSuffix) {
case 'jpeg':
$theImage = @imagecreatefromjpeg($file);
if (!$theImage) {
$theImage = @imagecreatefromstring(file_get_contents($file));
}
break;
case 'jpg':
$theImage = @imagecreatefromjpeg($file);
if (!$theImage) {
$theImage = @imagecreatefromstring(file_get_contents($file));
}
break;
case 'png':
$theImage = @imagecreatefrompng($file);
if (!$theImage) {
$theImage = @imagecreatefromstring(file_get_contents($file));
}
break;
case 'gif':
$theImage = @imagecreatefromgif($file);
if (!$theImage) {
$theImage = @imagecreatefromstring(file_get_contents($file));
}
break;
default:
$theImage = @imagecreatefromstring(file_get_contents($file));
break;
}
return $theImage;
}
php生成海报
最新推荐文章于 2024-09-12 07:59:54 发布