当前位置:网站首页>PHP generate Poster
PHP generate Poster
2022-07-05 13:42:00 【Black hearted green rose】
One 、 Method
**
* Generate posters
* @param array Parameters , Including pictures and words
* @param string $filename Generate poster file name , If this parameter is not passed, no file will be generated , Direct output picture
* @return [type] [description]
*/
function createPoster($config=array(),$filename=""){
// If there's anything wrong with reading the newspaper , You can note this first 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, // Font size
'fontColor'=>'255,255,255', // The font color
'angle'=>0,
);
$background = $config['background'];// The bottom background of the poster
// Background method
$backgroundInfo = getimagesize($background);
$backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
$background = $backgroundFun($background);
$backgroundWidth = imagesx($background); // Background width
$backgroundHeight = imagesy($background); // Background height
$imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
$color = imagecolorallocate($imageRes, 0, 0, 0);
imagefill($imageRes, 0, 0, $color);
// imageColorTransparent($imageRes, $color); // The color is transparent
imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
// Processed pictures
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']){ // If it is a string image stream
$info = getimagesizefromstring($val['url']);
$function = 'imagecreatefromstring';
}
$res = $function($val['url']);
$resWidth = $info[0];
$resHeight = $info[1];
// Create Sketchpad , Zoom the picture to the specified size
$canvas=imagecreatetruecolor($val['width'], $val['height']);
imagefill($canvas, 0, 0, $color);
// The key function , Parameters ( Target resources , Source , The starting coordinate of the target resource x,y, The starting coordinate of the source resource x,y, The width and height of the target resource w,h, The width and height of the source resource 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'];
// Place image
imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);// Left , On , Right , Next , Width , Height , transparency
}
}
// Working with text
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']);
}
}
// Generate pictures
if(!empty($filename)){
$res = imagejpeg ($imageRes,$filename,90); // Save to local
imagedestroy($imageRes);
if(!$res) return false;
return $filename;
}else{
imagejpeg ($imageRes); // Show... On the browser
imagedestroy($imageRes);
}
}
Two 、 Usage method
1. Generate a picture with QR code
$config = array(
'image'=>array(
array(
'url'=>'qrcode/qrcode.png', // QR code resources
'stream'=>0,
'left'=>116,
'top'=>-216,
'right'=>0,
'bottom'=>0,
'width'=>178,
'height'=>178,
'opacity'=>100
)
),
'background'=>'bg/bg1.jpg' // Background map
);
$filename = 'bg/'.time().'.jpg';
//echo createPoster($config,$filename);
echo createPoster($config);
2. Generate multi content posters
$config = array(
'text'=>array(
array(
'text'=>' nickname ',
'left'=>182,
'top'=>105,
'fontPath'=>'qrcode/simhei.ttf', // The font file
'fontSize'=>18, // Font size
'fontColor'=>'255,0,0', // The font color
'angle'=>0,
)
),
'image'=>array(
array(
'url'=>'qrcode/qrcode.png', // Image resource path
'left'=>130,
'top'=>-140,
'stream'=>0, // Whether the picture resource is a string image stream
'right'=>0,
'bottom'=>0,
'width'=>150,
'height'=>150,
'opacity'=>100
),
array(
'url'=>'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83eofD96opK97RXwM179G9IJytIgqXod8jH9icFf6Cia6sJ0fxeILLMLf0dVviaF3SnibxtrFaVO3c8Ria2w/0',
'left'=>120,
'top'=>70,
'right'=>0,
'stream'=>0,
'bottom'=>0,
'width'=>55,
'height'=>55,
'opacity'=>100
),
),
'background'=>'qrcode/bjim.jpg',
);
$filename = 'qrcode/'.time().'.jpg';
//echo createPoster($config,$filename);
echo createPoster($config);
边栏推荐
- 龙芯派2代烧写PMON和重装系统
- FPGA learning notes: vivado 2019.1 add IP MicroBlaze
- stm32逆向入门
- 【每日一题】1200. 最小绝对差
- MySQL get time
- Win10——轻量级小工具
- Record in-depth learning - some bug handling
- 49. Grouping of alphabetic ectopic words: give you a string array, please combine the alphabetic ectopic words together. You can return a list of results in any order. An alphabetic ectopic word is a
- Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
- 记录一下在深度学习-一些bug处理
猜你喜欢
When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
真正的缓存之王,Google Guava 只是弟弟
这18个网站能让你的页面背景炫酷起来
私有地址有那些
zabbix 监控
Huawei push service content, read notes
搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑
山东大学暑期实训一20220620
FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
随机推荐
Network security HSRP protocol
jasypt配置文件加密|快速入门|实战
记录一下在深度学习-一些bug处理
PostgreSQL Usage Summary (PIT)
49. 字母异位词分组:给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
Wonderful express | Tencent cloud database June issue
Go array and slice
53. Maximum subarray sum: give you an integer array num, please find a continuous subarray with the maximum sum (the subarray contains at least one element) and return its maximum sum.
Catch all asynchronous artifact completable future
运筹说 第68期|2022年最新影响因子正式发布 快看管科领域期刊的变化
如何把大的‘tar‘存档文件分割成特定大小的多个文件
Idea设置方法注释和类注释
go map
MATLAB论文图表标准格式输出(干货)
Laravel framework operation error: no application encryption key has been specified
Zhubo Huangyu: it's really bad not to understand these gold frying skills
Cloudcompare - point cloud slice
Idea set method annotation and class annotation
Go pointer
那些考研后才知道的事