当前位置:网站首页>PHP poster QR code synthesis
PHP poster QR code synthesis
2022-07-28 23:58:00 【Small cap I】
Combined parameter method :
/**
* [user_qrcode Parameter combination ]
* @param [type] $bgImg [ Background map ]
* @param [type] $codeImg [ QR code ]
* @param string $name [ name ]
* @param string $remask [ remarks ]
* @param string $content [ describe ]
* @param integer $key [ Generate a QR code name to represent ]
* @return [type] [description]
*/
public function user_qrcode($bgImg,$codeImg,$name=" I'm little Hongmao ",$remask=" Invite you to join XXX Surprise when buying ",$content=" Long press to identify or scan QR code to enter ",$key=0){
$config = array(
'text'=>array(
array(
'text'=>$name,
'left'=>250,
'top'=>815,
'fontPath'=>'/public/upload/ttf/simhei.ttf', // Font files need to be added to download and import
'fontSize'=>22, // Font size
'fontColor'=>'0,0,0', // The font color
'angle'=>0,
'text-aligin' => 'left'
),
array(
'text'=>$remask,
'left'=>250,
'top'=>860,
'fontPath'=>'/public/upload/ttf/simhei.ttf', // Font files need to be added to download and import
'fontSize'=>18, // Font size
'fontColor'=>'143,143,143', // The font color
'angle'=>0,
'text-aligin' => 'left'
),
array(
'text'=>'—— '.$content.' ——',
'left'=>0,
'right'=>0,
'top'=>970,
'fontPath'=>'/public/upload/ttf/simhei.ttf', // Font files need to be added to download and import
'fontSize'=>16, // Font size
'fontColor'=>'169,169,169', // The font color
'angle'=>0,
'text-aligin' => 'center' // The font is centered , Only center Effective , Nothing else
)
),
'image'=>array(
array(
'url'=>$codeImg, // Image resource path
'stream'=>0,
'left'=>50,
'top'=>740,
'right'=>0,
'bottom'=>0,
'width'=>180,
'height'=>180,
'opacity'=>100
),
),
'background'=>$bgImg,
);
$filename = "/public/upload/share_code/".time().rand(0000,9999).$num.".png";
return $this->createPoster($config,$filename);
}Poster synthesis 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]
*/
public 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) + 300; // Background height , Add... Here 300 Height , Bottom white area
$imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
$color = imagecolorallocate($imageRes, 255, 255, 255);
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);
if($val['text-aligin'] == 'center'){
$fontBox = imagettfbbox($val['fontSize'], 0, $val['fontPath'], $val['text']);// Get the required size of the text
// Center vertically and horizontally
//imagettftext($imageRes, $val['fontSize'], $val['angle'], ceil(($backgroundWidth - $fontBox[2]) / 2), ceil(($backgroundHeight - $fontBox[1] - $fontBox[7]) / 2), $fontColor, $val['fontPath'], $val['text']);
// Horizontal center
imagettftext($imageRes, $val['fontSize'], $val['angle'], ceil(($backgroundWidth - $fontBox[2]) / 2), $val['top'], $fontColor, $val['fontPath'], $val['text']);
}else{
$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 = imagepng($imageRes,ROOT_PATH.$filename); // Save to local
imagedestroy($imageRes);
if(!$res) return false;
return $filename;
}else{
imagepng ($imageRes); // Show... On the browser
imagedestroy($imageRes);
}
}design sketch :

边栏推荐
- Leetcode60. 排列序列
- CANoe应用案例之DoIP通信
- VMware VCSA 7.0 Install
- Leetcode63. 不同路径 II
- 【MySQL 8】Generated Invisible Primary Keys(GIPK)
- NPDP考试需要携带什么?文具携带说明
- Multi sensor fusion positioning (II) -- map based positioning
- 解决:Direct local .aar file dependencies are not supported when building an AAR.
- Leetcode64. 最小路径和
- Deep analysis of integrated learning gbdt
猜你喜欢

【C】atoi和offsetof的介绍和模拟实现

Multisensor fusion positioning (III) -- inertial technology

Hyperparametric optimization (grid search and Bayesian Optimization)

E-commerce data model design

Inspur clusterenginev4.0 remote command execution vulnerability cve-2020-21224

CANoe应用案例之DoIP通信

电商数据模型设计

Leetcode64. 最小路径和

Explanation of history and chemical properties of Worthington ribonuclease B

Jincang database kingbasees client programming interface guide ODBC (2. Overview)
随机推荐
AUTOCAD——Excel表格导入CAD、CAD合并两兄弟
VS2005透过SourceOffSite访问VSS2005的设置方法「建议收藏」
Compatibility description between kingbasees and Oracle (4. SQL)
Blocking queue
[data mining engineer - written examination] Dahua shares in 2022
电商数据模型设计
商家对积分体系运营的两个误解
Word中的\n是什么?:^p
Oracle创建表空间和用户
智能垃圾桶(七)——SG90舵机的介绍与使用(树莓派pico实现)
Leetcode59. 螺旋矩阵 II
Pagoda phpMyAdmin unauthorized access vulnerability
Kingbasees client programming interface guide ODBC (4. Create data source)
sql 左连接,内连接 的写法「建议收藏」
深度之眼(十八)——偏导数
实时数仓:美团基于Flink的实时数仓建设实施
酪氨酸脱羧酶丨Worthington粪链球菌酪氨酸脱羧酶的特征
Leetcode62. 不同路径
实时数仓:美团点评Flink的实时数仓应用分享
实时数仓:滴滴的实时数仓落地实践