为什么80%的码农都做不了架构师?>>>
今天产品提出要实现二维码圆角功能 为配合功能实现 特此记录
1.使用laravel框架生成二维码 安装类库参见地址 http://laravelacademy.org/post/2605.html
QrCode::format('png')->size(520)->generate($url,$path);
2.需要把如下两张图片放在一起
/*
* ttf 是字体文件路径
* qrcodePath是二维码路径
* qrcodeComplePath 是合成后二维码的保存路径
*area_name和table_name 是项目在二维码空白处要写的内容
*/
private function _mergeImage($ttf,$qrcodePath,$qrcodeCompletePath,$area_name,$table_name){
// $area_name = "大厅";
// $table_name = "200";
$font_size = 28;
$max_width = 827;
$image = imagecreatefromPng('http://cpx-oss.oss-cn-beijing.aliyuncs.com/dish_img/1588/c8c71c7104184ff3f7c92555c4e218e5.png');
$white=ImageColorAllocate($image, 255, 255, 255);
$black=ImageColorAllocate($image, 0, 0, 0);
$red=ImageColorAllocate($image, 255, 0, 0);
$green=ImageColorAllocate($image, 0, 255, 0);
$blue=ImageColorAllocate($image, 0, 0, 255);
$head = ImageColorAllocate($image, 138, 138, 138);
//二维码
$src_im = imagecreatefrompng($qrcodePath);
imagecopymerge($image,$src_im,190,85,30,30,430,430,100);
//桌台信息
$array = imagettfbbox($font_size, 0, $ttf, "{$area_name}:{$table_name}");
$length = $array[4] - $array[6];
// $string = "{$area_name}:{$table_name}";
if($length > ($max_width-200)){
$array = imagettfbbox($font_size, 0, $ttf, $area_name);
// $area_height = $array[3] - $array[5];
$area_width = $array[4] - $array[6];
$array = imagettfbbox($font_size, 0, $ttf, $table_name);
// $table_height = $array[3] - $array[5];
$table_width = $array[4] - $array[6];
$start1 = ceil(($max_width - $area_width) /2);
$start2 = ceil(($max_width - $table_width) /2);
imagettftext($image,$font_size,0,$start1,530,$black,$ttf,"{$area_name}");
imagettftext($image,$font_size,0,$start2,630,$black,$ttf,"{$table_name}");
}else{
$start = ceil((($max_width - $length))/2);
imagettftext($image,$font_size,0,$start,580,$black,$ttf,"{$area_name}:{$table_name}");
}
imagePng($image,$qrcodeCompletePath);
imageDestroy($image);
$image_r = $this->radius_img($qrcodeCompletePath, $radius = 45);
// header("Content-type:image/png");
imagePng($image_r,$qrcodeCompletePath);
// imagePng($image_r);
imageDestroy($image_r);
// die;
}
3.使用这种方式生成二维码时发布创建的背景的圆角没有了 如下图 为了使用整张图片具有圆角效果 调用了自定义的radius_img函数 实现的圆角效果 圆角代码
public function radius_img($imgpath = './t.png', $radius = 15) {
$ext = pathinfo($imgpath);
$src_img = null;
switch ($ext['extension']) {
case 'jpg':
$src_img = imagecreatefromjpeg($imgpath);
break;
case 'png':
$src_img = imagecreatefrompng($imgpath);
break;
}
$wh = getimagesize($imgpath);
$w = $wh[0];
$h = $wh[1];
// $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $radius; //圆 角半径
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {
//不在四角的范围内,直接画
imagesetpixel($img, $x, $y, $rgbColor);
} else {
//在四角的范围内选择画
//上左
$y_x = $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
//上右
$y_x = $w - $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
//下左
$y_x = $r; //圆心X坐标
$y_y = $h - $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
//下右
$y_x = $w - $r; //圆心X坐标
$y_y = $h - $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
}
return $img;
}
4.整体代码下来能实现所要的效果如下
虽然实现的效果 但是实现过程很有优化的空间 今天先记录一下 后续优化完成补充