This article mainly introduces PHP implementation to create two-dimensional code class, support set size, add logo, stroke, fillet, transparency, and other processing. Provide complete code, demonstration examples and detailed parameter description, convenient for everyone to learn to use. Hope to help everyone.
The implementation functions are as follows:
1. Create two-dimensional code
2. Add logo to QR code
3.logo can be stroked
4.logo rounded Corners
5.logo can be set to transparency
6.logo image and output image type support Png,jpg,gif format
7. Can set the output picture quality
Set parameter Description:
ECC
QR code quality l-smallest, M, Q, h-best
size
QR Code size 1-50
Dest_file
Generated QR code picture path
Quality
The resulting picture quality
logo
Logo path, empty means not to join the logo
logo_size
Logo size, null means automatic calculation by two-dimensional code size ratio
logo_outline_size
Logo stroke size, null means automatic calculation by logo size
Logo_outline_color
Logo stroke Color
logo_opacity
Logo opacity 0-100
Logo_radius
Logo fillet Angle 0-30
The code is as follows:
PHPQRCode.class.php
<?phprequire_once dirname (__file__). " /qrcode/qrlib.php ";/** * php Create two-dimensional code class * date:2018-03-18 * author:fdipzone * version:1.0 * * Description: * PHP Implementation to create two-dimensional code class , support to set the size, add logo, fillet, transparency, and other processing. * Func: * Public set_config Set Configuration * Public Generate create QR code * Private Create_qrcode Create a pure QR code picture * Private Add_logo Pure Two-dimensional code image and logo image * Private image_outline Picture object to stroke * Private Image_fillet picture pair Like fillet processing * Private imagecopymerge_alpha collage picture and keep the respective transparency * Private Create_dirs Create directory * Private Hex2rgb hex Color to RGB color * Private get_file_ext get picture type */class phpqrcode{//class start/** default settings */private $_config = Arra Y (' ecc ' = ' H ',//two D code quality l-smallest, M, Q, h-best ' size ' = 15, QR code size 1-50 ' dest_file ' = ' qrcode.png ',//Create a QR code path ' quality ' + 100, Image quality ' logo ' = ', '//logo path, empty means no logo ' logo_size ' + NULL,//logo size, null means automatic calculation of ' logo_outline_size ' by two-dimensional code size ratio ' = null ',/ /logo stroke size, null means automatically calculated according to logo size ' logo_outline_color ' + ' #FFFFFF ',//logo stroke color ' logo_opacity ' = 100, Logo opacity 0-100 ' logo_radius ' = 0,//logo fillet angle 0-30); /** * Config configuration * @param Array $config Configuration content */Public Function Set_config ($config) {//Allow settings configuration $config _keys = Array_keys ($this->_config); Get the incoming configuration, write the settings foreach ($config _keys as $k + = $v) {if (Isset ($config [$v])) {$this->_co nfig[$v] = $config [$v]; }}}/** * create QR code * @param string $data qr Code contents * @return String */Public function Generat E ($data) {//create temporary QR code picture $tmp _qrcode_file = $this->create_qrcode ($data); Combined temporary QR code image and logo image $this->add_logo ($tmp _qrcode_file); Delete temporary QR code picture if ($tmp _QRCOde_file!= ' && file_exists ($tmp _qrcode_file)) {unlink ($tmp _qrcode_file); } return file_exists ($this->_config[' Dest_file ')? $this->_config[' dest_file ': '; /** * Create temporary QR code picture * @param string $data qr Code contents * @return String */Private function Create_qrcode ($d ATA) {//temporary QR code picture $tmp _qrcode_file = dirname (__file__). ' /tmp_qrcode_ '. Time (). Mt_rand (100,999). PNG '; Create temporary QR code qrcode::p ng ($data, $tmp _qrcode_file, $this->_config[' ECC '), $this->_config[' size '], 2); Return temporary QR code path return file_exists ($tmp _qrcode_file)? $tmp _qrcode_file: "; }/** * Combined temporary QR code image and logo image * @param String $tmp _qrcode_file temporary QR code image */Private Function Add_logo ($tmp _qrco De_file) {//Create target folder $this->create_dirs (dirname ($this->_config[' dest_file ')); Gets the type of the target picture $dest _ext = $this->get_file_ext ($this->_config[' dest_file '); Need to add logo if (file_exIsts ($this->_config[' logo ')) {//create temporary QR code Picture object $tmp _qrcode_img = imagecreatefrompng ($tmp _qrcode_fi Le); Get temporary QR code picture size list ($qrcode _w, $qrcode _h, $qrcode _type) = getimagesize ($tmp _qrcode_file); Get logo image size and type list ($logo _w, $logo _h, $logo _type) = getimagesize ($this->_config[' logo '); Create logo Image Object switch ($logo _type) {Case 1: $logo _img = imagecreatefromgif ($this->_config[' lo Go ']); Break Case 2: $logo _img = imagecreatefromjpeg ($this->_config[' logo '); Break Case 3: $logo _img = imagecreatefrompng ($this->_config[' logo '); Break Default:return "; }//Set logo image to fit the size, no setting is automatically calculated proportionally $new _logo_w = isset ($this->_config[' logo_size ')? $this->_config[' logo_size ': (int) ($qrcode _W/5); $new _logo_h = isset ($this->_config[' logo_size ')? $this->_config[' logo_size ': (int) ($qRCODE_H/5); Adjust the logo image according to the set size $new _logo_img = Imagecreatetruecolor ($new _logo_w, $new _logo_h); Imagecopyresampled ($new _logo_img, $logo _img, 0, 0, 0, 0, $new _logo_w, $new _logo_h, $logo _w, $logo _h); Determine if you need a stroke if (!isset ($this->_config[' logo_outline_size ')) | | $this->_config[' logo_outline_size ']>0) { List ($new _logo_img, $new _logo_w, $new _logo_h) = $this->image_outline ($new _logo_img); }//Determine if fillet processing is required if ($this->_config[' Logo_radius ']>0) {$new _logo_img = $this Image_fillet ($new _logo_img); }//Combined logo with temporary QR code $pos _x = ($qrcode _w-$new _logo_w)/2; $pos _y = ($qrcode _h-$new _logo_h)/2; Imagealphablending ($tmp _qrcode_img, true); To fit the picture and retain their transparency $dest _img = $this->imagecopymerge_alpha ($tmp _qrcode_img, $new _logo_img, $pos _x, $pos _y, 0, 0 , $new _logo_w, $new _logo_h, $this->_config[' logo_oPacity ']); Generate picture switch ($dest _ext) {case 1:imagegif ($dest _img, $this->_config[' Dest_file '], $this-> ; _config[' quality '); Break Case 2:imagejpeg ($dest _img, $this->_config[' Dest_file '), $this->_config[' quality ']); Break Case 3:imagepng ($dest _img, $this->_config[' Dest_file '), (int) (($this->_config[' quality ']-1)/10)); Break }//Do not need to add logo}else{$dest _img = imagecreatefrompng ($tmp _qrcode_file); Generate picture switch ($dest _ext) {case 1:imagegif ($dest _img, $this->_config[' Dest_file '], $this-> ; _config[' quality '); Break Case 2:imagejpeg ($dest _img, $this->_config[' Dest_file '), $this->_config[' quality ']); Break Case 3:imagepng ($dest _img, $this->_config[' Dest_file '), (int) (($this->_config[' quality ']-1)/10)); Break }}}/** * Stroke the Picture object * @param Obj $img Picture Object * @return Array */Private Function Image_outline ($img) {//Get picture width high $img _w = Imagesx ($img); $img _h = Imagesy ($img); Calculate stroke size, without setting, automatically calculate proportionally $bg _w = isset ($this->_config[' logo_outline_size ')? Intval ($img _w + $this->_config[' logo_outline_size '): $img _w + (int) ($img _W/5); $BG _h = isset ($this->_config[' logo_outline_size ')? Intval ($img _h + $this->_config[' logo_outline_size '): $img _h + (int) ($img _H/5); Create a Basemap object $bg _img = Imagecreatetruecolor ($bg _w, $BG _h); Set the basemap color $rgb = $this->hex2rgb ($this->_config[' Logo_outline_color '); $bgcolor = Imagecolorallocate ($bg _img, $rgb [' R '], $rgb [' G '], $rgb [' B ']); Fill Basemap color Imagefill ($BG _img, 0, 0, $bgcolor); Combined picture and basemap, achieve stroke effect imagecopy ($BG _img, $img, (int) (($bg _w-$img _w)/2), (int) (($bg _h-$img _h)/2), 0, 0, $img _w, $img _h); $img = $BG _img; Return Array ($img, $BG _w, $BG _h); }/** * Fillet a Picture object * @param obj $img Picture Object * @return obj */Private Function Image_fillet ($img) {//Get picture height $img _ W = imagesx ($img); $img _h = Imagesy ($img); Create rounded Picture object $new _img = Imagecreatetruecolor ($img _w, $img _h); Save Transparent channel Imagesavealpha ($new _img, true); Fill the fillet picture $bg = Imagecolorallocatealpha ($new _img, 255, 255, 255, 127); Imagefill ($new _img, 0, 0, $BG); Fillet radius $r = $this->_config[' Logo_radius '); Perform fillet processing for ($x =0; $x < $img _w; $x + +) {for ($y =0; $y < $img _h; $y + +) {$rgb = Imagecolor At ($img, $x, $y); Not in the corners of the image, draw directly if ($x >= $r && $x <= ($img _w-$r)) | | ($y >= $r && $y <= ($img _h-$r)) {Imagesetpixel ($new _img, $x, $y, $rgb); In the corner of the picture, select Paint}else{//top Left $ox = $r;//center X coordinate $o y = $r; Center Y coordinate if (($x-$ox) * ($x-$ox) + ($y-$oy) * ($y-$oy)) <= ($r * $r)) {Imagesetpixel ($new _img, $x, $y, $RGB); }//upper right $ox = $img _w-$r; Center x coordinate $oy = $r; Center Y Coordinate if ((($x-$ox) * ($x-$ox) + ($y-$oy) * ($y-$oy)) <= ($r * $r)) {ImageSet Pixel ($new _img, $x, $y, $rgb); }//lower Left $ox = $r; Center x Coordinate $oy = $img _h-$r; Center Y Coordinate if ((($x-$ox) * ($x-$ox) + ($y-$oy) * ($y-$oy)) <= ($r * $r)) {ImageSet Pixel ($new _img, $x, $y, $rgb); }//Down Right $ox = $img _w-$r; Center x Coordinate $oy = $img _h-$r; Center Y Coordinate if ((($x-$ox) * ($x-$ox) + ($y-$oy) * ($y-$oy)) <= ($r * $r)) {ImageSet Pixel ($new _img, $x, $y, $rgb); } }}} return $new _img; }//collage pictures and keep their transparency private function imagecopymerge_alpha ($dest _img, $src _img, $pos _x, $pos _y, $src _x, $src _y, $src _w, $src _h, $opacity) {$w = Imagesx ($src _img); $h = Imagesy ($src _img); $tmp _img = Imagecreatetruecolor ($src _w, $src _h); Imagecopy ($tmp _img, $dest _img, 0, 0, $pos _x, $pos _y, $src _w, $src _h); Imagecopy ($tmp _img, $src _img, 0, 0, $src _x, $src _y, $src _w, $src _h); Imagecopymerge ($dest _img, $tmp _img, $pos _x, $pos _y, $src _x, $src _y, $src _w, $src _h, $opacity); return $dest _img; }/** * Create directory * @param String $path * @return Boolean */Private Function Create_dirs ($path) { if (!is_dir ($path)) {return mkdir ($path, 0777, true); } return true; }/** hex color to RGB color * @param String $color hex Color * @return Array */Private 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 {$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; /** Gets the picture type * @param String $file picture path * @return int */Private Function Get_file_ext ($file) { $filename = basename ($file); List ($name, $ext) = Explode ('. ', $filename); $ext _type = 0; Switch (Strtolower ($ext)) {case ' jpg ': case ' jpeg ': $ext _type = 2; Break Case ' gif ': $ext _type = 1; Break Case ' png ': $ext _type = 3; Break } return $ext _type; }}//Class end?>
demo.php
<?phprequire ' PHPQRCode.class.php '; $config = array ( ' ecc ' = ' H ', //L-smallest, M, Q, h-best ' size ' = >, //1-50 ' dest_file ' = ' qrcode.png ', ' quality ', ' + ', ' logo ' = ' logo.jpg ', ' Logo_size ' = +, ' logo_outline_size ' = ', ' logo_outline_color ' and ' #FFFF00 ', ' Logo_ Radius ' = +, ' logo_opacity ' + 100, '///QR code content $data = ' http://weibo.com/fdipzone ';//Create two-dimensional code class $ophpqrcode = new Phpqrcode ();//Set Configuration $ophpqrcode->set_config ($config);//create QR code $qrcode = $oPHPQRCode->generate ($data);// Show QR code echo ' ';? >
Related recommendations:
(Advanced article) PHP generation with logo Two-dimensional code method summary