Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
virtruvio committed May 16, 2018
2 parents 2c8cd5c + 6dfda10 commit 327471d
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 10 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

All notable changes to `laravel-medialibrary` will be documented in this file

## 7.3.8 - 2018-05-15

- fix `ids` option of `RegenerateCommand`

## 7.3.7 - 2018-05-15

- bugfix around responsive images

## 7.3.6 - 2018-05-15

- add support from `webp`

## 7.3.5 - 2018-05-08

- fix bug where `addMediaFromUrl` would not work if the file contained a space
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![StyleCI](https://styleci.io/repos/33916850/shield)](https://styleci.io/repos/33916850)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-medialibrary.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-medialibrary)

This Laravel >=5.5 package can associate all sorts of files with Eloquent models. It provides a
This package can associate all sorts of files with Eloquent models. It provides a
simple API to work with. To learn all about it, head over to [the extensive documentation](https://docs.spatie.be/laravel-medialibrary/).

Here are a few short examples of what you can do:
Expand Down Expand Up @@ -158,6 +158,7 @@ return [
*/
'image_generators' => [
Spatie\MediaLibrary\ImageGenerators\FileTypes\Image::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Webp::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Pdf::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Svg::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Video::class,
Expand Down
1 change: 1 addition & 0 deletions config/medialibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
*/
'image_generators' => [
Spatie\MediaLibrary\ImageGenerators\FileTypes\Image::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Webp::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Pdf::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Svg::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Video::class,
Expand Down
27 changes: 19 additions & 8 deletions src/Commands/RegenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,31 @@ public function handle()
public function getMediaToBeRegenerated(): Collection
{
$modelType = $this->argument('modelType') ?? '';
$mediaIds = $this->option('ids');
$mediaIds = $this->getMediaIds();

if ($modelType === '' && ! $mediaIds) {
if ($modelType === '' && count($mediaIds) === 0) {
return $this->mediaRepository->all();
}

if ($mediaIds) {
if (! is_array($mediaIds)) {
$mediaIds = explode(',', $mediaIds);
}
if (! count($mediaIds)) {
return $this->mediaRepository->getByModelType($modelType);
}

return $this->mediaRepository->getByIds($mediaIds);
}

protected function getMediaIds(): array
{
$mediaIds = $this->option('ids');

if (! is_array($mediaIds)) {
$mediaIds = explode(',', $mediaIds);
}

return $this->mediaRepository->getByIds($mediaIds);
if (count($mediaIds) === 1 && str_contains($mediaIds[0], ',')) {
$mediaIds = explode(',', $mediaIds[0]);
}

return $this->mediaRepository->getByModelType($modelType);
return $mediaIds;
}
}
50 changes: 50 additions & 0 deletions src/ImageGenerators/FileTypes/Webp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Spatie\MediaLibrary\ImageGenerators\FileTypes;

use Illuminate\Support\Collection;
use Spatie\MediaLibrary\Conversion\Conversion;
use Spatie\MediaLibrary\ImageGenerators\BaseGenerator;

class Webp extends BaseGenerator
{
public function convert(string $file, Conversion $conversion = null) : string
{
$pathToImageFile = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).'.png';

$image = imagecreatefromwebp($file);

imagepng($image, $pathToImageFile, 9);

imagedestroy($image);

return $pathToImageFile;
}

public function requirementsAreInstalled() : bool
{
if (! function_exists('imagecreatefromwebp')) {
return false;
}

if (! function_exists('imagepng')) {
return false;
}

if (! function_exists('imagedestroy')) {
return false;
}

return true;
}

public function supportedExtensions() : Collection
{
return collect(['webp']);
}

public function supportedMimeTypes() : Collection
{
return collect(['image/webp']);
}
}
2 changes: 1 addition & 1 deletion src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function getResponsiveImageUrls(string $conversionName = ''): array

public function hasResponsiveImages(string $conversionName = ''): bool
{
return count($this->getResponsiveImageUrls($conversionName));
return count($this->getResponsiveImageUrls($conversionName)) > 0;
}

public function getSrcset(string $conversionName = ''): string
Expand Down
27 changes: 27 additions & 0 deletions tests/Feature/Commands/RegenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,33 @@ public function it_can_regenerate_files_by_media_ids()
$this->assertFileExists($derivedImage2);
}

/** @test */
public function it_can_regenerate_files_by_comma_separated_media_ids()
{
$media = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->preservingOriginal()
->toMediaCollection('images');

$media2 = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->toMediaCollection('images');

$derivedImage = $this->getMediaDirectory("{$media->id}/conversions/test-thumb.jpg");
$derivedImage2 = $this->getMediaDirectory("{$media2->id}/conversions/test-thumb.jpg");

unlink($derivedImage);
unlink($derivedImage2);

$this->assertFileNotExists($derivedImage);
$this->assertFileNotExists($derivedImage2);

Artisan::call('medialibrary:regenerate', ['--ids' => ['1,2']]);

$this->assertFileExists($derivedImage);
$this->assertFileExists($derivedImage2);
}

/** @test */
public function it_can_regenerate_files_even_if_there_are_files_missing()
{
Expand Down
Binary file added tests/Support/testfiles/test.webp
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ public function getTestSvg()
return $this->getTestFilesDirectory('test.svg');
}

public function getTestWebp()
{
return $this->getTestFilesDirectory('test.webp');
}

private function setUpMorphMap()
{
Relation::morphMap([
Expand Down
27 changes: 27 additions & 0 deletions tests/Unit/ImageGenerators/WebpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Spatie\MediaLibrary\Tests\Unit\ImageGenerators;

use Spatie\MediaLibrary\Tests\TestCase;
use Spatie\MediaLibrary\ImageGenerators\FileTypes\Webp;

class WebpTest extends TestCase
{
/** @test */
public function it_can_convert_a_webp()
{
$imageGenerator = new Webp();

if (! $imageGenerator->requirementsAreInstalled()) {
$this->markTestSkipped('Skipping pdf test because requirements to run it are not met');
}

$media = $this->testModelWithoutMediaConversions->addMedia($this->getTestWebp())->toMediaCollection();

$this->assertTrue($imageGenerator->canConvert($media));

$imageFile = $imageGenerator->convert($media->getPath());

$this->assertEquals('image/png', mime_content_type($imageFile));
}
}

0 comments on commit 327471d

Please sign in to comment.