├── .scrutinizer.yml ├── .styleci.yml ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── favicon.php ├── resources └── fonts │ └── OpenSans-Regular.ttf └── src ├── Favicon.php ├── FaviconServiceProvider.php ├── Generators ├── EnvironmentGenerator.php └── FaviconGenerator.php ├── Http └── Controllers │ └── FaviconController.php └── helpers.php /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | filter: 2 | excluded_paths: [tests/*] 3 | 4 | checks: 5 | php: 6 | remove_extra_empty_lines: true 7 | remove_php_closing_tag: true 8 | remove_trailing_whitespace: true 9 | fix_use_statements: 10 | remove_unused: true 11 | preserve_multiple: false 12 | preserve_blanklines: true 13 | order_alphabetically: true 14 | fix_php_opening_tag: true 15 | fix_linefeed: true 16 | fix_line_ending: true 17 | fix_identation_4spaces: true 18 | fix_doc_comments: true 19 | 20 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.1 5 | - 7.2 6 | - 7.3 7 | - 7.4 8 | - 8.0 9 | 10 | env: 11 | global: 12 | - ILLUMINATE_VERSION="" 13 | - TESTBENCH_VERSION="" 14 | 15 | matrix: 16 | - COMPOSER_FLAGS="--prefer-lowest" 17 | - COMPOSER_FLAGS="" 18 | - ILLUMINATE_VERSION="5.7.*" TESTBENCH_VERSION="3.7.*" 19 | - ILLUMINATE_VERSION="5.8.*" TESTBENCH_VERSION="3.8.*" 20 | - ILLUMINATE_VERSION="6.*" TESTBENCH_VERSION="4.*" 21 | 22 | matrix: 23 | exclude: 24 | - php: 7.1 25 | env: ILLUMINATE_VERSION="6.*" TESTBENCH_VERSION="4.*" 26 | 27 | before_script: 28 | - travis_retry composer self-update 29 | - if [ "$ILLUMINATE_VERSION" != "" ] && [ "$TESTBENCH_VERSION" != "" ]; then 30 | composer require "illuminate/http:${ILLUMINATE_VERSION}" --no-update; 31 | composer require "illuminate/support:${ILLUMINATE_VERSION}" --no-update; 32 | composer require "orchestra/testbench:${TESTBENCH_VERSION}" --no-update; 33 | fi 34 | - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source 35 | 36 | script: 37 | - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover 38 | 39 | after_script: 40 | - php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover 41 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-favicon` will be documented in this file 4 | 5 | ## 1.0.0 - 201X-XX-XX 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Beyond Code GmbH 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 | # Laravel Favicon 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-favicon.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-favicon) 4 | [![Build Status](https://img.shields.io/travis/beyondcode/laravel-favicon/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-favicon) 5 | [![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-favicon.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-favicon) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-favicon.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-favicon) 7 | 8 | Create dynamic favicons based on your environment settings. 9 | 10 | ![](https://beyondco.de/github/favicons/screenshot.png) 11 | 12 | ## Laravel Package Development 13 | 14 | [![https://phppackagedevelopment.com](https://beyondco.de/courses/phppd.jpg)](https://phppackagedevelopment.com) 15 | 16 | If you want to learn how to create reusable PHP packages yourself, take a look at my upcoming [PHP Package Development](https://phppackagedevelopment.com) video course. 17 | 18 | ## Installation 19 | 20 | You can install the package via composer: 21 | 22 | ```bash 23 | composer require beyondcode/laravel-favicon 24 | ``` 25 | 26 | The service provider for this package will be automatically registered for you. 27 | 28 | ### Compatibility 29 | 30 | | | 1.0 | 1.1 | 1.2 | 1.3 | 1.4 | 1.5 | 1.6 | 1.7 | 31 | | ----- | --- | --- | --- | --- | --- | --- | --- | --- | 32 | | 5.6.x | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 33 | | 5.7.x | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 34 | | 5.8.x | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 35 | | 6.x | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 36 | | 7.x | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 37 | | 8.x | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | 38 | | 9.x | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | 39 | | 10.x | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | 40 | | 11.x | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | 41 | | 12.x | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | 42 | 43 | ## Usage 44 | 45 | To make use of this package, make use of the `favicon` helper function that this package provides. 46 | 47 | You can simply wrap the function around your favicon icon names, like this: 48 | 49 | ```html 50 | 51 | 52 | 53 | ``` 54 | 55 | ### Customization 56 | 57 | You can completely customize which environments you want to have enabled for the favicon generation, as well as the font and colors that will be used. 58 | 59 | To modify the default values, publish the package configuration file using: 60 | 61 | ``` 62 | php artisan vendor:publish --provider='BeyondCode\LaravelFavicon\FaviconServiceProvider' --tag='config' 63 | ``` 64 | 65 | This will publish the `config/favicon.php` file. 66 | 67 | This is what the default content looks like: 68 | 69 | ```php 70 | return [ 71 | 72 | /* 73 | * The list of enabled environments for the dynamic favicon 74 | * generation. You can specify the text to display as well 75 | * as the font and background color for the text. 76 | * 77 | * If no background color is specified, the text will be 78 | * on a transparent background. 79 | */ 80 | 'enabled_environments' => [ 81 | 'local' => [ 82 | 'text' => 'DEV', 83 | 'color' => '#000000', 84 | 'background_color' => '#ffffff', 85 | ], 86 | ], 87 | 88 | /* 89 | * The dynamic favicon text padding to apply. 90 | */ 91 | 'padding' => [ 92 | 'x' => 2, 93 | 'y' => 2, 94 | ], 95 | 96 | /* 97 | * The font file to use for the dynamic favicon generation. 98 | * The default value will use OpenSans Regular. 99 | */ 100 | 'font' => null, 101 | 102 | /* 103 | * Intervention Image supports "GD Library" and "Imagick" to process images 104 | * internally. You may choose one of them according to your PHP 105 | * configuration. By default, PHP's "GD Library" implementation is used. 106 | * 107 | * If you want to convert ICO files, you need to use imagick. 108 | * 109 | * Supported: "gd", "imagick" 110 | * 111 | */ 112 | 'image_driver' => 'gd', 113 | 114 | /* 115 | * The prefix to use for the dynamic favicon URL. 116 | */ 117 | 'url_prefix' => 'laravel-favicon', 118 | 119 | /* 120 | * The favicon generator class to use. The default generator 121 | * makes use of the environment settings defined in this file. 122 | * But you can create your own favicon generator if you want. 123 | */ 124 | 'generator' => \BeyondCode\LaravelFavicon\Generators\EnvironmentGenerator::class, 125 | 126 | 127 | ]; 128 | ``` 129 | 130 | Modify the settings to suit your needs. 131 | 132 | ## Custom generator 133 | 134 | The default favicon generator will write the text on the bottom-right corner of your favicon, in the desired color, font and background-color. 135 | If you want to generate a completely custom favicon, you can create your own FaviconGenerator implementation class and set it in the configuration file. 136 | 137 | This is the interface that the generator should implement: 138 | 139 | ```php 140 | interface FaviconGenerator 141 | { 142 | public function generate(string $icon): Response; 143 | 144 | public function shouldGenerateFavicon(): bool; 145 | } 146 | ``` 147 | 148 | The `generate` method receives the icon url/filename and expects you to return an illuminate HTTP response. 149 | 150 | The `shouldGenerateFavicon` method can be used to determine if a custom favicon should get generated. 151 | 152 | ## FAQ 153 | 154 | ### My ICO files are not working, why? 155 | 156 | In order to modify ICO files, you need the Imagick PHP library installed and enabled in your `config/favicon.php` file. 157 | 158 | ### Is there a performance impact when I'm using this package? 159 | 160 | No - the default generator only modifies your favicon when the specified environment is enabled. This means, that production environments only see the static assets that you already have. 161 | 162 | ## Changelog 163 | 164 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 165 | 166 | ## Contributing 167 | 168 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 169 | 170 | ## Security 171 | 172 | If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker. 173 | 174 | ## Credits 175 | 176 | - [Marcel Pociot](https://github.com/mpociot) 177 | - [All Contributors](../../contributors) 178 | 179 | ## License 180 | 181 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 182 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "beyondcode/laravel-favicon", 3 | "description": "Create dynamic favicons based on your environment settings.", 4 | "keywords": [ 5 | "beyondcode", 6 | "laravel-favicon" 7 | ], 8 | "homepage": "https://github.com/beyondcode/laravel-favicon", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Marcel Pociot", 13 | "email": "marcel@beyondco.de", 14 | "homepage": "https://beyondcode.de", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^7.1|^8.0|^8.1|^8.2|^8.3", 20 | "illuminate/http": "5.6.*|5.7.*|5.8.*|6.*|7.*|8.*|9.*|^10.0|^11.0|^12.0", 21 | "illuminate/support": "5.6.*|5.7.*|5.8.*|6.*|7.*|8.*|9.*|^10.0|^11.0|^12.0", 22 | "intervention/image": "^2.4" 23 | }, 24 | "require-dev": { 25 | "phpunit/phpunit": "^7.0|^8.0|^9.0|^10.0|^11.0", 26 | "orchestra/testbench": "^3.7|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "BeyondCode\\LaravelFavicon\\": "src" 31 | }, 32 | "files": [ 33 | "src/helpers.php" 34 | ] 35 | }, 36 | "autoload-dev": { 37 | "psr-4": { 38 | "BeyondCode\\LaravelFavicon\\Tests\\": "tests" 39 | } 40 | }, 41 | "scripts": { 42 | "test": "vendor/bin/phpunit", 43 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 44 | 45 | }, 46 | "config": { 47 | "sort-packages": true 48 | }, 49 | "extra": { 50 | "laravel": { 51 | "providers": [ 52 | "BeyondCode\\LaravelFavicon\\FaviconServiceProvider" 53 | ] 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /config/favicon.php: -------------------------------------------------------------------------------- 1 | [ 14 | 'local' => [ 15 | 'text' => 'DEV', 16 | 'color' => '#000000', 17 | 'background_color' => '#ffffff', 18 | ], 19 | ], 20 | 21 | /* 22 | * The dynamic favicon text padding to apply. 23 | */ 24 | 'padding' => [ 25 | 'x' => 2, 26 | 'y' => 2, 27 | ], 28 | 29 | /* 30 | * The font file to use for the dynamic favicon generation. 31 | * The default value will use OpenSans Regular. 32 | */ 33 | 'font' => null, 34 | 35 | /* 36 | * Intervention Image supports "GD Library" and "Imagick" to process images 37 | * internally. You may choose one of them according to your PHP 38 | * configuration. By default, PHP's "GD Library" implementation is used. 39 | * 40 | * If you want to convert ICO files, you need to use imagick. 41 | * 42 | * Supported: "gd", "imagick" 43 | * 44 | */ 45 | 'image_driver' => 'gd', 46 | 47 | /* 48 | * The prefix to use for the dynamic favicon URL. 49 | */ 50 | 'url_prefix' => 'laravel-favicon', 51 | 52 | /* 53 | * The favicon generator class to use. The default generator 54 | * makes use of the environment settings defined in this file. 55 | * But you can create your own favicon generator if you want. 56 | */ 57 | 'generator' => \BeyondCode\LaravelFavicon\Generators\EnvironmentGenerator::class, 58 | 59 | ]; 60 | -------------------------------------------------------------------------------- /resources/fonts/OpenSans-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beyondcode/laravel-favicon/c6299f295a0386052e6419069489dd535aed6fda/resources/fonts/OpenSans-Regular.ttf -------------------------------------------------------------------------------- /src/Favicon.php: -------------------------------------------------------------------------------- 1 | config = $config; 15 | } 16 | 17 | public function getFaviconText(string $environment) 18 | { 19 | return Arr::get($this->config, 'enabled_environments.'.$environment.'.text'); 20 | } 21 | 22 | public function getFaviconColor(string $environment) 23 | { 24 | return Arr::get($this->config, 'enabled_environments.'.$environment.'.color'); 25 | } 26 | 27 | public function getFaviconBackgroundColor(string $environment) 28 | { 29 | return Arr::get($this->config, 'enabled_environments.'.$environment.'.background_color'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/FaviconServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 18 | $this->publishes([ 19 | __DIR__.'/../config/favicon.php' => config_path('favicon.php'), 20 | ], 'config'); 21 | } 22 | 23 | $this->registerRoutes(); 24 | } 25 | 26 | /** 27 | * Register the application services. 28 | */ 29 | public function register() 30 | { 31 | $this->mergeConfigFrom(__DIR__.'/../config/favicon.php', 'favicon'); 32 | 33 | $this->app->singleton(Favicon::class, function () { 34 | return new Favicon(config('favicon')); 35 | }); 36 | 37 | $this->app->bind(FaviconGenerator::class, function () { 38 | return app(config('favicon.generator')); 39 | }); 40 | } 41 | 42 | protected function registerRoutes() 43 | { 44 | Route::get(config('favicon.url_prefix').'/{icon}', FaviconController::class) 45 | ->where('icon', '.*'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Generators/EnvironmentGenerator.php: -------------------------------------------------------------------------------- 1 | favicon = $favicon; 27 | 28 | $this->manager = new ImageManager([ 29 | 'driver' => config('favicon.image_driver'), 30 | ]); 31 | 32 | $this->environment = config('app.env'); 33 | } 34 | 35 | public function generate(string $icon): Response 36 | { 37 | $paddingX = config('favicon.padding.x'); 38 | $paddingY = config('favicon.padding.y'); 39 | 40 | $img = $this->manager->make($icon); 41 | 42 | $font = $this->getFont($this->favicon->getFaviconText($this->environment)); 43 | 44 | $font->file(config('favicon.font') ?? __DIR__.'/../../resources/fonts/OpenSans-Regular.ttf'); 45 | 46 | $font->valign('top'); 47 | 48 | $font->color($this->favicon->getFaviconColor($this->environment)); 49 | 50 | $this->calculateDynamicFontSize($font, $img, $paddingX); 51 | 52 | $environmentTextImage = $this->createEnvironmentTextImage($font); 53 | 54 | $output = $this->manager->canvas($img->width(), $img->height()); 55 | 56 | $output->insert($img); 57 | 58 | $output->insert($environmentTextImage, 'bottom-right', $paddingX, $paddingY); 59 | 60 | return $output->response('png'); 61 | } 62 | 63 | protected function getFont(string $text): AbstractFont 64 | { 65 | if (config('favicon.image_driver') === 'imagick') { 66 | return new ImagickFont($text); 67 | } 68 | 69 | return new GdFont($text); 70 | } 71 | 72 | protected function calculateDynamicFontSize(AbstractFont $font, Image $icon, $paddingX) 73 | { 74 | $size = $font->getBoxSize(); 75 | 76 | while ($size['width'] + $paddingX > $icon->width() && $font->getSize() > 0) { 77 | $fontSize = $font->getSize(); 78 | 79 | $font->size($fontSize - 1); 80 | 81 | $size = $font->getBoxSize(); 82 | } 83 | } 84 | 85 | protected function createEnvironmentTextImage(AbstractFont $font) 86 | { 87 | $size = $font->getBoxSize(); 88 | 89 | $environmentText = $this->manager->canvas($size['width'], $size['height'], $this->favicon->getFaviconBackgroundColor($this->environment)); 90 | 91 | $font->applyToImage($environmentText); 92 | 93 | return $environmentText; 94 | } 95 | 96 | public function shouldGenerateFavicon(): bool 97 | { 98 | return config('favicon.enabled_environments.'.$this->environment) !== null; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/Generators/FaviconGenerator.php: -------------------------------------------------------------------------------- 1 | generate($request->route('icon')); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | shouldGenerateFavicon(app()->environment())) { 9 | return rtrim(config('app.url'), '/').'/'.config('favicon.url_prefix').'/'.trim($image, '/'); 10 | } 11 | 12 | return $image; 13 | } 14 | } 15 | --------------------------------------------------------------------------------