<?php
/*
 * PHP QR Code encoder
 *
 * Image output of code using GD2
 *
 * PHP QR Code is distributed under LGPL 3
 * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

define('QR_IMAGE', true);

class QRimage
{
    //----------------------------------------------------------------------
    public static function png(
        $frame,
        $filename = false,
        $pixelPerPoint = 4,
        $outerFrame = 4,
        $saveandprint = FALSE,
        $back_color = 0xFFFFFF,
        $fore_color = 0x000000 // PHP 8+: provide default
    ) {
        $image = self::image($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);

        if ($filename === false) {
            header("Content-type: image/png");
            imagepng($image);
        } else {
            if ($saveandprint === TRUE) {
                imagepng($image, $filename);
                header("Content-type: image/png");
                imagepng($image);
            } else {
                imagepng($image, $filename);
            }
        }

        imagedestroy($image);
    }

    //----------------------------------------------------------------------
    public static function jpeg(
        $frame,
        $filename = false,
        $pixelPerPoint = 8,
        $outerFrame = 4,
        $jpeg_quality = 85,
        $saveandprint = FALSE,
        $back_color = 0xFFFFFF,
        $fore_color = 0x000000 // PHP 8+: provide default
    ) {
        $image = self::image($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);

        if ($filename === false) {
            header("Content-type: image/jpeg");
            imagejpeg($image, null, $jpeg_quality);
        } else {
            if ($saveandprint === TRUE) {
                imagejpeg($image, $filename, $jpeg_quality);
                header("Content-type: image/jpeg");
                imagejpeg($image, null, $jpeg_quality);
            } else {
                imagejpeg($image, $filename, $jpeg_quality);
            }
        }

        imagedestroy($image);
    }

    //----------------------------------------------------------------------
    private static function image(
        $frame,
        $pixelPerPoint = 4,
        $outerFrame = 4,
        $back_color = 0xFFFFFF,
        $fore_color = 0x000000 // PHP 8+: provide default
    ) {
        $h = count($frame);
        $w = strlen($frame[0]);

        $imgW = $w + 2 * $outerFrame;
        $imgH = $h + 2 * $outerFrame;

        $base_image = imagecreate($imgW, $imgH);

        $col[0] = self::allocateColor($base_image, $back_color);
        $col[1] = self::allocateColor($base_image, $fore_color);

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

    //----------------------------------------------------------------------
    private static function allocateColor($img, $hexColor)
    {
        $r = ($hexColor >> 16) & 0xFF;
        $g = ($hexColor >> 8) & 0xFF;
        $b = $hexColor & 0xFF;

        return imagecolorallocate($img, $r, $g, $b);
    }
}
