Skip to content

Conversation

@gatasi
Copy link

@gatasi gatasi commented Apr 15, 2021

Both Magento and ScandiPWA documentations recommend setting the webroot to the /pub directory.

Therefore generating the icons URLs with 'pub/' will result in 404 errors when the webroot is properly set.

This PR addresses this issue by removing 'pub/' from icons URLs.

@bahramdavoodi
Copy link

Hi @gatasi,

this should use from env config. my suggestion is this code.

<?php
/**
 * @category ScandiPWA
 * @package ScandiPWA\Customization
 * @author Rihards Abolins <[email protected]>
 * @copyright Copyright (c) 2015 Scandiweb, Ltd (http://scandiweb.com)
 * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
 */

namespace ScandiPWA\Customization\Controller;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Image\AdapterFactory;

/**
 * Class AppIcon
 * @package ScandiPWA\Customization\Controller
 */
class AppIcon
{
    const REFERENCE_IMAGE_PATH = 'favicon/favicon.png';

    const STORAGE_PATH = 'favicon/icons/';

    const IMAGE_RESIZING_CONFIG = [
        'ios' => [
            'type' => 'ios',
            'sizes' => [120, 152, 167, 180, 1024]
        ],
        'ios_startup' => [
            'type' => 'ios_startup',
            'sizes' => [2048, 1668, 1536, 1125, 1242, 750, 640]
        ],
        'android' => [
            'type' => 'android',
            'sizes' => [36, 48, 72, 96, 144, 192, 512]
        ]
    ];

    /**
     * @var DeploymentConfig
     */
    protected $deploymentConfig;

    /**
     * @var Filesystem
     */
    protected $fileSystem;

    /**
     * @var AdapterFactory
     */
    protected $imageFactory;

    /**
     * AppIcon constructor.
     * @param Filesystem $fileSystem
     * @param AdapterFactory $imageFactory
     */
    public function __construct(
        DeploymentConfig $deploymentConfig,
        Filesystem $fileSystem,
        AdapterFactory $imageFactory
    ) {
        $this->deploymentConfig = $deploymentConfig;
        $this->imageFactory = $imageFactory;
        $this->fileSystem = $fileSystem;
    }

    /**
     * @param string $name
     * @param int $width
     * @param int $height
     * @param string $absolutePath
     * @return bool
     */
    protected function saveImage ($source, $name, $width = null, $height = null)
    {
        $path = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath(self::STORAGE_PATH) . $name . '.png';
        $this->saveImageWithPath($source, $path, $width, $height);
    }

    /**
     * @param $path
     * @param $width
     * @param $height
     * @param $absolutePath
     * @return bool
     */
    protected function saveImageWithPath ($source, $targetPath, $width = null, $height = null)
    {
        $imageResize = $this->imageFactory->create();
        $imageResize->open($source);
        $imageResize->constrainOnly(true);
        $imageResize->keepTransparency(true);

        // Resizes image
        if ($width !== null && $height !== null) {
            $imageResize->keepFrame(false);
            $imageResize->keepAspectRatio(false);
            $imageResize->resize($width,$height);
        }

        try {
            $imageResize->save($targetPath);
        } catch (\Exception $e) {
            return false;
        }

        return true;
    }

    /**
     * @return array
     */
    public function getIconData ()
    {
        $output = [];
        foreach (self::IMAGE_RESIZING_CONFIG as $config) {
            foreach ($config['sizes'] as $size) {
                $width = is_array($size) ? $size[0] : $size;
                $height = is_array($size) ? $size[1] : $size;
                $name = 'icon_' . $config['type'] . '_' . $width . 'x' . $height;
                $src = '../' . self::STORAGE_PATH . $name . '.png';
                $output[] = [
                    'src' => $src,
                    'type' => 'image/png',
                    'sizes' => $width . 'x' . $height
                ];
            }
        }
        return $output;
    }

    /**
     * @return array[]
     */
    public function getIconLinks ()
    {
        $output = [
            'icon' => [],
            'ios' => [],
            'ios_startup' => [],
            'android' => []
        ];

        $mediaDirectory = $this->deploymentConfig->get('directories/document_root_is_pub', false)
            ? DIRECTORY_SEPARATOR . $this->fileSystem->getDirectoryReadByPath(DirectoryList::MEDIA)
                ->getAbsolutePath(self::STORAGE_PATH)
            : DIRECTORY_SEPARATOR . $this->fileSystem->getDirectoryReadByPath(DirectoryList::PUB)
                ->getAbsolutePath(DirectoryList::MEDIA . DIRECTORY_SEPARATOR . self::STORAGE_PATH);

        foreach (self::IMAGE_RESIZING_CONFIG as $type => $config) {
            foreach ($config['sizes'] as $size) {
                $width = is_array($size) ? $size[0] : $size;
                $height = is_array($size) ? $size[1] : $size;
                $size = $width . 'x' . $height;
                $name = 'icon_' . $config['type'] . '_' . $width . 'x' . $height;
                $href = $mediaDirectory . $name . '.png';
                $output[$type][$size] = [
                    'href' => $href,
                    'sizes' => $size
                ];
                $output['icon'][$size] = [
                    'href' => $href,
                    'sizes' => $size
                ];
            }
        }

        return $output;
    }

    /**
     * Creates main favicon icon.
     */
    private function buildFaviconImage($sourcePath)
    {
        $targetPath = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath(self::REFERENCE_IMAGE_PATH);
        $this->saveImageWithPath($sourcePath, $targetPath);
    }

    /**
     * @return bool
     */
    public function buildAppIcons ($sourcePath)
    {
        if (!file_exists($sourcePath))
            return false;

        $this->buildFaviconImage($sourcePath);

        foreach (self::IMAGE_RESIZING_CONFIG as $config) {
            foreach ($config['sizes'] as $size) {
                $width = is_array($size) ? $size[0] : $size;
                $height = is_array($size) ? $size[1] : $size;
                $name = 'icon_' . $config['type'] . '_' . $width . 'x' . $height;
                $this->saveImage($sourcePath, $name, $width, $height);
            }
        }
    }
}

@carinadues carinadues requested a review from zans-laksa July 6, 2022 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants