Skip to content

Commit

Permalink
Composer support
Browse files Browse the repository at this point in the history
  • Loading branch information
grubersjoe committed Jun 14, 2018
1 parent a91cf1d commit ee8abda
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 26 deletions.
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ BingPhoto is a simple PHP class to fetch Bing's image of the day with meta data.

It is also possible to cache the images locally, which can be useful in combination with a periodic cronjob. See the `cacheDir` parameter for this (optional) feature. Disclaimer: this might be a copyright issue.

## Installation

Use [Composer](https://getcomposer.org/) to install this package:

```sh
composer require grubersjoe/bing-daily-photo
```

## Basic usage

```php
$bing = new BingPhoto();
$bing = new grubersjoe\BingPhoto();
$image = $bing->getImage();

// Example result ($image)
Expand Down Expand Up @@ -41,9 +49,9 @@ Breaking change: the parameter `resolution` was renamed to `quality`. See also t

```php
// Fetches two images of the day starting yesterday from Bing
$bing = new BingPhoto([
$bing = new grubersjoe\BingPhoto([
'n' => 2,
'date' => BingPhoto::YESTERDAY
'date' => grubersjoe\BingPhoto::YESTERDAY
]);

foreach ($bing->getImages() as $image) {
Expand All @@ -53,21 +61,21 @@ foreach ($bing->getImages() as $image) {

```php
// Fetches the current image of the day in low resolution from the French Bing portal
$bing = new BingPhoto([
$bing = new grubersjoe\BingPhoto([
'locale' => 'fr-FR',
'quality' => BingPhoto::QUALITY_LOW,
'quality' => grubersjoe\BingPhoto::QUALITY_LOW,
]);

printf('<img src="%s">', $bing->getImage()['url']);
```

```php
// Fetches three images of the day in high quality from the German Bing portal, starting yesterday
$bing = new BingPhoto([
$bing = new grubersjoe\BingPhoto([
'n' => 3,
'date' => BingPhoto::YESTERDAY,
'date' => grubersjoe\BingPhoto::YESTERDAY,
'locale' => 'de-DE',
'quality' => BingPhoto::QUALITY_HIGH,
'quality' => grubersjoe\BingPhoto::QUALITY_HIGH,
]);

foreach ($bing->getImages() as $image) {
Expand All @@ -78,9 +86,9 @@ foreach ($bing->getImages() as $image) {
```php
// Using the local cache
// (remember to create the directory first!)
$bing = new BingPhoto([
$bing = new grubersjoe\BingPhoto([
'cacheDir' => '/tmp/bing-photo',
'n' => 5,
'quality' => BingPhoto::QUALITY_LOW,
'quality' => grubersjoe\BingPhoto::QUALITY_LOW,
]);
```
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
{
"name": "grubersjoe/bing-daily-photo",
"description": "A class to fetch Bing's image of the day with meta data",
"keywords": ["bing", "daily", "photo"],
"license": "MIT",
"authors": [
{
"name": "Jonathan Gruber",
"homepage": "https://grubersjoe.de"
}
],
"autoload": {
"psr-4": {
"grubersjoe\\": "src/"
}
},
"require": {
"php": "^5.6.0 || ^7.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
}
Expand Down
18 changes: 10 additions & 8 deletions BingPhoto.php → src/BingPhoto.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace grubersjoe;

/**
* A simple class which fetches Bing's image of the day with meta data
*/
Expand Down Expand Up @@ -32,7 +34,7 @@ class BingPhoto
* $args['locale'] string Localization string (en-US, de-DE, ...)
* $args['n'] int Number of images / days
* $args['quality'] string Resolution of images(s)
* @throws Exception
* @throws \Exception
*/
public function __construct(array $args = [])
{
Expand All @@ -45,7 +47,7 @@ public function __construct(array $args = [])
if (file_exists($cacheDir)) {
$this->cacheImages();
} else {
throw new Exception(sprintf('Given cache directory %s does not exist', $cacheDir));
throw new \Exception(sprintf('Given cache directory %s does not exist', $cacheDir));
}
}
}
Expand Down Expand Up @@ -100,7 +102,7 @@ private function setArgs(array $args)
$defaultArgs = [
'cacheDir' => false,
'date' => self::TODAY,
'locale' => str_replace('_', '-', Locale::getDefault()),
'locale' => str_replace('_', '-', \Locale::getDefault()),
'n' => 1,
'quality' => self::QUALITY_HIGH,
];
Expand All @@ -126,7 +128,7 @@ private function sanitizeArgs(array $args)

/**
* Fetches the image meta data from Bing (JSON)
* @throws Exception
* @throws \Exception
*/
private function fetchImagesMetadata()
{
Expand All @@ -140,7 +142,7 @@ private function fetchImagesMetadata()
$this->setAbsoluteUrl();
$this->setQuality();
} else {
throw new Exception('Unable to retrieve JSON data: ' . $error);
throw new \Exception('Unable to retrieve JSON data: ' . $error);
}
}

Expand All @@ -153,14 +155,14 @@ private function cacheImages()
$fetchList = [];

// Build a list of to be cached dates
$baseDate = (new DateTime())->modify(sprintf('-%d day', $this->args['date'] - 1));
$baseDate = (new \DateTime())->modify(sprintf('-%d day', $this->args['date'] - 1));
for ($i = 0; $i < $this->args['n']; $i++) {
$date = $baseDate->modify('-1 day')->format('Ymd');
$fetchList[$date] = true;
}

// Check current cache
$dirIterator = new DirectoryIterator($this->args['cacheDir']);
$dirIterator = new \DirectoryIterator($this->args['cacheDir']);
foreach ($dirIterator as $image) {
if ($image->isFile() && $image->getExtension() === 'jpg') {
$imageShouldBeCached = in_array($image->getBasename('.jpg'), array_keys($fetchList));
Expand Down Expand Up @@ -198,7 +200,7 @@ private function fetchImageFiles(array $fetchList)
}
}
}
} catch (Exception $e) {
} catch (\Exception $e) {
error_log($e->getMessage());
exit($e->getMessage());
}
Expand Down
19 changes: 11 additions & 8 deletions BingPhotoTest.php → src/BingPhotoTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace grubersjoe;

use PHPUnit\Framework\TestCase;

require('BingPhoto.php');
Expand All @@ -10,7 +12,7 @@ class BingPhotoTest extends TestCase
* @dataProvider invalidArgumentProvider
* @param $expected
* @param $args
* @throws Exception
* @throws \Exception
*/
public function testArgsValidation($expected, $args = [])
{
Expand All @@ -25,12 +27,12 @@ public function testArgsValidation($expected, $args = [])
/**
* @dataProvider countArgsProvider
* @param $args
* @throws Exception
* @throws \Exception
*/
public function testCount($args = [])
{
$bingPhoto = new BingPhoto($args);
$count = min($args['n'] ?? 1, BingPhoto::LIMIT_N);
$count = min(isset($args['n']) ? $args['n'] : 1, BingPhoto::LIMIT_N);
$this->assertCount($count, $bingPhoto->getImages());
}

Expand All @@ -40,21 +42,22 @@ public function testCount($args = [])
/**
* @dataProvider qualityArgsProvider
* @param $args
* @throws Exception
* @throws \Exception
*/
public function testQuality($args = [])
{
$bingPhoto = new BingPhoto($args);
foreach ($bingPhoto->getImages() as $image) {
list($width, $height) = getimagesize($image['url']);
$this->assertEquals($width . 'x' . $height, $args['quality'] ?? BingPhoto::QUALITY_HIGH);
$quality = isset($args['quality']) ? $args['quality'] : BingPhoto::QUALITY_HIGH;
$this->assertEquals($width . 'x' . $height, $quality);
}
}

/**
* @dataProvider cacheArgsProvider
* @param $args
* @throws Exception
* @throws \Exception
*/
public function testCache($args = [])
{
Expand Down Expand Up @@ -97,12 +100,12 @@ public function testCache($args = [])
/**
* @dataProvider invalidCacheArgsProvider
* @param $args
* @throws Exception
* @throws \Exception
*/
public function testInvalidCache($args = [])
{
if (!empty($args['cacheDir']) && !file_exists($args['cacheDir'])) {
$this->expectException(Exception::class);
$this->expectException(\Exception::class);
}

$bingPhoto = new BingPhoto($args);
Expand Down

0 comments on commit ee8abda

Please sign in to comment.