通过PHP将文本添加到PNG文件

时间:2015-02-06 16:39:05

标签: php png imagecreatefrompng

我正在尝试通过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函数来改变图像并为其添加文本。帮助请...谢谢

0 个答案:

没有答案