<?phprequire_once dirname(
__FILE__
).
"/qrcode/qrlib.php"
;
class
PHPQRCode{
private
$_config
=
array
( 'ecc' => 'H',
'size' => 15,
'dest_file' => 'qrcode.png',
'quality' => 100,
'logo' => '',
'logo_size' => null,
'logo_outline_size' => null,
'logo_outline_color' => '#FFFFFF',
'logo_opacity' => 100,
'logo_radius' => 0,
);
public
function
set_config(
$config
){
$config_keys
=
array_keys
(
$this
->_config);
foreach
(
$config_keys
as
$k
=>
$v
){
if
(isset(
$config
[
$v
])){
$this
->_config[
$v
] =
$config
[
$v
];
}
}
}
public
function
generate(
$data
){
$tmp_qrcode_file
=
$this
->create_qrcode(
$data
);
$this
->add_logo(
$tmp_qrcode_file
);
if
(
$tmp_qrcode_file
!='' &&
file_exists
(
$tmp_qrcode_file
)){
unlink(
$tmp_qrcode_file
);
}
return
file_exists
(
$this
->_config['dest_file'])?
$this
->_config['dest_file'] : '';
}
private
function
create_qrcode(
$data
){
$tmp_qrcode_file
= dirname(
__FILE__
).'/tmp_qrcode_'.time().mt_rand(100,999).'.png';
QRcode::png(
$data
,
$tmp_qrcode_file
,
$this
->_config['ecc'],
$this
->_config['size'], 2);
return
file_exists
(
$tmp_qrcode_file
)?
$tmp_qrcode_file
: '';
}
private
function
add_logo(
$tmp_qrcode_file
){
$this
->create_dirs(dirname(
$this
->_config['dest_file']));
$dest_ext
=
$this
->get_file_ext(
$this
->_config['dest_file']);
if
(
file_exists
(
$this
->_config['logo'])){
$tmp_qrcode_img
= imagecreatefrompng(
$tmp_qrcode_file
);
list(
$qrcode_w
,
$qrcode_h
,
$qrcode_type
) =
getimagesize
(
$tmp_qrcode_file
);
list(
$logo_w
,
$logo_h
,
$logo_type
) =
getimagesize
(
$this
->_config['logo']);
switch
(
$logo_type
){
case
1:
$logo_img
= imagecreatefromgif(
$this
->_config['logo']);
break
;
case
2:
$logo_img
= imagecreatefromjpeg(
$this
->_config['logo']);
break
;
case
3:
$logo_img
= imagecreatefrompng(
$this
->_config['logo']);
break
;
default
:
return
'';
}
$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);
$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
);
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
);
}
if
(
$this
->_config['logo_radius']>0){
$new_logo_img
=
$this
->image_fillet(
$new_logo_img
);
}
$pos_x
= (
$qrcode_w
-
$new_logo_w
)/2;
$pos_y
= (
$qrcode_h
-
$new_logo_h
)/2;
imagealphablending(
$tmp_qrcode_img
, true);
$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']);
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
;
}
}
else
{
$dest_img
= imagecreatefrompng(
$tmp_qrcode_file
);
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
;
}
}
}
private
function
image_outline(
$img
){
$img_w
= imagesx(
$img
);
$img_h
= imagesy(
$img
);
$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);
$bg_img
= imagecreatetruecolor(
$bg_w
,
$bg_h
);
$rgb
=
$this
->hex2rgb(
$this
->_config['logo_outline_color']);
$bgcolor
= imagecolorallocate(
$bg_img
,
$rgb
['r'],
$rgb
['g'],
$rgb
['b']);
imagefill(
$bg_img
, 0, 0,
$bgcolor
);
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
);
}
private
function
image_fillet(
$img
){
$img_w
= imagesx(
$img
);
$img_h
= imagesy(
$img
);
$new_img
= imagecreatetruecolor(
$img_w
,
$img_h
);
imagesavealpha(
$new_img
, true);
$bg
= imagecolorallocatealpha(
$new_img
, 255, 255, 255, 127);
imagefill(
$new_img
, 0, 0,
$bg
);
$r
=
$this
->_config['logo_radius'];
for
(
$x
=0;
$x
<
$img_w
;
$x
++){
for
(
$y
=0;
$y
<
$img_h
;
$y
++){
$rgb
= imagecolorat(
$img
,
$x
,
$y
);
if
((
$x
>=
$r
&&
$x
<=(
$img_w
-
$r
)) || (
$y
>=
$r
&&
$y
<=(
$img_h
-
$r
))){
imagesetpixel(
$new_img
,
$x
,
$y
,
$rgb
);
}
else
{
$ox
=
$r
;
$oy
=
$r
;
if
( ( (
$x
-
$ox
)*(
$x
-
$ox
) + (
$y
-
$oy
)*(
$y
-
$oy
) ) <= (
$r
*
$r
) ){
imagesetpixel(
$new_img
,
$x
,
$y
,
$rgb
);
}
$ox
=
$img_w
-
$r
;
$oy
=
$r
;
if
( ( (
$x
-
$ox
)*(
$x
-
$ox
) + (
$y
-
$oy
)*(
$y
-
$oy
) ) <= (
$r
*
$r
) ){
imagesetpixel(
$new_img
,
$x
,
$y
,
$rgb
);
}
$ox
=
$r
;
$oy
=
$img_h
-
$r
;
if
( ( (
$x
-
$ox
)*(
$x
-
$ox
) + (
$y
-
$oy
)*(
$y
-
$oy
) ) <= (
$r
*
$r
) ){
imagesetpixel(
$new_img
,
$x
,
$y
,
$rgb
);
}
$ox
=
$img_w
-
$r
;
$oy
=
$img_h
-
$r
;
if
( ( (
$x
-
$ox
)*(
$x
-
$ox
) + (
$y
-
$oy
)*(
$y
-
$oy
) ) <= (
$r
*
$r
) ){
imagesetpixel(
$new_img
,
$x
,
$y
,
$rgb
);
}
}
}
}
return
$new_img
;
}
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
;
}
private
function
create_dirs(
$path
){
if
(!
is_dir
(
$path
)){
return
mkdir
(
$path
, 0777, true);
}
return
true;
}
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
;
}
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
;
}
}