PHP Classes

File: src/Core/Writers/CustomPngWriter.php

Recommend this page to a friend!
  Packages of Amirreza Ebrahimi   HeroQR Powerful PHP QR Code Library to generate PNG, SVG, PDF, Logos Ready to Use with Laravel   src/Core/Writers/CustomPngWriter.php   Download  
File: src/Core/Writers/CustomPngWriter.php
Role: Class source
Content type: text/plain
Description: Class source
Class: HeroQR Powerful PHP QR Code Library to generate PNG, SVG, PDF, Logos Ready to Use with Laravel
Generate QR code images in several formats
Author: By
Last change: feat: add SVG customization support for shapes, markers, and cursors (previously only PNG)
Date: 15 days ago
Size: 1,695 bytes
 

Contents

Class file image Download
<?php

namespace HeroQR\Core\Writers;

use
Endroid\QrCode\{Label\LabelInterface,
   
Logo\LogoInterface,
   
QrCodeInterface,
   
Writer\Result\GdResult,
   
Writer\Result\PngResult,
   
Writer\Result\ResultInterface};
use
HeroQR\Contracts\Customs\Writer\AbstractPngWriter;

/**
 * Writes QR codes as PNG with custom options
 *
 * Extends the base PNG writer to allow optional compression and
 * handles embedding logos and labels in the QR code
 */
readonly class CustomPngWriter extends AbstractPngWriter
{
    public const
WRITER_OPTION_COMPRESSION_LEVEL = 'compression_level';

   
/**
     * Generates an PNG QR code with optional logo, label, and customization options
     *
     * @param QrCodeInterface $qrCode QR Code instance to render
     * @param LogoInterface|null $logo Optional logo to embed in the QR code
     * @param LabelInterface|null $label Optional label to add to the QR code
     * @param array $options Optional PNG flags (compression level and...)
     * @return ResultInterface PNG result of the QR code
     * @throws \Exception
     */
   
public function write(
       
QrCodeInterface $qrCode,
        ?
LogoInterface $logo = null,
        ?
LabelInterface $label = null,
        array
$options = []
    ):
ResultInterface
   
{

       
$options[self::WRITER_OPTION_COMPRESSION_LEVEL] = $options[self::WRITER_OPTION_COMPRESSION_LEVEL] ?? 1;

       
/**
         * @var GdResult $gdResult
         */
       
$gdResult = parent::write($qrCode, $logo, $label, $options);

        return new
PngResult(
           
$gdResult->getMatrix(),
           
$gdResult->getImage(),
           
$options[self::WRITER_OPTION_COMPRESSION_LEVEL]
        );
    }
}