我正在尝试通过 PHP 将文本添加到 QR 条码生成器。这是我到目前为止的代码:
<?php
define('QR_IMAGE', true);
class QRimage {
//----------------------------------------------------------------------
public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE)
{
$image = self::image($frame, $pixelPerPoint, $outerFrame);
$im = imagecreatefrompng($image);
// if there's an error, stop processing the page:
if(!$im)
{
die("");
}
// define some colours to use with the image
$yellow = imagecolorallocate($im, 255, 255, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// get the width and the height of the image
$width = imagesx($im);
$height = imagesy($im);
// draw a black rectangle across the bottom, say, 20 pixels of the image:
imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);
// now we want to write in the centre of the rectangle:
$font = 4; // store the int ID of the system font we're using in $font
$text = "test text goes here"; // store the text we're going to write in $text
// calculate the left position of the text:
$leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2;
// finally, write the string:
imagestring($im, $font, $leftTextPos, $height-18, $text, $yellow);
// output the image
// tell the browser what we're sending it
// output the image as a png
imagepng($im,$filename);
// tidy up
imagedestroy($im);
}
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4)
{
$h = count($frame);
$w = strlen($frame[0]);
$imgW = $w + 2*$outerFrame;
$imgH = $h + 2*$outerFrame;
$base_image =ImageCreate($imgW, $imgH);
$col[0] = ImageColorAllocate($base_image,255,255,255);
$col[1] = ImageColorAllocate($base_image,0,0,0);
imagefill($base_image, 0, 0, $col[0]);
for($y=0; $y<$h; $y++) {
for($x=0; $x<$w; $x++) {
if ($frame[$y][$x] == '1') {
ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
}
}
}
$target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
ImageDestroy($base_image);
return $target_image;
}
}
?>
但是我收到一条错误消息:警告:imagecreatefrompng() 期望参数 1 是字符串,第 35 行 C:\xampp\htdocs\tpm_dev\phpqrcode\qrimage.php 中给出的资源
第 35 行是这样的:
$im = imagecreatefrompng($image);
不知道我做错了什么,因为变量 $image 是指以 PNG 格式生成的 QR 条形码,而我想做的是使用 php 的 imagecreatefrompng 函数来更改图像并向其添加文本。请帮助...谢谢