php生成index,index.php

/*

描述:根据参数自动生成图像

访问:?参数=值&参数=值&...

参数:template=模板名称

crop=值(裁剪图像后显示的区域,根据模板,由 0 开始)

tcr=值(文本反色:0 否,1 是)

模板参数=值

*/

include 'functions.php';

/* 检测 User-Agent */

$do_check_ua = false; // 开关

/* 生成频率限制(全局)*/

$do_check_freq = false; // 开关

$freq_interval = 1; // 两次生成的时间间隔(秒)

/* 检测 */

if ($do_check_ua) {

checkUserAgent();

}

if ($do_check_freq) {

checkFreq();

}

/* 路径 */

$template_dir = 'templates/';

$template_json_dir = $template_dir . 'json/';

$template_image_dir = $template_dir . 'image/';

$template_font_dir = $template_dir . 'font/';

/* 参数:模板 */

$template_selected = $_GET['template'];

/* 处理 JSON */

// 模板 JSON 路径

$template_json = $template_json_dir . $template_selected . '.json';

// 读取 JSON

$template_json_content = file_get_contents($template_json) or die('模板不存在!');

// 解析 JSON

$template_json_data = json_decode($template_json_content, true);

// 图像是否具有 Alpha 通道(默认:否)

$gen_alpha = $template_json_data['alpha'] or $gen_alpha = false;

// 图像是否需要替换颜色(默认:否)

$gen_replace_color = $template_json_data['replace_color']['enable'] or $gen_replace_color = false;

// 图像是否需要裁剪(默认:否)

$gen_crop = $template_json_data['crop']['enable'] or $gen_crop = false;

// 背景图像

$gen_bg_file = $template_json_data['image'];

$gen_image = imagecreatefrompng($template_image_dir . $gen_bg_file);

// 字体

$gen_font_file = $template_json_data['font'];

$gen_font = $template_font_dir . $gen_font_file;

// 输出格式

$gen_format = $template_json_data['format'];

// 文本组数量

$count_text_group = $template_json_data['params']['count_text_group'];

// 数值数量

$count_value_item = $template_json_data['params']['count_value_item'];

/* 保存 Alpha 通道 */

if ($gen_alpha) {

// imagealphablending($gen_image, false);

imagesavealpha($gen_image, true);

}

/* 替换颜色 */

if ($gen_replace_color) {

// 截取 replace_color - colors 部分

$json_colors = $template_json_data['replace_color']['colors'];

// 需要替换的颜色数量

$count_replace_colors = sizeof($json_colors);

// 将真彩色图像转换为调色板图像

imagetruecolortopalette($gen_image, false, 256);

for ($color_item = 0; $color_item < $count_replace_colors; $color_item++) {

$get_color_x = $json_colors[$color_item]['get_x'];

$get_color_y = $json_colors[$color_item]['get_y'];

$get_replace = $json_colors[$color_item]['get_replace'];

$r_red = $json_colors[$color_item]['r_red'];

$r_green = $json_colors[$color_item]['r_green'];

$r_blue = $json_colors[$color_item]['r_blue'];

if ($get_replace == 1) {

// 使用 JSON 中的值

} elseif ($get_replace == 2) {

// 使用 URL 中的参数

$r_red = intval($_GET[$r_red]);

$r_green = intval($_GET[$r_green]);

$r_blue = intval($_GET[$r_blue]);

} else {

die('未正确设置替换颜色来源类型');

}

$color_index = imagecolorat($gen_image, $get_color_x, $get_color_y);

// 替换颜色

imagecolorset($gen_image, $color_index, $r_red, $r_green, $r_blue);

}

/* 透明色 */

if ($gen_alpha) {

// 获取透明色

$transparent_color = imagecolorat($gen_image, 10, 10);

// 十进制 RGB -> 十六进制颜色值

$c_red = dechex(($transparent_color >> 16) & 0xFF);

$c_green = dechex(($transparent_color >> 8) & 0xFF);

$c_blue = dechex($transparent_color & 0xFF);

// 十六进制颜色值 -> 十进制颜色值

$transparent_color = hexdec($c_red . $c_green . $c_blue);

// 设置图像的透明色

imagecolortransparent($gen_image, $transparent_color);

}

// 将调色板图像转换为真彩色图像

imagepalettetotruecolor($gen_image);

// 设定图像的混色模式

imagealphablending($gen_image, true);

}

// 是否将文本颜色设为反色(默认:否)

$text_color_reverse = intval($_GET['tcr']) or $text_color_reverse = 0;

/* 反色:图像 */

if ($text_color_reverse == 1) {

imagefilter($gen_image, IMG_FILTER_NEGATE);

}

/* 遍历 JSON 中的文本组 */

for ($group_num = 1; $group_num <= $count_text_group; $group_num++) {

// 组号(转为字符串)

$group_num_str = strval($group_num);

// 当前组文本的数量

$group_size = sizeof($template_json_data['params']['texts'][$group_num_str]);

// 截取 params - texts - 组号 部分

$json_text_group = $template_json_data['params']['texts'][$group_num_str];

// 遍历当前文本组中的项

for ($group_item = 0; $group_item < $group_size; $group_item++) {

$t_font_size = $json_text_group[$group_item]['font_size'];

$t_rotation = $json_text_group[$group_item]['rotation'];

$t_x = $json_text_group[$group_item]['x'];

$t_y = $json_text_group[$group_item]['y'];

$t_color = $json_text_group[$group_item]['color'];

$t_text_align_v = $json_text_group[$group_item]['text_align_v'];

$t_text_align_h = $json_text_group[$group_item]['text_align_h'];

$t_need_eval = $json_text_group[$group_item]['need_eval'];

// URL 文本参数

$param_text = $_GET['t' . $group_num_str];

// 是否:文本 -> 执行表达式后返回的结果

// SELF 为文本自身,VALUE_* 为 URL 中对应的参数 v*(v1、v2、...)

// 在 JSON 中的 count_value_item 设定数值参数数量

if ($t_need_eval) {

// 表达式

$t_expression = $json_text_group[$group_item]['expression'];

// 替换表达式中的关键词

$t_expression = str_replace('SELF', $param_text, $t_expression);

for ($value_item = 1; $value_item <= $count_value_item; $value_item++) {

// URL 数值参数

$param_value = $_GET['v' . $value_item];

$param_value = isInteger('integer', $param_value); // 校验 $param_value 是否为数值,否则为 0

$t_expression = str_replace('VALUE_' . $value_item, $param_value, $t_expression);

}

// eval()

$param_text = eval('return ' . $t_expression . ';');

}

// 文本框高度 & 文本宽度

$gen_text_box = imagettfbbox($t_font_size, $t_rotation, $gen_font, $param_text);

$gen_text_height = $gen_text_box[7] - $gen_text_box[1];

$gen_text_width = $gen_text_box[2] - $gen_text_box[0];

// 文本垂直位置(居上、居中、居下)

if ($t_text_align_v == 'center') {

// y 为文字中心距离图像顶部距离

// y = 原y - 字符串高度 / 2

$t_y = $t_y - $gen_text_height / 2;

} elseif ($t_text_align_v == 'bottom') {

// y 为文字底部距离图像顶部距离

// y = 原y - 字符串高度

$t_y = $t_y - $gen_text_height;

}

// 文本水平位置(居左、居中、居右)

if ($t_text_align_h == 'right') {

// x 为文字右侧距离图像左侧距离;x = 原x - 字符串宽度

$t_x = $t_x - $gen_text_width;

} else if ($t_text_align_h == 'center') {

// x 为文字中心距离图像左侧距离;x = 原x - 字符串宽度 / 2

$t_x = $t_x - $gen_text_width / 2;

}

// 生成文本

imagettftext($gen_image, $t_font_size, $t_rotation, $t_x, $t_y, hexdec($t_color), $gen_font, $param_text);

}

}

/* 反色:恢复 */

if ($text_color_reverse == 1) {

imagefilter($gen_image, IMG_FILTER_NEGATE);

}

/* 裁剪 */

if ($gen_crop) {

// 裁剪后显示的区域(默认:0)

$sel_area = intval($_GET['crop']) or $sel_area = 0;

// 源图:宽度、高度

$src_info = getimagesize($template_image_dir . $gen_bg_file);

$width_src = $src_info[0];

$height_src = $src_info[1];

// 裁剪区域

$crop_areas = $template_json_data['crop']['areas'];

// 裁剪区域数量

$count_crop_areas = sizeof($crop_areas);

// 判断选择的区域是否有效

if ($sel_area < $count_crop_areas) {

// 裁剪起点

$crop_start_x = $crop_areas[$sel_area]['start_x'];

$crop_start_y = $crop_areas[$sel_area]['start_y'];

// 裁剪后:宽度、高度

$crop_width = $crop_areas[$sel_area]['width'];

$crop_height = $crop_areas[$sel_area]['height'];

$gen_image_crop = imagecreatetruecolor($crop_width, $crop_height);

// 当前透明色

$color_transparent = colorDecToRGB(imagecolortransparent($gen_image));

// 生成透明背景图

$color_fill = imagecolorallocate($gen_image_crop, $color_transparent[0], $color_transparent[1], $color_transparent[2]);

imagecolortransparent($gen_image_crop, $color_fill);

imagefill($gen_image_crop, 0, 0, $color_fill);

// 裁剪

// int $dst_x, int $dst_y, int $src_x, int $src_y

imagecopyresampled($gen_image_crop, $gen_image, 0, 0, $crop_start_x, $crop_start_y, $crop_width, $crop_height, $crop_width, $crop_height);

$gen_image = $gen_image_crop;

} else {

die('裁剪区域无效');

}

}

/* 输出的格式 */

if ($gen_format == 'PNG') {

header('Content-Type: image/png');

imagepng($gen_image);

} elseif ($gen_format == 'GIF') {

header('Content-Type: image/gif');

imagegif($gen_image);

} elseif ($gen_format == "TEXT") {

header('Content-Type: text/html');

imagepng($gen_image);

} else {

die('未在模板中正确设置输出格式');

}

/* 销毁 */

imagedestroy($gen_image);

一键复制

编辑

Web IDE

原始数据

按行查看

历史

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值