Skip to content

Commit

Permalink
fix: fix imageUrl in Image module to generate images from picsum photos
Browse files Browse the repository at this point in the history
#990 (#998)

* create method urlPicsumPhotos for generate random image from https://picsum.photos and changed the name of urlImage in urlLoremFlickr

* created imageUrl: Generates a random image url with `https://loremflickr.com/` or "https://picsum.photos"

* created imageUrl: Generates a random image url with `https://loremflickr.com/` or "https://picsum.photos"
  • Loading branch information
bitalec authored Nov 25, 2024
1 parent cf1c0bb commit 9b7d709
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 10 deletions.
46 changes: 41 additions & 5 deletions include/faker-cxx/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ enum class ImageCategory
Transport
};

/**
* @brief Generates a random image url with `https://loremflickr.com/` or "https://picsum.photos".
*
* @param width The width of the image. Defaults to `640`.
* @param height The height of the image. Defaults to `480`.
*
* @returns Random real image url from external service.
*
* @code
* faker::image::imageUrl() // "https://loremflickr.com/640/480" or "https://picsum.photos/640/480"
* @endcode
*/
FAKER_CXX_EXPORT std::string imageUrl(unsigned width = 640, unsigned height = 480);

/**
* @brief Generates a real image url with `https://loremflickr.com/`.
*
Expand All @@ -34,13 +48,34 @@ enum class ImageCategory
* @returns Random real image url from external service.
*
* @code
* faker::image::imageUrl() // "https://loremflickr.com/640/480"
* faker::image::imageUrl(800, 600) // "https://loremflickr.com/800/600"
* faker::image::imageUrl(800, 600, ImageCategory::Animals) // "https://loremflickr.com/800/600/animals"
* faker::image::urlLoremFlickr() // "https://loremflickr.com/640/480"
* faker::image::urlLoremFlickr(800, 600) // "https://loremflickr.com/800/600"
* faker::image::urlLoremFlickr(800, 600, ImageCategory::Animals) // "https://loremflickr.com/800/600/animals"
* @endcode
*/
FAKER_CXX_EXPORT std::string imageUrl(unsigned width = 640, unsigned height = 480,
std::optional<ImageCategory> category = std::nullopt);
FAKER_CXX_EXPORT std::string urlLoremFlickr(unsigned width = 640, unsigned height = 480,
std::optional<ImageCategory> category = std::nullopt);

/**
* @brief Generates a real image url with "https://picsum.photos" .
*
* @param width The width of the image. Defaults to `640`.
* @param height The height of the image. Defaults to `480`.
* @param greyscale The optional greyscale filter. True for apply or false for disabled
* @param blur The optional blur effect between 1 and 10
*
* @returns Random real image url from external service
*
* @code
* faker::image::urlPicsPhotos() // "https://picsum.photos/640/480"
* faker::image::urlPicsPhotos(800,600,true) // "https://picsum.photos/800/600?grayscale
* faker::image::urlPicsPhotos(800,600,false,5) // "https://picsum.photos/800/600?blur=5
* faker::image::urlPicsPhotos(800,600,true,1) // "https://picsum.photos/800/600?grayscale&blur=1
*@endcode
*/
FAKER_CXX_EXPORT std::string urlPicsumPhotos(unsigned width = 640, unsigned height = 480,
std::optional<bool> greyscale = std::nullopt,
std::optional<int> blur = std::nullopt);

/**
* @brief Generates a random avatar from GitHub.
Expand Down Expand Up @@ -71,6 +106,7 @@ FAKER_CXX_EXPORT std::string dimensions();
*
* @code
* faker::image::type() // "png"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view type();
}
36 changes: 35 additions & 1 deletion src/modules/image.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "faker-cxx/image.h"

#include <array>
#include <faker-cxx/datatype.h>
#include <optional>
#include <string>
#include <string_view>
Expand All @@ -25,14 +26,47 @@ std::unordered_map<ImageCategory, std::string> imageCategoryToLoremFlickrStringM
};
}

std::string imageUrl(unsigned int width, unsigned int height, std::optional<ImageCategory> category)
std::string imageUrl(unsigned width, unsigned height)
{
if (faker::datatype::boolean(0.5))
{
return urlLoremFlickr();
}
else
return urlPicsumPhotos();
}

std::string urlLoremFlickr(unsigned int width, unsigned int height, std::optional<ImageCategory> category)
{
const std::string image_category =
category.has_value() ? common::format("/{}", imageCategoryToLoremFlickrStringMapping.at(category.value())) : "";

return common::format("https://loremflickr.com/{}/{}{}", width, height, image_category);
}

std::string urlPicsumPhotos(unsigned width, unsigned height, const std::optional<bool> greyscale,
const std::optional<int> blur)
{
std::string url = common::format("https://picsum.photos/{}/{}", width, height);
std::string params;

params += (greyscale.has_value() && greyscale.value()) ? "greyscale" : "";

if (blur.has_value())
{
if (blur.value() < 1 || blur.value() > 10)
return common::format("{}", "Error: blur must be between 1 and 10.");
if (!params.empty())
params += "&";
params += common::format("blur={}", blur.value());
}
if (!params.empty())
{
url = common::format("{}?{}", url, params);
}
return common::format("{}", url);
}

std::string githubAvatarUrl()
{
return common::format("https://avatars.githubusercontent.com/u/{}", number::integer<int>(100000000));
Expand Down
67 changes: 63 additions & 4 deletions tests/modules/image_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ class ImageTest : public Test
public:
};

TEST_F(ImageTest, shouldGenerateImageUrlDefault)
TEST_F(ImageTest, shouldGenerateRandomImageUrl)
{
const auto generatedImageUrl = imageUrl();

ASSERT_TRUE(generatedImageUrl == urlLoremFlickr() || generatedImageUrl == urlPicsumPhotos());
}

TEST_F(ImageTest, shouldGenerateurlLoremFlickrDefault)
{
const auto generatedImageUrl = urlLoremFlickr();

ASSERT_EQ(generatedImageUrl, "https://loremflickr.com/640/480");
}

Expand All @@ -29,22 +36,74 @@ TEST_F(ImageTest, shouldGenerateImageUrl)
const auto width = 800;
const auto height = 600;

const auto generatedImageUrl = imageUrl(width, height);
const auto generatedImageUrl = urlLoremFlickr(width, height);

ASSERT_EQ(generatedImageUrl, "https://loremflickr.com/800/600");
}

TEST_F(ImageTest, shouldGenerateImageUrlCategory)
TEST_F(ImageTest, shouldGenerateurlLoremFlickrCategory)
{
const auto width = 800;
const auto height = 600;
const auto category = ImageCategory::Fashion;

const auto generatedImageUrl = imageUrl(width, height, category);
const auto generatedImageUrl = urlLoremFlickr(width, height, category);

ASSERT_EQ(generatedImageUrl, "https://loremflickr.com/800/600/fashion");
}

TEST_F(ImageTest, shouldGenerateImageUrlPicsumDefault)
{
const auto generatedImageUrl = urlPicsumPhotos();

ASSERT_EQ(generatedImageUrl, "https://picsum.photos/640/480");
}

TEST_F(ImageTest, shouldGenerateImageUrlPicsumPhotos)
{
const auto width = 800;
const auto height = 600;

const auto generatedImageUrl = urlPicsumPhotos(width, height);

ASSERT_EQ(generatedImageUrl, "https://picsum.photos/800/600");
}

TEST_F(ImageTest, shouldGenerateImageUrlPicsumPhotosGreyscale)
{
const auto width = 800;
const auto height = 600;
const auto greyscale = true;

const auto generatedImageUrl = urlPicsumPhotos(width, height, greyscale);

ASSERT_EQ(generatedImageUrl, "https://picsum.photos/800/600?greyscale");
}

TEST_F(ImageTest, shouldGenerateImageUrlPicsumPhotosBlur)
{
const auto width = 800;
const auto height = 600;
const auto greyscale = false;
const auto blur = 5;

const auto generatedImageUrl = urlPicsumPhotos(width, height, greyscale, blur);

ASSERT_EQ(generatedImageUrl, "https://picsum.photos/800/600?blur=5");
}

TEST_F(ImageTest, shouldGenerateImageUrlPicsumPhotosBlurandGreyscale)
{
const auto width = 800;
const auto height = 600;
const auto greyscale = true;
const auto blur = 6;

const auto generatedImageUrl = urlPicsumPhotos(width, height, greyscale, blur);

ASSERT_EQ(generatedImageUrl, "https://picsum.photos/800/600?greyscale&blur=6");
}

TEST_F(ImageTest, shouldGenerateGithubAvatarUrl)
{
const auto generatedGithubAvatarUrl = githubAvatarUrl();
Expand Down

0 comments on commit 9b7d709

Please sign in to comment.