参考代码如下:
public static function addLogo($logo, $qrcodePath, $fileName) { $dir = FileUtil::getFilePath($fileName . '.png'); //创建本地文件路径 $qrResource = imagecreatefromstring(file_get_contents($qrcodePath)); @unlink($qrcodePath); // 删除临时二维码文件 $logo = imagecreatefromstring(file_get_contents($logo)); $qrWidth = imagesx($qrResource); //二维码图片宽度 $logoWidth = imagesx($logo); //logo图片宽度 $logoHeight = imagesy($logo); //logo图片高度 $logoQrWidth = $qrWidth / 4; $scale = $logoWidth / $logoQrWidth; $logoQrHeight = $logoHeight / $scale; $fromWidth = ($qrWidth - $logoQrWidth) / 2; imagesavealpha($logo, true); imagecolortransparent($logo, imagecolorat($logo, 1, 1)); // 透明处理 //重新组合图片并调整大小 imagecopyresampled($qrResource, $logo, $fromWidth, $fromWidth, 0, 0, $logoQrWidth, $logoQrHeight, $logoWidth, $logoHeight); imagecopyresized($qrResource, $logo, $fromWidth, $fromWidth, 0, 0, $logoQrWidth, $logoQrHeight, $logoWidth, $logoHeight); // 输出图像到文件中 imagepng($qrResource, $dir); return $dir; }