src/Services/FaviconGenerator/FaviconImageGenerator.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\Services\FaviconGenerator;
  3. class FaviconImageGenerator extends FaviconGenerator
  4. {
  5.     private $faviconFilePath;
  6.     private $appleFilePath;
  7.     private $appleStartupImageProportion;
  8.     private $faviconDirPath;
  9.     public function __construct($applicationName$faviconDir$faviconFilePath$appleFilePath$appleStartupImageProportion 20.0)
  10.     {
  11.         parent::__construct($applicationName$faviconDir);
  12.         $this->faviconFilePath $faviconFilePath;
  13.         $this->appleFilePath $appleFilePath;
  14.         $this->appleStartupImageProportion $appleStartupImageProportion;
  15.         $this->faviconDirPath '/static/icons/';
  16.     }
  17.     public function generate()
  18.     {
  19.         if (self::createFaviconDir()) {
  20.             self::generateAppleImages();
  21.             self::generateAppleStartupImages($this->appleStartupImageProportion);
  22.             self::generateMsImages();
  23.             self::generateAndroidImages();
  24.             self::generateManifestJson();
  25.         }
  26.     }
  27.     private function hex2RGB($hex)
  28.     {
  29.         $hex str_replace("#"""strtolower($hex));
  30.         if (strlen($hex) == 3) {
  31.             $r hexdec(substr($hex01).substr($hex01));
  32.             $g hexdec(substr($hex11).substr($hex11));
  33.             $b hexdec(substr($hex21).substr($hex21));
  34.         } else {
  35.             $r hexdec(substr($hex02));
  36.             $g hexdec(substr($hex22));
  37.             $b hexdec(substr($hex42));
  38.         }
  39.         $rgb = array($r$g$b);
  40.         return $rgb;
  41.     }
  42.     private function desiredSize($o_w$o_h$n_w$n_h)
  43.     {
  44.         $n_r $n_w $n_h;
  45.         $o_r $o_w $o_h;
  46.         if ($n_r >= 1) {
  47.             if ($o_r >= 1) {
  48.                 if ($n_r >= $o_r) {
  49.                     $w $o_w / ($o_h $n_h);
  50.                     $h $n_h;
  51.                 } else {
  52.                     $w $n_w;
  53.                     $h $o_h / ($o_w $n_w);
  54.                 }
  55.             } else {
  56.                 $w $o_w / ($o_h $n_h);
  57.                 $h $n_h;
  58.             }
  59.         } else {
  60.             if ($o_r >= 1) {
  61.                 $w $n_w;
  62.                 $h $o_h / ($o_w $n_w);
  63.             } else {
  64.                 if ($n_r >= $o_r) {
  65.                     $w $o_w / ($o_h $n_h);
  66.                     $h $n_h;
  67.                 } else {
  68.                     $w $n_w;
  69.                     $h $o_h / ($o_w $n_w);
  70.                 }
  71.             }
  72.         }
  73.         return ['width' => $w'height' => $h];
  74.     }
  75.     public function resizeImage($source$target$newWidth$newheight$background 'transparent'$proportion 100.0)
  76.     {
  77.         if (!file_exists($source)) {
  78.             return false;
  79.         }
  80.         if (preg_match('/#([a-f0-9]{3}){1,2}\b/i'$background)) {
  81.             $background self::hex2RGB($background);
  82.         }
  83.         if ($proportion >= 10.0 && $proportion <= 100.0) {
  84.             $proportion /= 100.0;
  85.         } else {
  86.             $proportion 1.0;
  87.         }
  88.         $info getimagesize($source);
  89.         $mime $info['mime'];
  90.         switch ($mime) {
  91.             case 'image/jpeg':
  92.                 $imageCreateFunction 'imagecreatefromjpeg';
  93.                 $imageSaveFunction 'imagejpeg';
  94.                 $newImageExt 'jpg';
  95.                 break;
  96.             case 'image/png':
  97.                 $imageCreateFunction 'imagecreatefrompng';
  98.                 $imageSaveFunction 'imagepng';
  99.                 $newImageExt 'png';
  100.                 break;
  101.             case 'image/gif':
  102.                 $imageCreateFunction 'imagecreatefromgif';
  103.                 $imageSaveFunction 'imagegif';
  104.                 $newImageExt 'gif';
  105.                 break;
  106.             default:
  107.                 throw new Exception('Unknown image type.');
  108.         }
  109.         list($widthOrig$heightOrig) = getimagesize($source);
  110.         $size self::desiredSize($widthOrig$heightOrig$newWidth$newheight);
  111.         $new_width ceil($size['width'] * $proportion);
  112.         $new_height ceil($size['height'] * $proportion);
  113.         $x_mid $new_width 2;
  114.         $y_mid $new_height 2;
  115.         $myImage $imageCreateFunction($source);
  116.         $newImage imagecreatetruecolor($newWidth$newheight);
  117.         if ($background === 'transparent') {
  118.             imagesavealpha($newImagetrue);
  119.             $black imagecolorallocatealpha($newImage000127);
  120.             imagefill($newImage00$black);
  121.         } elseif (is_array($background) && count($background) === 3) {
  122.             $rgb $background;
  123.             $color imagecolorallocate($newImage$rgb[0], $rgb[1], $rgb[2]);
  124.             imagefill($newImage00$color);
  125.             imagealphablending($myImagefalse);
  126.             imagesavealpha($myImagetrue);
  127.         }
  128.         imagecopyresampled($newImage$myImage, (($newWidth 2)- ($x_mid)), (($newheight 2) - $y_mid), 00$new_width$new_height$widthOrig$heightOrig);
  129.         imagedestroy($myImage);
  130.         $imageSaveFunction($newImage"$target.$newImageExt");
  131.         return $newImage;
  132.     }
  133.     public function generateAppleImages()
  134.     {
  135.         foreach ($this->apple as $ap) {
  136.             $width $ap['width'];
  137.             $height $ap['height'];
  138.             $extensao $ap['ext'];
  139.             $fileName $ap['name'].'-'.$width.'x'.$height;
  140.             $background $ap['background'];
  141.             self::resizeImage($this->faviconFilePath$this->faviconDir.$fileName$width$height$background);
  142.         }
  143.         self::resizeImage($this->faviconFilePath$this->faviconDir.'apple-icon'192192$background);
  144.         self::resizeImage($this->faviconFilePath$this->faviconDir.'apple-icon-precomposed'192192$background);
  145.     }
  146.     public function generateAppleStartupImages($proportion)
  147.     {
  148.         foreach ($this->appleStartupScreens as $ap) {
  149.             $width $ap['width'];
  150.             $height $ap['height'];
  151.             $extensao $ap['ext'];
  152.             $fileName $ap['name'].'-'.$width.'x'.$height;
  153.             $background $ap['background'];
  154.             $proportion $proportion ?? $ap['proportion'];
  155.             self::resizeImage($this->appleFilePath$this->faviconDir.$fileName$width$height$background$proportion);
  156.         }
  157.     }
  158.     public function generateMsImages()
  159.     {
  160.         foreach ($this->ms as $m) {
  161.             $width $m['width'];
  162.             $height $m['height'];
  163.             $extensao $m['ext'];
  164.             $fileName $m['content'].'-'.$width.'x'.$height;
  165.             $background $m['background'];
  166.             self::resizeImage($this->faviconFilePath$this->faviconDir.$fileName$width$height$background);
  167.         }
  168.     }
  169.     public function generateAndroidImages()
  170.     {
  171.         foreach ($this->android as $a) {
  172.             $width $a['width'];
  173.             $height $a['height'];
  174.             $extensao $a['ext'];
  175.             $fileName $a['name'].'-'.$width.'x'.$height;
  176.             $background $a['background'];
  177.             self::resizeImage($this->faviconFilePath$this->faviconDir.$fileName$width$height$background);
  178.         }
  179.     }
  180.     private function generateManifestJson()
  181.     {
  182.         $protocol = isset($_SERVER['HTTPS']) && strcasecmp('off'$_SERVER['HTTPS']) !== "https" "http";
  183.         $hostname $_SERVER['HTTP_HOST'];
  184.         $path $this->faviconDirPath;
  185.         $pathWrite $this->faviconDir;
  186.         $manifest = [];
  187.         $manifest['name'] = $this->applicationName;
  188.         $manifest['short_name'] = $this->applicationName;
  189.         $manifest['dir'] = 'ltr';
  190. //        $manifest['lang'] = CURRENT_LANGUAGE;
  191.         $manifest['start_url'] = $protocol.'://'.$hostname;
  192.         $manifest['display'] = 'standalone';
  193.         $manifest['orientation'] = 'portrait';
  194.         $icons = [];
  195.         foreach ($this->android as $a) {
  196.             if (isset($a['density'])) {
  197.                 $icon = [];
  198.                 $width $a['width'];
  199.                 $height $a['height'];
  200.                 $sizes $width.'x'.$height;
  201.                 $ext $a['ext'];
  202.                 $density $a['density'];
  203.                 $fileName $a['name'].'-'.$sizes.'.'.$ext;
  204.                 $type $a['type'].'/'.$ext;
  205.                 $icon['src'] = '/'.$path.$fileName;
  206.                 $icon['sizes'] = $sizes;
  207.                 $icon['type'] = $type;
  208.                 $icon['density'] = $density;
  209.                 $icons[] = $icon;
  210.             }
  211.         }
  212.         $manifest['icons'] = $icons;
  213.         $fp fopen($pathWrite.'manifest.json''w');
  214.         fwrite($fpjson_encode($manifest));
  215.         fclose($fp);
  216.     }
  217.     public function setAppleStartupImageProportion($appleStartupImageProportion)
  218.     {
  219.         $this->appleStartupImageProportion $appleStartupImageProportion;
  220.     }
  221.     public function getAppleStartupImageProportion ()
  222.     {
  223.         return $this->appleStartupImageProportion;
  224.     }
  225. }