0

I am trying to add text to a QR Barcode generator via PHP. This is the code I have so far:

<?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;
        }
    }

?>

However I am getting an error saying:Warning: imagecreatefrompng() expects parameter 1 to be string, resource given in C:\xampp\htdocs\tpm_dev\phpqrcode\qrimage.php on line 35

Line 35 is this:

$im = imagecreatefrompng($image);

Don't know what I am doing wrong since the variable $image is referring to the QR Barcode generated in PNG format and what I am trying to do is using the imagecreatefrompng function of php to alter the image and add text to it. Help pls ... thanks

elstiv
  • 383
  • 5
  • 27
  • 2
    obviously `$image` is a resource of SOME kind. perhaps it's already a GD image handle, in which case, you don't need to `imagecreate...()` at all. it's already loaded in GD. You'd need to see what this `self::image` is returning. – Marc B Feb 06 '15 at 16:40
  • Thanks @MarcB you solved it. I just removed the imagecreatefrompng() and it worked! – elstiv Feb 06 '15 at 17:18

0 Answers0