Skip to content

Commit 8217f5b

Browse files
Merge pull request #3 from riha112/1392
#1392 Favicon change through admin / M2 Support
2 parents 58fcfee + 80be46b commit 8217f5b

File tree

11 files changed

+716
-0
lines changed

11 files changed

+716
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ Adds *ScandiPWA* tab in the Store configuration. Adds color and content configur
77
- Product card displayed attribute selection
88
- Main menu switcher
99
- Cookie popup content
10+
- Webmanifest content
11+
- Favicon

src/Controller/AppIcon.php

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
}

src/Controller/Webmanifest.php

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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\Config\Storage\WriterInterface;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Framework\Exception\FileSystemException;
15+
use Magento\Framework\Filesystem;
16+
use Magento\Framework\App\Filesystem\DirectoryList;
17+
18+
/**
19+
* Class Webmanifest
20+
* @package ScandiPWA\Customization\Controller
21+
*/
22+
class Webmanifest
23+
{
24+
const WEBMANIFEST_CONFIG_PATH = 'webmanifest_customization/webmanifest/';
25+
26+
const STORAGE_PATH = 'webmanifest/manifest.webmanifest';
27+
28+
const ALLOWED_FIELDS = [
29+
'name',
30+
'short_name',
31+
'description',
32+
'background_color',
33+
'lang',
34+
'theme_color',
35+
'start_url',
36+
'orientation',
37+
'display',
38+
'categories',
39+
'dir',
40+
'iarc_rating_id',
41+
'icons',
42+
'prefer_related_applications',
43+
'related_applications',
44+
'scope',
45+
'screenshots',
46+
'serviceworker',
47+
'shortcuts'
48+
];
49+
50+
/**
51+
* @var WriterInterface
52+
*/
53+
protected $writer;
54+
55+
/**
56+
* @var ScopeConfigInterface
57+
*/
58+
protected $scopeConfig;
59+
60+
/**
61+
* @var Filesystem
62+
*/
63+
protected $fileSystem;
64+
65+
/**
66+
* Webmanifest constructor.
67+
* @param WriterInterface $writer
68+
* @param ScopeConfigInterface $scopeConfig
69+
* @param Filesystem $fileSystem
70+
*/
71+
public function __construct(
72+
WriterInterface $writer,
73+
ScopeConfigInterface $scopeConfig,
74+
Filesystem $fileSystem
75+
)
76+
{
77+
$this->writer = $writer;
78+
$this->scopeConfig = $scopeConfig;
79+
$this->fileSystem = $fileSystem;
80+
}
81+
82+
/**
83+
* @param array $data
84+
* @return false|string
85+
*/
86+
protected function getGeneratedJson(array $data)
87+
{
88+
$arrayKeys = array_keys($data);
89+
if (empty($arrayKeys))
90+
return false;
91+
92+
$unSupportedKeys = array_filter($arrayKeys, function ($key) {
93+
return !in_array($key, self::ALLOWED_FIELDS);
94+
});
95+
96+
foreach ($unSupportedKeys as $unSupportedKey) {
97+
unset($data[$unSupportedKey]);
98+
}
99+
100+
return json_encode($data);
101+
}
102+
103+
/**
104+
* @param array $data
105+
* @throws FileSystemException
106+
*/
107+
public function saveJson(array $data)
108+
{
109+
$jsonData = $this->getGeneratedJson($data);
110+
111+
if (!$jsonData || empty($data))
112+
return;
113+
114+
$fileWriter = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
115+
116+
$fileWriter->writeFile(self::STORAGE_PATH, $jsonData);
117+
}
118+
119+
/**
120+
* @return array
121+
*/
122+
public function load()
123+
{
124+
$data = [];
125+
foreach (self::ALLOWED_FIELDS as $field) {
126+
$value = $this->scopeConfig->getValue(self::WEBMANIFEST_CONFIG_PATH . $field);
127+
if (!empty($value)) {
128+
if (in_array($field, ['background_color', 'theme_color'])) {
129+
$value = '#' . $value;
130+
}
131+
$data[$field] = $value;
132+
}
133+
134+
}
135+
return $data;
136+
}
137+
}

0 commit comments

Comments
 (0)