[英]How to add transparent logo in QRCode image
Here is the code I have to use: 这是我必须使用的代码:
// 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);
Logo added in the center of the QRCode image, but transparent area filled with white color. 在QRCode图像的中心添加了徽标,但是透明区域填充了白色。
Could anyone please check and update to me. 任何人都可以检查并更新给我。
I took a slightly different approach to the problem having run into issues once or twice before. 对于之前遇到过一两次的问题,我采取了略有不同的方法。 The following creates a new transparent PNG image onto which we add the QR-code and the logo.
下面创建了一个新的透明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 );
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.