ctms/dacms-home/controllers/ImageController.php
fm453 314745edf8 优化ctms-api语法、修复已知BUG;
主要修复ctms-api、dacms对PHP新版本的支持问题
2025-04-10 23:19:15 +08:00

293 lines
11 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace frontend\controllers;
use Yii;
use yii\web\IdentityInterface;
use yii\helpers\Url;
// use yii\imagine\Image;
class ImageController extends \yii\web\Controller
{
public $enableCsrfValidation = FALSE;//取消对POST数据的csrf令牌验证
public function actionIndex()
{
global $_GPC;
global $_HI;
global $_FM;
$_HI['action'] = 'index';
//本方法采用GD库支持
//创建图片
$height = isset($_HI['post']['h']) ? abs($_HI['post']['h']) : (isset($_HI['get']['h']) ? abs($_HI['get']['h']) : 0);
$height = !$height ? 300 : $height;
$width = isset($_HI['post']['w']) ? abs($_HI['post']['w']) : (isset($_HI['get']['w']) ? abs($_HI['get']['w']) : 0);
$width = !$width ? 300 : $width;
$im = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream"); //参数为宽度和高度
// $im=ImageCreateFromJpg("https://public.hiluker.com/vcms/nopic.jpg"); //使用背景图片创建
$bc = isset($_HI['post']['bc']) ? ltrim($_HI['post']['bc']) : (isset($_HI['get']['bc']) ? ltrim($_HI['get']['bc']) : '');
$bc = !$bc ? 'AFAFAF' : $bc;
$fc = isset($_HI['post']['fc']) ? ltrim($_HI['post']['fc']) : (isset($_HI['get']['fc']) ? ltrim($_HI['get']['fc']) : '');
$fc = !$fc ? '333333' : $fc;
//添加颜色
$bc = $this->Hex2RGB('#' . $bc);
$fc = $this->Hex2RGB('#' . $fc);
$text_color = imagecolorallocate($im, $fc['r'], $fc['g'], $fc['b']);
$background_color = imagecolorallocate($im, $bc['r'], $bc['g'], $bc['b']);
$default_color = imagecolorallocate($im, 5, 5, 5);
//绘制矩形图片
imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $background_color); //填充
// imagefilledrectangle($im , 0, 0, $width-1 , $height-1 , $default_color); //填充
// imagerectangle($im , 0, 0, $width-1 , $height-1, $text_color); //边框
//添加文字
$text = isset($_HI['post']['text']) ? trim($_HI['post']['text']) : (isset($_HI['get']['text']) ? trim($_HI['get']['text']) : '');
$text = !($text) ? '' : html_entity_decode($text);
// imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text );
// 图片源字体尺寸文字方向0从左至右90从一至下x、y,首字符起点位置左下角坐标color颜色索引。使用负的颜色索引值具有关闭防锯齿的效果fontfile字体文件
// imagettftext($im,10,0,$width/2,$height/2,$text_color,"/fonts/STHeiti Medium.ttc","☆"); //中心位置标记
$strlen = mb_strlen($text); //字符数(中文算1个字符)
//BUG 180430 只输入一个字时部分情况报错01,`;无字符输入时报错
if ($strlen == 0) {
imagettftext($im, 10, 0, $width / 2 - 1, $height / 2 - 1, $default_color, "/fonts/STHeiti Medium.ttc", ""); //中心位置标记
} else {
//文字方向
$fd = isset($_HI['post']['fd']) ? intval($_HI['post']['fd']) : (isset($_HI['get']['fd']) ? intval($_HI['get']['fd']) : -1);
if ($fd == -1) {
if ($width < $height) {
$fd = 270;
} else {
$fd = 0;
}
}
$dstyle = 1; //文本框长度以矩形长为限
// $dstyle=2; //文本框长度以矩形宽为限
if ($dstyle == 1) {
$long = max($width, $height); //长
$short = min($width, $height); //短
$fd = ($width < $height) ? 270 : $fd;
} else {
$long = $width;
$short = $height; //短
}
//设置文字大小及位置、方向等
$size_l = round($long / $strlen, 2) - 0.01;
$size_l = intval($long / $strlen);
$size_s = round($short / 3, 2) - 0.01;
$size_s = intval($short / 3);
$size = min($size_l, $size_s); //每个字的框宽限制
$f_w = imagefontwidth(6);
$f_h = imagefontheight(6);
$fontsize = intval(($size - $f_w) / 1.2); //字体大小
$text_w = $f_w;
if ($strlen > 1) {
for ($i = 0; $i < $strlen; $i++) {
$t = mb_substr($text, $i, 1, 'utf-8');
$len = strlen($t);
if ($len == 3) {
$text_w += $fontsize + $f_w;
} else if ($len == 2) {
$text_w += $fontsize * 2 / 3;
} else if ($len == 1) {
$text_w += $fontsize * 2 / 3;
}
}
$text_w = $text_w - $f_w;
} else {
$text_w = $fontsize;
}
$p_l = $long / 2 - $text_w / 2;
$p_l = intval($p_l);
$p_s = $short / 2 + $fontsize / 2; //字符串底部-y轴高度
$p_s = intval($p_s);
if ($fd == 90) {
//90度时右下角位置
$x = $p_s - $f_h;
$y = $p_l + $text_w;
} else if ($fd == 270) {
//270度时左上角位置
$x = $short / 2 - $fontsize / 2;
$y = $p_l + $f_w;
} else {
//0度时左下角位置
$x = ($strlen == 1) ? $p_l - $f_w : $p_l - $fontsize / 2 + 2 * $f_w;
$y = $p_s - $f_h;
}
// var_dump('文本:'.$text);var_dump('字数:'.$strlen);var_dump('字框宽度:'.$size);var_dump('字体大小:'.$fontsize);var_dump('字体宽度:'.$f_w);var_dump('字体高度:'.$f_h);var_dump('文本框总宽度:'.$text_w);var_dump('字符X轴位置:'.$x);var_dump('字符Y轴位置:'.$y);var_dump('文字方向:'.$fd);die();
imagettftext($im, $fontsize, $fd, $x + 1, $y + 1, $default_color, "/fonts/Arial Unicode.ttf", $text);
imagettftext($im, $fontsize, $fd, $x, $y, $text_color, "/fonts/Arial Unicode.ttf", $text);
}
//生成图片
ob_clean();
// header("Content-type:image/jpeg;");
// imagejpeg($im,null,100);
header("Content-type:image/png;");
imagepng($im);
//在根目录下生成图片文件
// imagejpeg($im,"temp2.jpg",100);
// imagepng($im,"temp2.png");
//释放内存
imagedestroy($im);
}
/**
* RGB转 十六进制
* @param $rgb RGB颜色的字符串 如rgb(255,255,255);
* @return string 十六进制颜色值 如:#FFFFFF
*/
function RGB2Hex($rgb)
{
$regexp = "/^rgb\(([0-9]{0,3})\,\s*([0-9]{0,3})\,\s*([0-9]{0,3})\)/";
$re = preg_match($regexp, $rgb, $match);
$re = array_shift($match);
$hexColor = "#";
$hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
for ($i = 0; $i < 3; $i++) {
$r = NULL;
$c = $match[$i];
$hexAr = array();
while ($c > 16) {
$r = $c % 16;
$c = ($c / 16) >> 0;
array_push($hexAr, $hex[$r]);
}
array_push($hexAr, $hex[$c]);
$ret = array_reverse($hexAr);
$item = implode('', $ret);
$item = str_pad($item, 2, '0', STR_PAD_LEFT);
$hexColor .= $item;
}
return $hexColor;
}
/**
* 十六进制 转 RGB
*/
function Hex2RGB($hexColor)
{
$color = str_replace('#', '', $hexColor);
if (strlen($color) > 3) {
$rgb = array(
'r' => hexdec(substr($color, 0, 2)),
'g' => hexdec(substr($color, 2, 2)),
'b' => hexdec(substr($color, 4, 2))
);
} else {
$color = $hexColor;
$r = substr($color, 0, 1) . substr($color, 0, 1);
$g = substr($color, 1, 1) . substr($color, 1, 1);
$b = substr($color, 2, 1) . substr($color, 2, 1);
$rgb = array(
'r' => hexdec($r),
'g' => hexdec($g),
'b' => hexdec($b)
);
}
return $rgb;
}
/**
* 生成图片
* @param array 参数,包括图片和文字
* @param string $filename 生成海报文件名,不传此参数则不生成文件,直接输出图片
* @return [type] [description]
*/
function createPoster($config = array(), $filename = "")
{
//如果要看报什么错可以先注释调这个header
if (empty($filename)) header("content-type: image/png");
$imageDefault = array(
'left' => 0,
'top' => 0,
'right' => 0,
'bottom' => 0,
'width' => 100,
'height' => 100,
'opacity' => 100
);
$textDefault = array(
'text' => '',
'left' => 0,
'top' => 0,
'fontSize' => 32, //字号
'fontColor' => '255,255,255', //字体颜色
'angle' => 0,
);
$background = $config['background'];//海报最底层得背景
//背景方法
$backgroundInfo = getimagesize($background);
$backgroundFun = 'imagecreatefrom' . image_type_to_extension($backgroundInfo[2], FALSE);
$background = $backgroundFun($background);
$backgroundWidth = imagesx($background); //背景宽度
$backgroundHeight = imagesy($background); //背景高度
$imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
$color = imagecolorallocate($imageRes, 0, 0, 0);
imagefill($imageRes, 0, 0, $color);
// imageColorTransparent($imageRes, $color); //颜色透明
imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
//处理了图片
if (!empty($config['image'])) {
foreach ($config['image'] as $key => $val) {
$val = array_merge($imageDefault, $val);
$info = getimagesize($val['url']);
$function = 'imagecreatefrom' . image_type_to_extension($info[2], FALSE);
if ($val['stream']) { //如果传的是字符串图像流
$info = getimagesizefromstring($val['url']);
$function = 'imagecreatefromstring';
}
$res = $function($val['url']);
$resWidth = $info[0];
$resHeight = $info[1];
//建立画板 ,缩放图片至指定尺寸
$canvas = imagecreatetruecolor($val['width'], $val['height']);
imagefill($canvas, 0, 0, $color);
//关键函数参数目标资源目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h
imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
$val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
$val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
//放置图像
imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);//左,上,右,下,宽度,高度,透明度
}
}
//处理文字
if (!empty($config['text'])) {
foreach ($config['text'] as $key => $val) {
$val = array_merge($textDefault, $val);
list($R, $G, $B) = explode(',', $val['fontColor']);
$fontColor = imagecolorallocate($imageRes, $R, $G, $B);
$val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
$val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
}
}
//生成图片
if (!empty($filename)) {
$res = imagejpeg($imageRes, $filename, 90); //保存到本地
imagedestroy($imageRes);
if (!$res) return FALSE;
return $filename;
} else {
imagejpeg($imageRes); //在浏览器上显示
imagedestroy($imageRes);
}
}
}