Skip to content

Commit

Permalink
Add webPInformation model
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisdeBest committed Jun 2, 2020
1 parent cc7a234 commit 49edf01
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^7.4",
"codebuds/webp-converter": "^0.2.2",
"codebuds/webp-converter": "^0.2.5",
"symfony/console": "^5.0",
"symfony/stopwatch": "^5.0",
"symfony/finder": "^5.0",
Expand Down
85 changes: 85 additions & 0 deletions src/Model/WebPInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace CodeBuds\WebPConversionBundle\Model;


use CodeBuds\WebPConversionBundle\Traits\ConvertibleImageTrait;
use Exception;
use Symfony\Component\HttpFoundation\File\File;

class WebPInformation
{
private int $quality;

private string $filename;

private string $path;

private string $filenameSuffix;

private bool $saved = false;

/**
* @param array $information
* @return $this
*/
public function __construct(array $information)
{
$this->path = $information["options"]["savePath"];
$this->quality = $information["options"]["quality"];
$this->filename = $information["options"]["filename"];
$this->filenameSuffix = $information["options"]["filenameSuffix"];
$this->saved = $information["options"]["saveFile"];

return $this;
}

/**
* @return int
*/
public function getQuality(): int
{
return $this->quality;
}

/**
* @return string
*/
public function getFilename(): string
{
return $this->filename;
}

/**
* @return string
*/
public function getPath(): string
{
return $this->path;
}

/**
* @return string
*/
public function getFilenameSuffix(): string
{
return $this->filenameSuffix;
}

/**
* @return bool
*/
public function isSaved(): bool
{
return $this->saved;
}

/**
* @return string
*/
public function getConvertedFullPath(): string
{
return "{$this->path}/{$this->filename}{$this->filenameSuffix}.webp";
}

}
22 changes: 11 additions & 11 deletions src/Service/ImageConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,27 @@
namespace CodeBuds\WebPConversionBundle\Service;

use CodeBuds\WebPConversionBundle\Model\Image;
use CodeBuds\WebPConversionBundle\Model\WebPInformation;
use CodeBuds\WebPConverter\WebPConverter;
use Exception;
use Symfony\Component\HttpFoundation\File\File;

class ImageConverter
{
/**
* @param Image $image
* @param $image
* @param bool $saveFile
* @param bool $force
* @return array|mixed
* @throws Exception
*/
public function convert(Image $image, bool $force = false, bool $saveFile = true)
public function convert($image, bool $force = false, bool $saveFile = true)
{
$options = $this->createOptionsArray($image);
$options['saveFile'] = $saveFile;
$options['force'] = $force;

$webPInfo = WebPConverter::createWebPImage($image->getImageFile(), $options);

if (!$saveFile) {
return $webPInfo;
}

return $webPInfo['path'];
return new WebPInformation(WebPConverter::createWebPImage($image->getImageFile(), $options));
}

/**
Expand All @@ -40,7 +36,11 @@ public function convertedImageExists(Image $image): bool
return (file_exists($image->getConvertedFullPath()));
}

private function createOptionsArray(Image $image): array
/**
* @param $image
* @return array
*/
private function createOptionsArray($image): array
{
return [
'quality' => $image->getQuality(),
Expand All @@ -49,4 +49,4 @@ private function createOptionsArray(Image $image): array
'filenameSuffix' => $image->getConvertedFilenameSuffix()
];
}
}
}
1 change: 0 additions & 1 deletion src/Service/ImageUploadHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function __construct(string $uploadPath, string $projectDir)

/**
* @param UploadedFile $uploadedFile
* @return File
* @throws Exception
*/
public function uploadAction(UploadedFile $uploadedFile): File
Expand Down

0 comments on commit 49edf01

Please sign in to comment.