1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| <?php * author lkj * date 2015/12/26 * @param string $img 底图路径 * @param string $qrcode 二维码路径 * @param int $x 二维码左边距 true水平居中 * @param int $y 二维码上边距 true垂直居中 * @param int $percent 二维码按比例缩放 * @param bool $save false直接输出true保存文件 */
bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h ) 将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。 */
function mergerImg($img, $qrcode, $x, $y, $percent, $save = false) { list($max_width, $max_height) = getimagesize($img); $dests = imagecreatetruecolor($max_width, $max_height); $dst_im = imagecreatefromjpeg($img); imagecopy($dests, $dst_im, 0, 0, 0, 0, $max_width, $max_height); imagedestroy($dst_im);
list($width, $height) = getimagesize($qrcode);
$newwidth = $width * $percent; $newheight = $height * $percent; $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefrompng($qrcode); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); $name = 'tmep.png'; imagepng($thumb, $name);
$src_im = imagecreatefrompng($name); $src_info = getimagesize($name);
if ($x === true) { $padding_left = ($max_width - $src_info[0]) / 2; } else { $padding_left = $x; } if ($y === true) { $padding_top = ($max_height - $src_info[1]) / 2; } else { $padding_top = $y; } imagecopy($dests, $src_im, $padding_left, $padding_top, 0, 0, $src_info[0], $src_info[1]);
$color = imagecolorallocate($dests, 255, 255, 255); $fontfile = "font.ttf"; $string = "@likangjun.com"; imagettftext($dests, 18, 0, 230, 760, $color, $fontfile, $string); unlink($name);
if ($save) { $savepath = "qrcode.png"; $result = imagepng($dests, $savepath); imagedestroy($dests); echo $result ? 'success' : 'fail'; }
header('Content-Type: image/jpeg'); imagepng($dests); imagedestroy($dests); }
|