![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Catalog/Model/Product/Pdf/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Radosław Stępień <[email protected]> <[email protected]> */ //phpcs:ignoreFile namespace Cnc\Catalog\Model\Product\Pdf; use Endroid\QrCode\ErrorCorrectionLevel; use Endroid\QrCode\Exception\ValidationException; use Endroid\QrCode\QrCode; use Endroid\QrCode\Writer\PngWriter; use Magento\Catalog\Model\Product; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Store\Model\StoreManagerInterface; class GenerateQrCode { const QR_CODE_IMAGE_EXTENSION = '.png'; const QR_CODE_IMAGE_PATH = '/pub/media/product_pdf/qr_codes/'; /** @var PngWriter */ private $pngWriter; /** @var StoreManagerInterface */ private $storeManager; /** @var QrCode */ private $qrCode; /** * GenerateQrCode constructor. * @param PngWriter $pngWriter * @param StoreManagerInterface $storeManager * @param QrCode $qrCode */ public function __construct( PngWriter $pngWriter, StoreManagerInterface $storeManager, QrCode $qrCode ) { $this->pngWriter = $pngWriter; $this->storeManager = $storeManager; $this->qrCode = $qrCode; } /** * Generate QR code * @param Product $product * @return string * @throws ValidationException * @throws NoSuchEntityException */ public function getQrCode(Product $product): string { $this->qrCode->setSize(400); $this->qrCode->setText($product->getProductUrl()); $this->qrCode->setLogoWidth(200); $this->qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH()); $this->qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]); $this->qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]); $this->qrCode->setEncoding('UTF-8'); $filename = $product->getSku() . self::QR_CODE_IMAGE_EXTENSION; $imagePath = BP . self::QR_CODE_IMAGE_PATH . $product->getSku() . '/' . $filename; $qrCodePath = substr(self::QR_CODE_IMAGE_PATH . $product->getSku() . '/' . $filename, 1); if (!file_exists($imagePath)) { if (!file_exists(BP . '/pub/media/product_pdf')) { mkdir(BP . '/pub/media/product_pdf'); } if (!file_exists(BP . '/pub/media/product_pdf/qr_codes')) { mkdir(BP . '/pub/media/product_pdf/qr_codes'); } if (!file_exists(BP . self::QR_CODE_IMAGE_PATH . $product->getSku())) { mkdir(BP . self::QR_CODE_IMAGE_PATH . $product->getSku()); } $localImageFile = fopen($imagePath, 'w'); $pngData = $this->pngWriter->writeString($this->qrCode); fwrite($localImageFile, $pngData); fclose($localImageFile); } return $this->storeManager->getStore()->getBaseUrl() . $qrCodePath; } }