|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @category ScandiPWA |
| 4 | + * @package ScandiPWA\Customization |
| 5 | + * @author Rihards Abolins <[email protected]> |
| 6 | + * @copyright Copyright (c) 2015 Scandiweb, Ltd (http://scandiweb.com) |
| 7 | + * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) |
| 8 | + */ |
| 9 | + |
| 10 | +namespace ScandiPWA\Customization\Controller; |
| 11 | + |
| 12 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 13 | +use Magento\Framework\Filesystem; |
| 14 | +use Magento\Framework\Image\AdapterFactory; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class AppIcon |
| 18 | + * @package ScandiPWA\Customization\Controller |
| 19 | + */ |
| 20 | +class AppIcon |
| 21 | +{ |
| 22 | + const REFERENCE_IMAGE_PATH = 'favicon/favicon.png'; |
| 23 | + |
| 24 | + const STORAGE_PATH = 'favicon/icons/'; |
| 25 | + |
| 26 | + const IMAGE_RESIZING_CONFIG = [ |
| 27 | + 'apple' => [ |
| 28 | + 'type' => 'ios', |
| 29 | + 'sizes' => [120, 152, 167, 180, 1024] |
| 30 | + ], |
| 31 | + 'apple_startup' => [ |
| 32 | + 'type' => 'ios_startup', |
| 33 | + 'sizes' => [2048, 1668, 1536, 1125, 1242, 750, 640] |
| 34 | + ], |
| 35 | + 'android' => [ |
| 36 | + 'type' => 'android', |
| 37 | + 'sizes' => [36, 48, 72, 96, 144, 192, 512] |
| 38 | + ] |
| 39 | + ]; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var Filesystem |
| 43 | + */ |
| 44 | + protected $fileSystem; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var AdapterFactory |
| 48 | + */ |
| 49 | + protected $imageFactory; |
| 50 | + |
| 51 | + /** |
| 52 | + * AppIcon constructor. |
| 53 | + * @param Filesystem $fileSystem |
| 54 | + * @param AdapterFactory $imageFactory |
| 55 | + */ |
| 56 | + public function __construct( |
| 57 | + Filesystem $fileSystem, |
| 58 | + AdapterFactory $imageFactory |
| 59 | + ) |
| 60 | + { |
| 61 | + $this->imageFactory = $imageFactory; |
| 62 | + $this->fileSystem = $fileSystem; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param string $name |
| 67 | + * @param int $width |
| 68 | + * @param int $height |
| 69 | + * @param string $absolutePath |
| 70 | + * @return bool |
| 71 | + */ |
| 72 | + protected function saveImage ($name, $width, $height, $absolutePath) |
| 73 | + { |
| 74 | + $imageResizedDir = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath(self::STORAGE_PATH) . $name . '.png'; |
| 75 | + |
| 76 | + $imageResize = $this->imageFactory->create(); |
| 77 | + $imageResize->open($absolutePath); |
| 78 | + $imageResize->constrainOnly(true); |
| 79 | + $imageResize->keepTransparency(true); |
| 80 | + $imageResize->keepFrame(false); |
| 81 | + $imageResize->keepAspectRatio(false); |
| 82 | + $imageResize->resize($width,$height); |
| 83 | + |
| 84 | + try { |
| 85 | + $imageResize->save($imageResizedDir); |
| 86 | + } catch (\Exception $e) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return array |
| 95 | + */ |
| 96 | + public function getIconData () |
| 97 | + { |
| 98 | + $output = []; |
| 99 | + foreach (self::IMAGE_RESIZING_CONFIG as $config) { |
| 100 | + foreach ($config['sizes'] as $size) { |
| 101 | + $width = is_array($size) ? $size[0] : $size; |
| 102 | + $height = is_array($size) ? $size[1] : $size; |
| 103 | + $name = 'icon_' . $config['type'] . '_' . $width . 'x' . $height; |
| 104 | + $src = 'pub/media/' . self::STORAGE_PATH . $name . '.png'; |
| 105 | + $output[] = [ |
| 106 | + 'src' => $src, |
| 107 | + 'type' => 'image/png', |
| 108 | + 'size' => $width . 'x' . $height |
| 109 | + ]; |
| 110 | + } |
| 111 | + } |
| 112 | + return $output; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return array[] |
| 117 | + */ |
| 118 | + public function getIconLinks () |
| 119 | + { |
| 120 | + $output = [ |
| 121 | + 'icon' => [], |
| 122 | + 'ios_startup' => [] |
| 123 | + ]; |
| 124 | + |
| 125 | + foreach (self::IMAGE_RESIZING_CONFIG as $type => $config) { |
| 126 | + $targetPath = $type === 'apple_startup' ? 'ios_startup' : 'icon'; |
| 127 | + foreach ($config['sizes'] as $size) { |
| 128 | + $width = is_array($size) ? $size[0] : $size; |
| 129 | + $height = is_array($size) ? $size[1] : $size; |
| 130 | + $size = $width . 'x' . $height; |
| 131 | + $name = 'icon_' . $config['type'] . '_' . $width . 'x' . $height; |
| 132 | + $href = 'pub/media/' . self::STORAGE_PATH . $name . '.png'; |
| 133 | + $output[$targetPath][$size] = [ |
| 134 | + 'href' => $href, |
| 135 | + 'sizes' => $size |
| 136 | + ]; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return $output; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @return bool |
| 145 | + */ |
| 146 | + public function buildAppIcons () { |
| 147 | + $absolutePath = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath(self::REFERENCE_IMAGE_PATH); |
| 148 | + |
| 149 | + if (!file_exists($absolutePath)) |
| 150 | + return false; |
| 151 | + |
| 152 | + foreach (self::IMAGE_RESIZING_CONFIG as $config) { |
| 153 | + foreach ($config['sizes'] as $size) { |
| 154 | + $width = is_array($size) ? $size[0] : $size; |
| 155 | + $height = is_array($size) ? $size[1] : $size; |
| 156 | + $name = 'icon_' . $config['type'] . '_' . $width . 'x' . $height; |
| 157 | + $this->saveImage($name, $width, $height, $absolutePath); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments