Skip to content

Commit

Permalink
Static Random Image Support (#1)
Browse files Browse the repository at this point in the history
* picsumStaticRandomUrl method added
  • Loading branch information
h3xexe committed Jul 5, 2020
1 parent 3b8e8fb commit d6e573f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ $faker->addProvider(new \Mmo\Faker\PicsumProvider($faker));
$url = $faker->picsumUrl(); // https://picsum.photos/640/480?random=24398
$url = $faker->picsumUrl(400, 400); // https://picsum.photos/400/400?random=23446

// static random
$url = $faker->picsumStaticRandomUrl(400, 400); // https://picsum.photos/seed/5efe7fec1bd11/400/400

// download image to tmp dir
$path = $faker->picsum(null, 400, 400, true); // /tmp/72c04225dd87efc261d29d3a050aa9b6.jpg

// Signature
// picsumUrl($width = 640, $height = 480, $id = null, $randomize = true, $gray = false, $blur = null)
// picsumUrl($width = 640, $height = 480, $id = null, $randomize = true, $gray = false, $blur = null, $static = false)
// picsumStaticRandomUrl($width = 640, $height = 480, $gray = false, $blur = null)
// picsum($dir = null, $width = 640, $height = 480, $fullPath = true, $id = null, $randomize = true, $gray = false, $blur = null)
```
23 changes: 23 additions & 0 deletions src/PicsumProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ public static function picsumUrl($width = 640, $height = 480, $id = null, $rando
return $baseUrl . $url . $queryString;
}

public static function picsumStaticRandomUrl($width = 640, $height = 480, $gray = false, $blur = null)
{
$baseUrl = 'https://picsum.photos/';

$url = 'seed/' . uniqid() . '/' . "{$width}/{$height}";

$queryParams = array();
if ($gray) {
$queryParams['grayscale'] = '';
}

if ($blur) {
$queryParams['blur'] = '';
}

$queryString = '';
if (!empty($queryParams)) {
$queryString = '?' . http_build_query($queryParams);
}

return $baseUrl . $url . $queryString;
}

/**
* Download a remote random image to disk and return its location
*
Expand Down
22 changes: 22 additions & 0 deletions test/Faker/PicsumProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ public function testPicsumUrlWithGrayAndBlur()
$this->assertSame('https://picsum.photos/800/400?grayscale=&blur=', $imageUrl);
}

public function testpicsumStaticRandomUrl()
{
$imageUrl = PicsumProvider::picsumStaticRandomUrl(
800,
400
);

$this->assertRegExp('#^https:\/\/picsum\.photos\/seed\/[A-Za-z0-9]+\/800\/400#', $imageUrl);
}

public function testpicsumStaticRandomUrlWithGrayAndBlur()
{
$imageUrl = PicsumProvider::picsumStaticRandomUrl(
800,
400,
true,
true
);

$this->assertRegExp('#^https:\/\/picsum\.photos\/seed\/[A-Za-z0-9]+\/800\/400+\?grayscale=&blur=#', $imageUrl);
}

public function testPicsumUrlAddsARandomGetParameterByDefault()
{
$url = PicsumProvider::picsumUrl(800, 400);
Expand Down

0 comments on commit d6e573f

Please sign in to comment.