Skip to content

Commit

Permalink
added getImageSizes() method
Browse files Browse the repository at this point in the history
  • Loading branch information
abordage committed Jun 6, 2022
1 parent 5023049 commit 833559d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ as a string instead of saving it:
$imageBlob = OpenGraphImages::make($text)->get();
```

If after generation you need to get sizes of the image, you can get it as follows:

```php
$openGraphImage = OpenGraphImages::make($text, 'twitter');
$openGraphImage->save($path);
$imageSizes = $openGraphImage->getImageSizes();
// return [
// 'width' => 1200,
// 'height' => 600
// ];
```

## Usage with `spatie/laravel-medialibrary`
[spatie/laravel-medialibrary](https://github.com/spatie/laravel-medialibrary) is a great package for associate all sorts of files with Eloquent models. If you are using this package (or similar), all you need to do is to add new collections to the model and attach images using media library.

Expand Down Expand Up @@ -156,9 +168,10 @@ $page->title = 'Your awesome title';
$page->save();

// generate image and attach to model
$imageBlob = OpenGraphImages::make($page->title)->get();
$page->addMediaFromString($imageBlob)
$image = OpenGraphImages::make($page->title);
$page->addMediaFromString($image->get())
->usingFileName(\Str::slug($page->title) . '.png')
->withCustomProperties($image->getImageSizes())
->toMediaCollection('opengraph');
```

Expand All @@ -170,11 +183,11 @@ $page->title = 'Your awesome title';
$page->save();

$presets = ['opengraph', 'twitter', 'vk'];
foreach ($presets as $preset){
// generate image and attach to model
$imageBlob = OpenGraphImages::make($page->title, $preset)->get();
$page->addMediaFromString($imageBlob)
foreach ($presets as $preset) {
$image = OpenGraphImages::make($page->title, $preset);
$page->addMediaFromString($image->get())
->usingFileName(\Str::slug($page->title) . '-' . $preset . '.png')
->withCustomProperties($image->getImageSizes())
->toMediaCollection($preset);
}
```
Expand Down Expand Up @@ -332,12 +345,13 @@ $config = [

## API Reference

| Method | Returns | Added in | Changed in |
|-------------------------------------------------------|:-------:|:--------:|:----------:|
| `make(string $text, string $preset = 'opengraph')` | self | 0.1.0 | 0.2.0 |
| `makeCustom(string $text, int $width, int $height)` | self | 0.2.0 | - |
| `get()` | string | 0.1.0 | - |
| `save(string $path)` | boolean | 0.1.0 | - |
| Method | Returns | Added in | Changed in |
|-----------------------------------------------------|:-------:|:--------:|:----------:|
| `make(string $text, string $preset = 'opengraph')` | self | 0.1.0 | 0.2.0 |
| `makeCustom(string $text, int $width, int $height)` | self | 0.2.0 | - |
| `get()` | string | 0.1.0 | - |
| `save(string $path)` | boolean | 0.1.0 | - |
| `getImageSizes()` | array | 0.3.0 | - |

### Images aspect ratios

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"require": {
"php": ">=7.4",
"ext-imagick": "*",
"abordage/og-images": "^0.2.1",
"abordage/og-images": "^0.3.0",
"illuminate/support": "^8.0 || ^9.0"
},
"require-dev": {
Expand Down
1 change: 1 addition & 0 deletions src/Facades/OpenGraphImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @method static \Abordage\LaravelOpenGraphImages\OpenGraphImages makeCustom(string $text, int $width, int $height)
* @method string|null get()
* @method bool save(string $path)
* @method array getImageSizes()
*
* @see \Abordage\LaravelOpenGraphImages\OpenGraphImages
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/OpenGraphImagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ public function testMake(string $text): void
}
}

/**
* @dataProvider textProvider
*/
public function testGetImageSizes(string $text): void
{
$openGraphImages = $this->openGraphImages;
$result = $openGraphImages->getImageSizes();
$this->assertEquals([], $result);

$openGraphImages = $this->openGraphImages->make($text);
$result = $openGraphImages->getImageSizes();

foreach ($result as $key => $value) {
$this->assertIsInt($value);
$this->assertGreaterThan(0, $value);
}
}

/**
* @dataProvider textProvider
*/
Expand Down

0 comments on commit 833559d

Please sign in to comment.