这是我必须使用的代码:
// Start DRAWING LOGO IN QRCODE
$QR = imagecreatefrompng($filepath);
// START TO DRAW THE IMAGE ON THE QR CODE
$logo = imagecreatefromstring(file_get_contents($logopath));
imagecolortransparent($logo , imagecolorallocatealpha($logo , 0, 0, 0, 127));
imagealphablending($logo , false);
imagesavealpha($logo , true);
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
// Scale logo to fit in the QR Code
$logo_qr_width = $QR_width/3;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
imagecopyresampled($QR, $logo, $QR_width/3, $QR_height/3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
// Save QR code again, but with logo on it
imagepng($QR,$filepath);
在QRCode图像的中心添加了徽标,但是透明区域填充了白色。
任何人都可以检查并更新给我。
答案 0 :(得分:1)
我对问题采取了稍微不同的方法,之前曾遇到过一两次问题。下面将创建一个新的透明PNG图像,在其上添加QR码和徽标。
<?php
/*
to output directly to the browser set as false,
to save set as true
*/
$save=false;
$filepath='c:/wwwroot/images/qrcodes/4e9550be3c5ce4649ef00a70c9d6fb92bda09752.png';
$logopath='c:/wwwroot/images/ict_cmyk_jigsaw_1.png';
$source = imagecreatefrompng( $filepath ); # QR-Code
$logo = imagecreatefrompng( $logopath ); # Overlay
$sw = intval( imagesx( $source ) );
$sh = intval( imagesy( $source ) );
$lw = intval( imagesx( $logo ) );
$lh = intval( imagesy( $logo ) );
/* Create a new image onto which we will copy images & assign transparency */
$target = imagecreatetruecolor( $sw, $sh );
imagesavealpha( $target , true );
/* common divisor for overlay image size calculations */
$divisor = 3;
/* image size calculations */
$clw = $sw / $divisor; # calculated width
$scale = $lw / $clw; # calculated ratio
$clh = $lh / $scale; # calculated height
/* allocate a transparent colour for the new image */
$transparent = imagecolorallocatealpha( $target, 0, 0, 0, 127 );
imagefill( $target,0, 0, $transparent );
/* copy the QR-Code to the new image */
imagecopy( $target, $source, 0, 0, 0, 0, $sw, $sh );
/* Determine position of overlay image using divisor */
$px=$sw/$divisor;
$py=$sh/$divisor;
/* add the overlay */
imagecopyresampled( $target, $logo, $px, $py, 0, 0, $clw, $clh, $lw, $lh );
/* output or save image */
header( 'Content-Type: image/png' );
imagepng( $target, $save ? $filepath : null );
/* clean up */
imagedestroy( $target );
imagedestroy( $source );
imagedestroy( $logo );
?>