Skip to content

Commit

Permalink
Now supports image optimize #549
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Dec 23, 2024
1 parent 7fc2886 commit 22c42ad
Showing 1 changed file with 55 additions and 54 deletions.
109 changes: 55 additions & 54 deletions src/Upload/FileUploadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Intervention\Image\Interfaces\ImageInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Spatie\ImageOptimizer\OptimizerChainFactory;
use Symfony\Component\Mime\MimeTypesInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Unicorn\Flysystem\Base64DataUri;
Expand All @@ -20,11 +21,14 @@
use Unicorn\Upload\Event\FileUploadedEvent;
use Unicorn\Upload\Exception\FileUploadException;
use Windwalker\Core\Event\CoreEventAwareTrait;
use Windwalker\Core\Manager\Logger;
use Windwalker\Event\EventAwareInterface;
use Windwalker\Filesystem\Filesystem;
use Windwalker\Filesystem\Path;
use Windwalker\Filesystem\TempFileObject;
use Windwalker\Http\Helper\UploadedFileHelper;
use Windwalker\Stream\Stream;
use Windwalker\Stream\StreamHelper;
use Windwalker\Stream\StringStream;
use Windwalker\Utilities\Options\OptionsResolverTrait;
use Windwalker\Utilities\TypeCast;
Expand All @@ -33,6 +37,8 @@
use function Windwalker\uid;

use const Windwalker\Stream\READ_ONLY_FROM_BEGIN;
use const Windwalker\Stream\READ_WRITE_FROM_BEGIN;
use const Windwalker\Stream\READ_WRITE_RESET;

/**
* The FileUploadService class.
Expand Down Expand Up @@ -104,7 +110,7 @@ function (OptionsResolver $resizeResolver) {
->default('local');

$resolver->define('optimize')
->allowedTypes('bool', 'int')
->allowedTypes('bool', 'int', 'array')
->default(false);

$resolver->define('options')
Expand Down Expand Up @@ -388,70 +394,65 @@ public function resizeImage(
* @param array $resizeConfig
*
* @return StreamInterface
*
* @deprecated This method is not work currently
* @throws \Exception
*/
public function optimizeImage(StreamInterface $src, string $dest, array $resizeConfig): StreamInterface
{
$optimize = $resizeConfig['strip_exif'] || $this->options['optimize'];
$optimize = $resizeConfig['strip_exif'] ?: $this->options['optimize'];

if (!$optimize) {
if ($optimize === false) {
return $src;
}

// $filename = Path::getFilename($dest);
// $ext = Path::getExtension($dest);

// Todo: Test pngauant at Linux
// if ($ext === 'png') {
// $tmp = WINDWALKER_TEMP . '/images/' . $filename;
// Filesystem::mkdir(dirname($tmp));
// $tmpFile = new TempFileObject($tmp);
// $tmpFile->touch();
// StreamHelper::copyTo($src, $tmpFile->getStream());
// // $tmpFile->deleteWhenShutdown();
//
// $pngquant = WINDWALKER_TEMP . '/pngquant';
// $quality = $resizeConfig['quality'];
// $input = fopen('php://memory', READ_WRITE_FROM_BEGIN);
// $newImage = new Stream();
//
// $c = sprintf(
// '%s %s',
// $pngquant,
// $tmpFile->getPathname()
// );
// $o = shell_exec($c);
//
// show($o);
if (!class_exists(OptimizerChainFactory::class)) {
throw new \DomainException('Please install `spatie/image-optimizer` first.');
}

// while (!$src->eof()) {
// $buffer = $src->read(8192);
// fwrite($input, $buffer);
//
// $out = $process->getIncrementalOutput();
// $newImage->write($out);
//
// show($out);
// }
//
// $newImage->rewind();
//
// show((string) $newImage);
// exit(' @Checkpoint');
//
// return $newImage;
// }
$options = [];

if (is_array($optimize)) {
$options = $optimize;
}

$optimizer = OptimizerChainFactory::create($options);
$logger = Logger::getChannel('image-optimizer');

if ($logger) {
$optimizer->useLogger($logger);
}

$tmpDir = WINDWALKER_TEMP . '/images/';
Filesystem::mkdir($tmpDir);

$ext = Path::getExtension($dest);
$filename = uid();
$tmp = $tmpDir . $filename . '.' . $ext;
$tmpFile = new TempFileObject($tmp);
$tmpFile->deleteWhenDestruct(true);
$tmpFile->touch();

StreamHelper::copy($src, $tmpFile->getStream());

return $src;
$tmp = $tmpDir . $filename . '.new.' . $ext;
$newFile = new TempFileObject($tmp);
$newFile->deleteWhenDestruct(true);
$newFile->touch();

$optimizer->optimize($tmpFile->getPathname(), $newFile->getPathname());

$newImage = new Stream('php://memory', READ_WRITE_RESET);
StreamHelper::copy($newFile->getStream(), $newImage);
$newImage->rewind();

return $newImage;
}

protected function resizeByIntervension3(
StreamInterface|\SplFileInfo|string|UploadedFileInterface $src,
array $resizeConfig,
?string $outputFormat
): StreamInterface {
$optimize = $resizeConfig['strip_exif'] || $this->options['optimize'];
// $optimize = $resizeConfig['strip_exif'] || $this->options['optimize'];

$image = InterventionImage::read($src, $resizeConfig['driver']);

Expand All @@ -474,11 +475,11 @@ protected function resizeByIntervension3(
$image->scaleDown($width, $height);
}

if ($optimize && $outputFormat === 'png') {
$colors = is_int($this->options['optimize']) ? $this->options['optimize'] : 2048;

$image->reduceColors($colors);
}
// if ($optimize && $outputFormat === 'png') {
// $colors = is_int($this->options['optimize']) ? $this->options['optimize'] : 2048;
//
// $image->reduceColors($colors);
// }

$res = $this->encodeImageByExtension(
$image,
Expand Down

0 comments on commit 22c42ad

Please sign in to comment.