Skip to content

Commit 5950c70

Browse files
committed
v0.9 Beta
Add package to Github
1 parent 14d84ae commit 5950c70

12 files changed

+470
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/.phpunit.cache
2+
.phpunit.result.cache
3+
/.fleet
4+
/.idea
5+
/.vscode
6+
error_log

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "alirezasedghi/laravel-image-faker",
3+
"description": "A library to generate fake images for Laravel",
4+
"type": "laravel",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Alireza Sedghi",
9+
"email": "alirezasedghi911@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"php": ">=8.0.0"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Alirezasedghi\\LaravelImageFaker\\": "src"
18+
}
19+
},
20+
"extra": {
21+
"laravel": {
22+
"providers": [
23+
"Alirezasedghi\\LaravelImageFaker\\ImageFakerServiceProvider"
24+
],
25+
"aliases": {
26+
"ImageFaker": "Alirezasedghi\\LaravelImageFaker\\Facades\\ImageFaker"
27+
}
28+
}
29+
},
30+
"minimum-stability": "dev",
31+
"prefer-stable": true
32+
}

readme.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<h1 align="center">Laravel Image Faker</h1>
2+
3+
<p align="center">
4+
<a href="" rel="nofollow"><img alt="Required PHP Version" src="https://img.shields.io/badge/php->=8.0.0-blue?style=flat-square"></a>
5+
<a href="https://packagist.org/packages/alirezasedghi/laravel-image-faker" rel="nofollow"><img alt="Total Downloads" src="https://poser.pugx.org/alirezasedghi/laravel-image-faker/downloads?style=flat-square"></a>
6+
<a href="https://packagist.org/packages/alirezasedghi/laravel-image-faker" rel="nofollow"><img alt="Latest Stable Version" src="https://poser.pugx.org/alirezasedghi/laravel-image-faker/v/stable?style=flat-square"></a>
7+
<a href="https://raw.githubusercontent.com/AlirezaSedghi/laravel-image-faker/master/LICENSE" rel="nofollow"><img alt="License" src="https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square"></a>
8+
<a href="https://github.com/AlirezaSedghi/laravel-image-faker/issues" rel="nofollow"><img alt="GitHub issues" src="https://img.shields.io/github/issues/AlirezaSedghi/laravel-image-faker.svg?style=flat-square"></a>
9+
</p>
10+
11+
## Description
12+
Laravel Image Faker is an alternative image provider for [FakerPHP](https://github.com/FakerPHP/Faker).
13+
14+
## Installation
15+
```bash
16+
composer require alirezasedghi/laravel-image-faker
17+
```
18+
19+
## Resources
20+
The following sources are utilized by this project to create random images:
21+
22+
- [Lorem Picsum](https://picsum.photos/)
23+
- [LoremFlickr](https://loremflickr.com/)
24+
- [PlaceDog](https://placedog.net/)
25+
- [Fake People by BoredHumans.com](https://boredhumans.com/faces.php)
26+
27+
## Methods
28+
| Code | Description |
29+
|----------------------------------|--------------------------------------------------------------------------------|
30+
| ``` (new ImageFaker(new Serivce()))->imageUrl() ``` | Return a random image url from the specified service |
31+
| ``` (new ImageFaker(new Serivce()))->image() ``` | Download a random image from the specified service |
32+
33+
## Usage
34+
```php
35+
/**
36+
* Define the model's default state.
37+
*
38+
* @return array<string, mixed>
39+
*/
40+
public function definition(): array
41+
{
42+
/**
43+
* In order to utilize other services, the following substitutes can be used:
44+
* - new ImageFaker(new LoremFlickr());
45+
* - new ImageFaker(new PlaceDog());
46+
* - new ImageFaker(new FakePeople());
47+
*/
48+
$imageFaker = new ImageFaker(new Picsum());
49+
50+
return [
51+
'title' => $this->faker->sentence(),
52+
'content' => $this->faker->paragraph(),
53+
'attachments' => $imageFaker->image( storage_path("app/attachments/") )
54+
];
55+
}
56+
```
57+
58+
## Contributing
59+
Don't hesitate to send a PR if you're looking for a service that's not available in this package. 😁
60+
61+
## License
62+
The MIT License (MIT). Please see [License File](LICENSE) for more information.

src/Base.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Alirezasedghi\LaravelImageFaker;
4+
5+
class Base
6+
{
7+
/**
8+
* Service base url
9+
*/
10+
public string $base_url = '';
11+
12+
/**
13+
* Service files default format
14+
*/
15+
public string $default_format = 'jpg';
16+
17+
/**
18+
* Default user agent for curl
19+
*/
20+
public string $user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0';
21+
22+
/**
23+
* SSL verification of service
24+
*/
25+
public bool $SSL_verify_peer = true;
26+
27+
/**
28+
* Generate the URL that will return a random image
29+
*
30+
* @param int $width
31+
* @param int $height
32+
* @param bool $grayscale
33+
* @param bool $blur
34+
* @return string
35+
*/
36+
public function imageUrl(int $width = 640, int $height = 480, bool $grayscale = false, bool|int $blur = false): string
37+
{
38+
if ( empty($this->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+
}

src/Facades/ImageFaker.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Alirezasedghi\LaravelImageFaker\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
8+
class ImageFaker extends Facade
9+
{
10+
protected static function getFacadeAccessor() {
11+
return 'ImageFaker';
12+
}
13+
}

src/ImageFaker.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Alirezasedghi\LaravelImageFaker;
4+
5+
use Illuminate\Support\Str;
6+
7+
class ImageFaker
8+
{
9+
use ImageFakerTrait;
10+
11+
/**
12+
* Service
13+
*
14+
* @var Base
15+
*/
16+
protected Base $service;
17+
18+
/**
19+
* Construction
20+
*
21+
* @param Base $service
22+
*/
23+
public function __construct(Base $service)
24+
{
25+
$this->service = $service;
26+
}
27+
}

src/ImageFakerServiceProvider.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Alirezasedghi\LaravelImageFaker;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class ImageFakerServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register any application services.
11+
*/
12+
public function register()
13+
{
14+
$this->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+
}

src/ImageFakerTrait.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Alirezasedghi\LaravelImageFaker;
4+
5+
trait ImageFakerTrait
6+
{
7+
/**
8+
* Generate the URL that will return a random image
9+
*
10+
* @param int $width
11+
* @param int $height
12+
* @param bool $grayscale
13+
* @param bool $blur
14+
* @return string
15+
*/
16+
public function imageUrl(int $width = 640, int $height = 480, bool $grayscale = false, bool|int $blur = false): string
17+
{
18+
return $this->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+
}

src/Services/FakePeople.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Alirezasedghi\LaravelImageFaker\Services;
4+
5+
use Alirezasedghi\LaravelImageFaker\Base;
6+
7+
class FakePeople extends Base
8+
{
9+
/**
10+
* Number of 994 static images of fake people from the BoredHumans.com collection
11+
*
12+
* Unfortunately, custom sizing options for images are not currently available. All images are set at a fixed size of 512x512 pixels.
13+
*/
14+
15+
/**
16+
* Service base url
17+
*/
18+
public string $base_url = 'https://boredhumans.com/faces2';
19+
20+
/**
21+
* SSL verification of service
22+
*/
23+
public bool $SSL_verify_peer = false;
24+
25+
/**
26+
* Make url to get images
27+
*
28+
* @param int $width
29+
* @param int $height
30+
* @param array $imageOptions
31+
* @return string
32+
*/
33+
protected function makeUrl(int $width = 640, int $height = 480, array $imageOptions = []) {
34+
return sprintf(
35+
'%s/%s.jpg',
36+
$this->base_url,
37+
mt_rand(1, 994)
38+
);
39+
}
40+
}

0 commit comments

Comments
 (0)