├── .gitignore ├── LICENSE ├── composer.json ├── readme.md └── src ├── Base.php ├── Facades └── ImageFaker.php ├── ImageFaker.php ├── ImageFakerServiceProvider.php ├── ImageFakerTrait.php └── Services ├── FakePeople.php ├── Kittens.php ├── LoremFlickr.php ├── Picsum.php └── PlaceDog.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.phpunit.cache 2 | .phpunit.result.cache 3 | /.fleet 4 | /.idea 5 | /.vscode 6 | error_log 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Alireza Sedghi 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alirezasedghi/laravel-image-faker", 3 | "description": "A library to generate fake images for Laravel", 4 | "keywords": [ 5 | "PHP", 6 | "laravel", 7 | "laravel-faker", 8 | "image-faker", 9 | "image", 10 | "faker", 11 | "random-image", 12 | "PHP" 13 | ], 14 | "homepage": "https://github.com/AlirezaSedghi/laravel-image-faker", 15 | "type": "laravel", 16 | "license": "MIT", 17 | "authors": [ 18 | { 19 | "name": "Alireza Sedghi", 20 | "email": "alirezasedghi911@gmail.com" 21 | } 22 | ], 23 | "require": { 24 | "php": ">=8.0.0" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "Alirezasedghi\\LaravelImageFaker\\": "src" 29 | } 30 | }, 31 | "extra": { 32 | "laravel": { 33 | "providers": [ 34 | "Alirezasedghi\\LaravelImageFaker\\ImageFakerServiceProvider" 35 | ], 36 | "aliases": { 37 | "ImageFaker": "Alirezasedghi\\LaravelImageFaker\\Facades\\ImageFaker" 38 | } 39 | } 40 | }, 41 | "minimum-stability": "dev", 42 | "prefer-stable": true 43 | } 44 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |

Laravel Image Faker

2 | 3 |

4 | Required PHP Version 5 | Total Downloads 6 | Latest Stable Version 7 | Latest Stable Version 8 | License 9 | GitHub issues 10 |

11 | 12 | ## Description 13 | Laravel Image Faker is an alternative image provider for [FakerPHP](https://github.com/FakerPHP/Faker). 14 | 15 | ## Installation 16 | ```bash 17 | composer require alirezasedghi/laravel-image-faker 18 | ``` 19 | 20 | ## Resources 21 | The following sources are utilized by this project to create random images: 22 | 23 | - [Lorem Picsum](https://picsum.photos/) 24 | - [LoremFlickr](https://loremflickr.com/) 25 | - [PlaceDog](https://placedog.net/) 26 | - [Kittens (Random Cats) by TheOldReader.com](https://theoldreader.com/kittens/) 27 | - [Fake People by BoredHumans.com](https://boredhumans.com/faces.php) 28 | 29 | ## Methods 30 | | Code | Description | 31 | |----------------------------------|--------------------------------------------------------------------------------| 32 | | ``` (new ImageFaker(new Service()))->imageUrl() ``` | Return a random image url from the specified service | 33 | | ``` (new ImageFaker(new Service()))->image() ``` | Download a random image from the specified service | 34 | 35 | ## Usage 36 | ```php 37 | /** 38 | * Define the model's default state. 39 | * 40 | * @return array 41 | */ 42 | public function definition(): array 43 | { 44 | /** 45 | * In order to utilize other services, the following substitutes can be used: 46 | * - new ImageFaker(new LoremFlickr()); 47 | * - new ImageFaker(new PlaceDog()); 48 | * - new ImageFaker(new Kittens()); 49 | * - new ImageFaker(new FakePeople()); 50 | */ 51 | $imageFaker = new ImageFaker(new Picsum()); 52 | 53 | return [ 54 | 'title' => $this->faker->sentence(), 55 | 'content' => $this->faker->paragraph(), 56 | 'attachments' => $imageFaker->image( storage_path("app/attachments/") ) 57 | ]; 58 | } 59 | ``` 60 | 61 | ## Contributing 62 | Don't hesitate to send a PR if you're looking for a service that's not available in this package. 😁 63 | 64 | ## License 65 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 66 | -------------------------------------------------------------------------------- /src/Base.php: -------------------------------------------------------------------------------- 1 | base_url) || (static::class === "Alirezasedghi\LaravelImageFaker\Base") ) { 39 | throw new \InvalidArgumentException("Invalid service call. Don't call Base Service directly."); 40 | } 41 | 42 | $imageOptions = []; 43 | if ($grayscale === true) { 44 | $imageOptions["grayscale"] = true; 45 | } 46 | if ($blur) { 47 | $imageOptions["blur"] = ($blur === true) ? true : $blur; 48 | } 49 | 50 | return $this->makeUrl($width, $height, $imageOptions); 51 | } 52 | 53 | /** 54 | * Download a remote random image to disk and return its location 55 | * 56 | * Requires curl, or allow_url_fopen to be on in php.ini. 57 | * 58 | * @return bool|string 59 | */ 60 | public function image(string $dir = null, int $width = 640, int $height = 480, bool $fullPath = true, bool $grayscale = false, bool|int $blur = false) 61 | { 62 | if ( empty($this->base_url) || (static::class === "Alirezasedghi\LaravelImageFaker\Base") ) { 63 | throw new \InvalidArgumentException("Invalid service call. Don't call Base Service directly."); 64 | } 65 | 66 | $dir = null === $dir ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible 67 | 68 | // Validate directory path 69 | if (!is_dir($dir) || !is_writable($dir)) { 70 | throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir)); 71 | } 72 | 73 | // Generate a random filename. Use the server address so that a file 74 | // Generated at the same time on a different server won't have a collision. 75 | $name = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true)); 76 | $filename = sprintf('%s.%s', $name, $this->default_format); 77 | $filepath = $dir . DIRECTORY_SEPARATOR . $filename; 78 | 79 | $url = static::imageUrl($width, $height, $grayscale, $blur); 80 | 81 | // Save file 82 | if (function_exists('curl_exec')) { 83 | // Use cURL 84 | $fp = fopen($filepath, 'w'); 85 | $ch = curl_init($url); 86 | curl_setopt($ch, CURLOPT_FILE, $fp); 87 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 88 | curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent); 89 | curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); 90 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cache-Control: no-cache")); 91 | if ( $this->SSL_verify_peer === false ) 92 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 93 | $success = curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200; 94 | fclose($fp); 95 | curl_close($ch); 96 | 97 | if (!$success) { 98 | unlink($filepath); 99 | 100 | // Could not contact the distant URL or HTTP error - fail silently. 101 | return false; 102 | } 103 | } elseif (ini_get('allow_url_fopen')) { 104 | // Use remote fopen() via copy() 105 | $success = copy($url, $filepath); 106 | 107 | if (!$success) { 108 | // could not contact the distant URL or HTTP error - fail silently. 109 | return false; 110 | } 111 | } else { 112 | return new \RuntimeException('The image formatter downloads an image from a remote HTTP server. Therefore, it requires that PHP can request remote hosts, either via cURL or fopen()'); 113 | } 114 | 115 | return $fullPath ? $filepath : $filename; 116 | } 117 | 118 | /** 119 | * Make url to get images 120 | * 121 | * @param int $width 122 | * @param int $height 123 | * @param array $imageOptions 124 | * @return string 125 | */ 126 | protected function makeUrl(int $width = 640, int $height = 480, array $imageOptions = []) { 127 | return sprintf( 128 | '%s/%s/%s%s', 129 | $this->base_url, 130 | $width, 131 | $height, 132 | !empty($imageOptions) ? preg_replace('/\b\=1\b/', '', '?' . http_build_query($imageOptions)) : '' 133 | ); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/Facades/ImageFaker.php: -------------------------------------------------------------------------------- 1 | service = $service; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/ImageFakerServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->alias(ImageFaker::class, 'ImageFaker'); 15 | 16 | $this->app->bind('ImageFaker', function ($app) { // Base $service 17 | return new ImageFaker($app); 18 | }); 19 | } 20 | 21 | /** 22 | * Bootstrap any application services. 23 | */ 24 | public function boot() 25 | { 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/ImageFakerTrait.php: -------------------------------------------------------------------------------- 1 | service->imageUrl($width, $height, $grayscale, $blur); 19 | } 20 | 21 | /** 22 | * Download a remote random image to disk and return its location 23 | * 24 | * @return bool|string 25 | */ 26 | public function image(string $dir = null, int $width = 640, int $height = 480, bool $fullPath = true, bool $grayscale = false, bool|int $blur = false) { 27 | return $this->service->image($dir, $width, $height, $fullPath, $grayscale, $blur); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Services/FakePeople.php: -------------------------------------------------------------------------------- 1 | base_url, 37 | mt_rand(1, 994) 38 | ); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Services/Kittens.php: -------------------------------------------------------------------------------- 1 | base_url, 30 | $width, 31 | $height 32 | ); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Services/LoremFlickr.php: -------------------------------------------------------------------------------- 1 | base_url, 30 | $is_grayscale ? 'g/' : '', 31 | $width, 32 | $height, 33 | $is_grayscale ? '/random' : '', 34 | !empty($imageOptions) ? preg_replace('/\b\=1\b/', '', '?' . http_build_query($imageOptions)) : '' 35 | ); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Services/Picsum.php: -------------------------------------------------------------------------------- 1 | base_url, 36 | $width, 37 | $height, 38 | $is_grayscale ? '/g' : '', 39 | $is_blur ? '/b' : '', 40 | !empty($imageOptions) ? preg_replace('/\b\=1\b/', '', '?' . http_build_query($imageOptions)) : '' 41 | ); 42 | } 43 | } 44 | --------------------------------------------------------------------------------