16 changed files with 532 additions and 165 deletions
			
			
		| @ -0,0 +1,34 @@ | |||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | use core\forms\FaviconForm; | ||||||
|  | use kartik\form\ActiveForm; | ||||||
|  | use yii\helpers\Html; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * @var yii\web\View $this | ||||||
|  |  * @var FaviconForm $model | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | $this->title                   = Yii::t('main', 'Set Favicon'); | ||||||
|  | $this->params['breadcrumbs'][] = ['label' => Yii::t('main', 'Settings'), 'url' => ['index']]; | ||||||
|  | $this->params['breadcrumbs'][] = $this->title; | ||||||
|  | ?> | ||||||
|  | <div class="favicon-create"> | ||||||
|  | 
 | ||||||
|  |     <div class="box box-default"> | ||||||
|  |         <div class="box-body"> | ||||||
|  | 
 | ||||||
|  |             <?php $form = ActiveForm::begin(); ?> | ||||||
|  | 
 | ||||||
|  |             <?= $form->field($model, 'image')->fileInput(['accept' => 'image/png']) ?> | ||||||
|  | 
 | ||||||
|  |             <div class="form-group"> | ||||||
|  |                 <?= Html::submitButton(Yii::t('buttons', 'Save'), ['class' => 'btn btn-success']) ?> | ||||||
|  |             </div> | ||||||
|  | 
 | ||||||
|  |             <?php ActiveForm::end(); ?> | ||||||
|  | 
 | ||||||
|  |         </div> | ||||||
|  |     </div> | ||||||
|  | 
 | ||||||
|  | </div> | ||||||
| @ -1,39 +0,0 @@ | |||||||
| <?php |  | ||||||
| /** |  | ||||||
|  * Created by Error202 |  | ||||||
|  * Date: 24.08.2018 |  | ||||||
|  */ |  | ||||||
| 
 |  | ||||||
| namespace core\components; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| class TestForm extends LanguageDynamicModel |  | ||||||
| { |  | ||||||
| 	public $name; |  | ||||||
| 	public $content; |  | ||||||
| 	public $number; |  | ||||||
| 
 |  | ||||||
| 	public function rules() { |  | ||||||
| 		return array_merge( |  | ||||||
| 			parent::rules(), |  | ||||||
| 			[ |  | ||||||
| 				[['name', 'content'], 'required'], |  | ||||||
| 				['name', 'string', 'max' => 50], |  | ||||||
| 				['content', 'string'], |  | ||||||
| 				['number', 'integer'], |  | ||||||
| 			] |  | ||||||
| 		); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	public function attributeLabels() { |  | ||||||
| 		return array_merge( |  | ||||||
| 			parent::attributeLabels(), |  | ||||||
| 			[ |  | ||||||
| 				'name' => \Yii::t('main', 'Name'), |  | ||||||
| 				'content' => \Yii::t('main', 'Key'), |  | ||||||
| 				'number' => \Yii::t('main', 'Value'), |  | ||||||
| 			] |  | ||||||
| 		); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -0,0 +1,134 @@ | |||||||
|  | <?php | ||||||
|  | /** | ||||||
|  |  * Created by Error202 | ||||||
|  |  * Date: 03.10.2017 | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | namespace core\components\favicon; | ||||||
|  | 
 | ||||||
|  | use Yii; | ||||||
|  | use yii\imagine\Image; | ||||||
|  | use yii\web\View; | ||||||
|  | 
 | ||||||
|  | class FaviconGenerator | ||||||
|  | { | ||||||
|  |     public $originPath; | ||||||
|  |     public $cachePath; | ||||||
|  | 
 | ||||||
|  |     public function __construct() | ||||||
|  |     { | ||||||
|  |         $this->originPath = Yii::getAlias('@staticRoot') . '/images/origin/favicon'; | ||||||
|  |         $this->cachePath = Yii::getAlias('@staticRoot') . '/images/cache/favicon'; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public function generateIcons() | ||||||
|  |     { | ||||||
|  |         if (!file_exists($this->originPath)) { | ||||||
|  |             mkdir($this->originPath, 0777, true); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (!file_exists($this->cachePath)) { | ||||||
|  |             mkdir($this->cachePath, 0777, true); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (file_exists($this->originPath . '/favicon.png')) { | ||||||
|  |             $this->generateFavicons(); | ||||||
|  |             $this->generateAndroid(); | ||||||
|  |             $this->generateApple(); | ||||||
|  |             $this->generateMicrosoft(); | ||||||
|  |             $this->generateManifestJson(); | ||||||
|  |             $this->generateBrowserConfigXml(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected function generateFavicons() | ||||||
|  |     { | ||||||
|  |         foreach ([16, 32, 96, 194] as $s) { | ||||||
|  |             $filename = sprintf('favicon-%sx%s.png', $s, $s); | ||||||
|  | 
 | ||||||
|  |             Image::thumbnail($this->originPath . '/favicon.png', $s, $s) | ||||||
|  |                  ->save($this->cachePath . '/' . $filename); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected function generateAndroid() | ||||||
|  |     { | ||||||
|  |         foreach ([36, 48, 72, 96, 144, 192] as $s) { | ||||||
|  |             $filename = sprintf('android-chrome-%sx%s.png', $s, $s); | ||||||
|  | 
 | ||||||
|  |             Image::thumbnail($this->originPath . '/favicon.png', $s, $s) | ||||||
|  |                  ->save($this->cachePath . '/' . $filename); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected function generateApple() | ||||||
|  |     { | ||||||
|  |         foreach ([57, 60, 72, 76, 114, 120, 144, 152, 180] as $s) { | ||||||
|  |             $filename = sprintf('apple-touch-icon-%sx%s.png', $s, $s); | ||||||
|  | 
 | ||||||
|  |             Image::thumbnail($this->originPath . '/favicon.png', $s, $s) | ||||||
|  |                  ->save($this->cachePath . '/' . $filename); | ||||||
|  | 
 | ||||||
|  |             if ($s == 180) { | ||||||
|  |                 $filename = sprintf('apple-touch-icon.png', $s, $s); | ||||||
|  | 
 | ||||||
|  |                 Image::thumbnail($this->originPath . '/favicon.png', $s, $s) | ||||||
|  |                      ->save($this->cachePath . '/' . $filename); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected function generateMicrosoft() | ||||||
|  |     { | ||||||
|  |         foreach ([70, 150, 270, 310] as $s) { | ||||||
|  |             $filename = sprintf('mstile-%sx%s.png', $s, $s); | ||||||
|  | 
 | ||||||
|  |             Image::thumbnail($this->originPath . '/favicon.png', $s, $s) | ||||||
|  |                  ->save($this->cachePath . '/' . $filename); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected function generateManifestJson() | ||||||
|  |     { | ||||||
|  |         $cacheUrl = Yii::getAlias('@static') . '/images/cache'; | ||||||
|  | 
 | ||||||
|  |         // frontend | ||||||
|  |         file_put_contents( | ||||||
|  |             Yii::getAlias('@frontend/web') . '/' . 'manifest.json', | ||||||
|  |             (new View())->render('@core/components/favicon/manifest.php', [ | ||||||
|  |                 'appName' => isset(Yii::$app->params['settings']['site']['name']) ? Yii::$app->params['settings']['site']['name'] : Yii::$app->name, | ||||||
|  |                 'iconPath' => $cacheUrl . '/favicon' | ||||||
|  |             ]) | ||||||
|  |         ); | ||||||
|  | 
 | ||||||
|  |         // backend | ||||||
|  |         file_put_contents( | ||||||
|  |             Yii::getAlias('@backend/web') . '/' . 'manifest.json', | ||||||
|  |             (new View())->render('@core/components/favicon/manifest.php', [ | ||||||
|  |                 'appName' => isset(Yii::$app->params['settings']['site']['name']) ? Yii::$app->params['settings']['site']['name'] : Yii::$app->name, | ||||||
|  |                 'iconPath' => $cacheUrl . '/favicon' | ||||||
|  |             ]) | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected function generateBrowserConfigXml() | ||||||
|  |     { | ||||||
|  |         $cacheUrl = Yii::getAlias('@static') . '/images/cache'; | ||||||
|  | 
 | ||||||
|  |         // frontend | ||||||
|  |         file_put_contents( | ||||||
|  |             Yii::getAlias('@frontend/web') . '/' . 'browserconfig.xml', | ||||||
|  |             (new View())->render('@core/components/favicon/browserconfig.php', [ | ||||||
|  |                 'iconPath' => $cacheUrl . '/favicon' | ||||||
|  |             ]) | ||||||
|  |         ); | ||||||
|  | 
 | ||||||
|  |         // backend | ||||||
|  |         file_put_contents( | ||||||
|  |             Yii::getAlias('@backend/web') . '/' . 'browserconfig.xml', | ||||||
|  |             (new View())->render('@core/components/favicon/browserconfig.php', [ | ||||||
|  |                 'iconPath' => $cacheUrl . '/favicon' | ||||||
|  |             ]) | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,19 @@ | |||||||
|  | <?php | ||||||
|  | /** | ||||||
|  |  * @var $this \yii\web\View | ||||||
|  |  * @var $iconPath string | ||||||
|  |  */ | ||||||
|  | ?> | ||||||
|  | <?= '<?xml version="1.0" encoding="utf-8"?>' ?> | ||||||
|  | 
 | ||||||
|  | <browserconfig> | ||||||
|  |     <msapplication> | ||||||
|  |         <tile> | ||||||
|  |             <square70x70logo src="<?= $iconPath ?>/mstile-70x70.png"/>
 | ||||||
|  |             <square150x150logo src="<?= $iconPath ?>/mstile-150x150.png"/>
 | ||||||
|  |             <square310x310logo src="<?= $iconPath ?>/mstile-310x310.png"/>
 | ||||||
|  |             <wide310x150logo src="<?= $iconPath ?>/mstile-310x150.png"/>
 | ||||||
|  |             <TileColor>#ffffff</TileColor> | ||||||
|  |         </tile> | ||||||
|  |     </msapplication> | ||||||
|  | </browserconfig> | ||||||
| @ -0,0 +1,48 @@ | |||||||
|  | <?php | ||||||
|  | /** | ||||||
|  |  * @var $this \yii\web\View | ||||||
|  |  * @var $appName string | ||||||
|  |  * @var $iconPath string | ||||||
|  |  */ | ||||||
|  | ?> | ||||||
|  | { | ||||||
|  |   "name": "<?= $appName ?>",
 | ||||||
|  |   "icons": [ | ||||||
|  |     { | ||||||
|  |       "src": "<?= $iconPath ?>/android-chrome-36x36.png",
 | ||||||
|  |       "sizes": "36x36", | ||||||
|  |       "type": "image\/png", | ||||||
|  |       "density": "0.75" | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  |       "src": "<?= $iconPath ?>/android-chrome-48x48.png",
 | ||||||
|  |       "sizes": "48x48", | ||||||
|  |       "type": "image\/png", | ||||||
|  |       "density": "1.0" | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  |       "src": "<?= $iconPath ?>/android-chrome-72x72.png",
 | ||||||
|  |       "sizes": "72x72", | ||||||
|  |       "type": "image\/png", | ||||||
|  |       "density": "1.5" | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  |       "src": "<?= $iconPath ?>/android-chrome-96x96.png",
 | ||||||
|  |       "sizes": "96x96", | ||||||
|  |       "type": "image\/png", | ||||||
|  |       "density": "2.0" | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  |       "src": "<?= $iconPath ?>/android-chrome-144x144.png",
 | ||||||
|  |       "sizes": "144x144", | ||||||
|  |       "type": "image\/png", | ||||||
|  |       "density": "3.0" | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  |       "src": "<?= $iconPath ?>/android-chrome-192x192.png",
 | ||||||
|  |       "sizes": "192x192", | ||||||
|  |       "type": "image\/png", | ||||||
|  |       "density": "4.0" | ||||||
|  |     } | ||||||
|  |   ] | ||||||
|  | } | ||||||
| @ -0,0 +1,37 @@ | |||||||
|  | <?php | ||||||
|  | /** | ||||||
|  |  * Created by Error202 | ||||||
|  |  * Date: 11.09.2018 | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | namespace core\forms; | ||||||
|  | 
 | ||||||
|  | use yii\base\Model; | ||||||
|  | use Yii; | ||||||
|  | 
 | ||||||
|  | class FaviconForm extends Model | ||||||
|  | { | ||||||
|  |     public $image; | ||||||
|  | 
 | ||||||
|  |     public function rules() | ||||||
|  |     { | ||||||
|  |         return [ | ||||||
|  |             ['image', 'required'], | ||||||
|  |             [['image'], 'image', 'extensions' => 'png', 'minWidth' => 200, 'minHeight' => 200], | ||||||
|  |         ]; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public function attributeLabels() | ||||||
|  |     { | ||||||
|  |         return [ | ||||||
|  |             'image' => Yii::t('main', 'Image'), | ||||||
|  |         ]; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public function attributeHints() | ||||||
|  |     { | ||||||
|  |         return [ | ||||||
|  |             'image' => Yii::t('main', 'Only png files allowed. Minimum size: 200x200. Form: square'), | ||||||
|  |         ]; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,77 @@ | |||||||
|  | <?php | ||||||
|  | /** | ||||||
|  |  * Created by Error202 | ||||||
|  |  * Date: 03.10.2017 | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | namespace core\widgets; | ||||||
|  | 
 | ||||||
|  | use yii\base\Widget; | ||||||
|  | use yii\helpers\Url; | ||||||
|  | use Yii; | ||||||
|  | 
 | ||||||
|  | class FaviconWidget extends Widget | ||||||
|  | { | ||||||
|  |     public $iconsPath; | ||||||
|  |     public $iconsUrl; | ||||||
|  | 
 | ||||||
|  |     public function run() | ||||||
|  |     { | ||||||
|  |         /** @var \yii\web\View $View */ | ||||||
|  |         $View = \Yii::$app->get('view'); | ||||||
|  | 
 | ||||||
|  |         $this->iconsPath = Yii::getAlias('@staticRoot') . '/images/cache/favicon'; | ||||||
|  |         $this->iconsUrl = Yii::getAlias('@static') . '/images/cache/favicon'; | ||||||
|  | 
 | ||||||
|  |         foreach ([16, 32, 96, 194] as $s) { | ||||||
|  |             $filename = sprintf('favicon-%sx%s.png', $s, $s); | ||||||
|  |             $filepath = $this->iconsPath . '/' . $filename; | ||||||
|  |             if (!empty($filepath) && file_exists($filepath)) { | ||||||
|  |                 $View->registerLinkTag([ | ||||||
|  |                     'rel' => 'icon', | ||||||
|  |                     'type' => 'image/png', | ||||||
|  |                     'href' => Url::to(sprintf('%s/%s', $this->iconsUrl, $filename)), | ||||||
|  |                     'sizes' => sprintf('%sx%s', $s, $s), | ||||||
|  |                 ], basename($filepath)); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         $filepath = $this->iconsPath . '/' . 'android-chrome-192x192.png'; | ||||||
|  |         if (!empty($filepath) && file_exists($filepath)) { | ||||||
|  |             $View->registerLinkTag([ | ||||||
|  |                 'rel' => 'icon', | ||||||
|  |                 'type' => 'image/png', | ||||||
|  |                 'href' => Url::to(sprintf('%s/android-chrome-192x192.png', $this->iconsUrl)), | ||||||
|  |                 'sizes' => sprintf('%sx%s', 192, 192), | ||||||
|  |             ], basename($filepath)); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         foreach ([57, 60, 72, 76, 114, 120, 144, 152, 180] as $s) { | ||||||
|  |             $filename = sprintf('apple-touch-icon-%sx%s.png', $s, $s); | ||||||
|  |             $filepath = $this->iconsPath . '/' . $filename; | ||||||
|  |             if (!empty($filepath) && file_exists($filepath)) { | ||||||
|  |                 $View->registerLinkTag([ | ||||||
|  |                     'rel' => 'apple-touch-icon', | ||||||
|  |                     'type' => 'image/png', | ||||||
|  |                     'href' => Url::to(sprintf('%s/%s', $this->iconsUrl, $filename)), | ||||||
|  |                     'sizes' => sprintf('%sx%s', $s, $s), | ||||||
|  |                 ], basename($filepath)); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         $filepath = $this->iconsPath . 'mstile-144x144.png'; | ||||||
|  |         if (!empty($filepath) && file_exists($filepath)) { | ||||||
|  |             $View->registerMetaTag([ | ||||||
|  |                 'name' => 'msapplication-TileImage', | ||||||
|  |                 'content' => Url::to(sprintf('%s/mstile-144x144.png', $this->iconsUrl)), | ||||||
|  |             ], basename($filepath)); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         $filepath = Yii::getAlias('@webroot') . '/' . 'manifest.json'; | ||||||
|  |         if (!empty($filepath) && file_exists($filepath)) { | ||||||
|  |             $View->registerLinkTag([ | ||||||
|  |                 'rel' => 'manifest', | ||||||
|  |                 'href' => Url::to(sprintf('%s/manifest.json', Yii::getAlias('@web'))), | ||||||
|  |             ], basename($filepath)); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue