├── .github └── FUNDING.yml ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── composer.lock ├── config └── favicon-generator.php ├── resources └── views │ └── .gitkeep └── src ├── Commands └── FaviconGeneratorCommand.php ├── Facades └── FaviconGenerator.php ├── FaviconGenerator.php └── FaviconGeneratorServiceProvider.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: magarrent # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 13 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `favicon-generator` will be documented in this file. 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) magarrent 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Generate favicons from Images 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/magarrent/laravel-favicon-generator.svg?style=flat-square)](https://packagist.org/packages/magarrent/laravel-favicon-generator) 4 | 5 | ## Installation 6 | 7 | ``` 8 | Requires PHP Imagick 9 | ``` 10 | 11 | You can install the package via composer: 12 | 13 | ```bash 14 | composer require magarrent/favicon-generator 15 | ``` 16 | 17 | 18 | ## Usage 19 | 20 | To use your own image and convert it to the different favicons versions, use these methods. 21 | This will generate a "favicon" folder to your public_path with the different favicons formats and versions. 22 | Will also generate some XML and webmanifest according to the web standards. 23 | 24 | --- 25 | Use this methods in your own scenario, when you upload the image, or just a console command, whatever. 26 | Just write your input path and optionally the public output path. 27 | If you don't specify the output (2nd param), the favicons will be saved in ``public/favicons`` 28 | 29 | ```php 30 | $favicon = new FaviconGenerator('path-to-your-image', 'optional-public-dist-path'); 31 | $favicon->generateFaviconsFromImagePath(); 32 | ``` 33 | 34 | Once you generated the icons, just write these line into your ```` section of your **HTML** 35 | 36 | ```php 37 | {!! \Magarrent\FaviconGenerator\FaviconGenerator::generateHtmlMetaIcons() !!} 38 | ``` 39 | 40 | ## Testing 41 | 42 | ```bash 43 | composer test 44 | ``` 45 | 46 | ## Changelog 47 | 48 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 49 | 50 | ## Roadmap 51 | 52 | - Tests 53 | - Generate icons from Image class, not from String path 54 | 55 | ## Contributing 56 | 57 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 58 | 59 | ## Credits 60 | 61 | - [Marc Garcia Torrent](https://github.com/magarrent) 62 | - [All Contributors](../../contributors) 63 | 64 | ## License 65 | 66 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 67 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "magarrent/laravel-favicon-generator", 3 | "description": "Generate and render favicons from Images", 4 | "keywords": [ 5 | "magarrent", 6 | "laravel", 7 | "favicon-generator" 8 | ], 9 | "homepage": "https://github.com/magarrent/favicon-generator", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Marc Garcia Torrent", 14 | "email": "magarrent@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.1", 20 | "spatie/laravel-package-tools": "^1.13.0", 21 | "intervention/image": "^2.7", 22 | "illuminate/contracts": "^9.0" 23 | }, 24 | "require-dev": { 25 | "laravel/pint": "^1.0", 26 | "nunomaduro/collision": "^6.0", 27 | "nunomaduro/larastan": "^2.0.1", 28 | "orchestra/testbench": "^7.0", 29 | "pestphp/pest": "^1.21", 30 | "pestphp/pest-plugin-laravel": "^1.1", 31 | "phpstan/extension-installer": "^1.1", 32 | "phpstan/phpstan-deprecation-rules": "^1.0", 33 | "phpstan/phpstan-phpunit": "^1.0", 34 | "phpunit/phpunit": "^9.5" 35 | }, 36 | "autoload": { 37 | "psr-4": { 38 | "Magarrent\\FaviconGenerator\\": "src", 39 | "Magarrent\\FaviconGenerator\\Database\\Factories\\": "database/factories" 40 | } 41 | }, 42 | "autoload-dev": { 43 | "psr-4": { 44 | "Magarrent\\FaviconGenerator\\Tests\\": "tests" 45 | } 46 | }, 47 | "scripts": { 48 | "post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi", 49 | "analyse": "vendor/bin/phpstan analyse", 50 | "test": "vendor/bin/pest", 51 | "test-coverage": "vendor/bin/pest --coverage", 52 | "format": "vendor/bin/pint" 53 | }, 54 | "config": { 55 | "sort-packages": true, 56 | "allow-plugins": { 57 | "pestphp/pest-plugin": true, 58 | "phpstan/extension-installer": true 59 | } 60 | }, 61 | "extra": { 62 | "laravel": { 63 | "providers": [ 64 | "Magarrent\\FaviconGenerator\\FaviconGeneratorServiceProvider" 65 | ], 66 | "aliases": { 67 | "FaviconGenerator": "Magarrent\\FaviconGenerator\\Facades\\FaviconGenerator" 68 | } 69 | } 70 | }, 71 | "minimum-stability": "dev", 72 | "prefer-stable": true 73 | } 74 | -------------------------------------------------------------------------------- /config/favicon-generator.php: -------------------------------------------------------------------------------- 1 | comment('All done'); 16 | 17 | return self::SUCCESS; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Facades/FaviconGenerator.php: -------------------------------------------------------------------------------- 1 | publicPath))) { 19 | mkdir(public_path($this->publicPath), 0755, true); 20 | } 21 | $this->distPath = public_path($this->publicPath); 22 | } 23 | 24 | public function generateFaviconsFromImagePath() { 25 | // create an image manager instance with imagick driver 26 | Image::configure(['driver' => 'imagick']); 27 | 28 | Image::make($this->filePath)->resize(192, 192)->save($this->distPath . "/android-chrome-192x192.png", '100', 'png'); 29 | Image::make($this->filePath)->resize(512, 512)->save($this->distPath . "/android-chrome-512x512.png", '100', 'png'); 30 | Image::make($this->filePath)->resize(180, 180)->save($this->distPath . "/apple-touch-icon.png", '100', 'png'); 31 | Image::make($this->filePath)->resize(16, 16)->save($this->distPath . "/favicon-16x16.png", '100', 'png'); 32 | Image::make($this->filePath)->resize(32, 32)->save($this->distPath . "/favicon-32x32.png", '100', 'png'); 33 | Image::make($this->filePath)->resize(150, 150)->save($this->distPath . "/mstile-150x150.png", '100', 'png'); 34 | 35 | // favicon.ico 36 | $icon = new \Imagick(); 37 | $icon->addImage(new \Imagick($this->distPath . "/favicon-16x16.png")); 38 | $icon->addImage(new \Imagick($this->distPath . "/favicon-32x32.png")); 39 | $icon->setResolution(16,16); 40 | $icon->writeImages($this->distPath . "/favicon.ico", true); 41 | 42 | $this->saveBrowserConfigXml(); 43 | $this->saveSiteWebManifest(); 44 | } 45 | 46 | public function saveBrowserConfigXml(): void 47 | { 48 | $xml = ' 49 | 50 | 51 | 52 | 53 | #FFFFFF 54 | 55 | 56 | '; 57 | 58 | 59 | $xmlFile = fopen("{$this->distPath}/browserconfig.xml", "w") or die("Unable to open file!"); 60 | fwrite($xmlFile, $xml); 61 | fclose($xmlFile); 62 | } 63 | 64 | public function saveSiteWebManifest(): void 65 | { 66 | $json = '{ 67 | "name": "", 68 | "short_name": "", 69 | "icons": [ 70 | { 71 | "src": "/' . $this->publicPath . '/android-chrome-192x192.png", 72 | "sizes": "192x192", 73 | "type": "image/png" 74 | }, 75 | { 76 | "src": "/' . $this->publicPath . '/android-chrome-512x512.png", 77 | "sizes": "512x512", 78 | "type": "image/png" 79 | } 80 | ], 81 | "theme_color": "#ffffff", 82 | "background_color": "#ffffff", 83 | "display": "standalone" 84 | }'; 85 | 86 | 87 | $jsonFile = fopen("{$this->distPath}/site.webmanifest", "w") or die("Unable to open file!"); 88 | fwrite($jsonFile, $json); 89 | fclose($jsonFile); 90 | } 91 | 92 | /** 93 | * @param String $publicPath 94 | * @return string 95 | */ 96 | public static function generateHtmlMetaIcons(String $publicPath = 'favicon'): string 97 | { 98 | $html = ' 99 | 100 | 101 | 102 | 103 | 104 | 105 | '; 106 | 107 | return $html; 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /src/FaviconGeneratorServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('favicon-generator') 20 | ->hasConfigFile() 21 | ->hasCommand(FaviconGeneratorCommand::class); 22 | } 23 | } 24 | --------------------------------------------------------------------------------