Skip to content

Commit

Permalink
Fixes #186 - Adds f.Image.PicsumUrl https://picsum.photos service.
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Nov 3, 2018
1 parent 3aeb013 commit 21f6f6b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## v24.3.1
* `tr` - Turkish locale first/last names updated. Lorem data set updated. Thanks ahmetcanaydemir!
* `tr` - Turkish locale first/last names updated. Lorem data set updated. Thanks ahmetcanaydemir!
* Added `f.Image.PicsumUrl` (https://picsum.photos) service as faster alternative to Lorem Pixel.

## v24.3.0
* Data / feature parity with faker.js @ 9dd5a52
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ public void Using_FakerT_Inheritance()
* `Technics` - Get a technology related image.
* `Transport` - Get a transportation related image.
* `DataUri` - Get a SVG data URI image with a specific width and height.
* `PicsumUrl` - Get an image from the https://picsum.photos service.
* **`Internet`**
* `Avatar` - Generates a legit Internet URL avatar from twitter accounts.
* `Email` - Generates an email address.
Expand Down
10 changes: 10 additions & 0 deletions Source/Bogus.Tests/DataSetTests/ImageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,15 @@ public void url_generated_should_have_https()
{
image.Sports(https: true).Should().StartWith("https://");
}

[Fact]
public void can_use_picsum_Url()
{
var url = image.PicsumUrl(200, 300);
url.Should().Be("https://picsum.photos/200/300/?image=654");

url = image.PicsumUrl(300, 200, true, true);
url.Should().Be("https://picsum.photos/g/300/200/?image=119&blur");
}
}
}
34 changes: 34 additions & 0 deletions Source/Bogus/DataSets/Images.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;

namespace Bogus.DataSets
{
Expand Down Expand Up @@ -222,5 +223,38 @@ public string DataUri(int width, int height, string htmlColor = "grey")

return rawPrefix + Uri.EscapeDataString(svgString);
}

/// <summary>
/// Get an image from the https://picsum.photos service.
/// </summary>
/// <param name="width">Width of the image.</param>
/// <param name="height">Height of the image.</param>
/// <param name="grayscale">Grayscale (no color) image.</param>
/// <param name="blur">Blurry image.</param>
/// <param name="imageId">Optional Image ID found here https://picsum.photos/images</param>
public string PicsumUrl(int width = 640, int height = 480, bool grayscale = false, bool blur = false, int? imageId = null )
{
const string Url = "https://picsum.photos";

var sb = new StringBuilder();

if (grayscale)
{
sb.Append("/g");

}

sb.Append($"/{width}/{height}");

var n = imageId ?? this.Random.Number(0, 1084);
sb.Append($"/?image={n}");

if (blur)
{
sb.Append("&blur");
}

return Url + sb;
}
}
}

0 comments on commit 21f6f6b

Please sign in to comment.