oc-resizer-plugin/console/ResizeMake.php

178 lines
5.6 KiB
PHP

<?php namespace Aweos\Resizer\Console;
use Storage;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use System\Classes\MediaLibrary;
use Aweos\Resizer\Models\Setting;
use GuzzleHttp\Exception\ClientException;
use October\Rain\Database\Attach\Resizer;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
class ResizeMake extends Command
{
public $media = null;
public $http = null;
/**
* @var string The console command name.
*/
protected $name = 'resize:make';
/**
* @var string The console command description.
*/
protected $description = 'Resizes configured images';
public function forAllResizables($callback) {
$folders = Setting::get('folders');
$sizes = Setting::get('srcx');
sort($sizes);
foreach($folders as $folder) {
$this->start($folder['folder'], $sizes, $callback);
}
}
/**
* Execute the console command.
* @return void
*/
public function handle()
{
$this->http = new Client(['base_uri' => 'https://api.tinify.com']);
$this->media = MediaLibrary::instance();
ProgressBar::setFormatDefinition('custom', '%current%/%max% %bar% -- %message% (%size%)');
$tinifyKey = Setting::get('tinify') ? Setting::get('tinypngkey') : null;
$i = 0;
$this->forAllResizables(function($file, $w) use (&$i) {
$i++;
});
$bar = $this->output->createProgressBar($i);
$bar->setFormat('custom');
/**
* @param File $file The current filename
* @param int $w Current width
* @param string $compressed compressed file
* @param string $uncompressed uncompressed file
*/
$this->forAllResizables(function($file, $w, $compressed, $uncompressed) use ($bar, $tinifyKey) {
$bar->setMessage($file->path);
$bar->setMessage($w, 'size');
$bar->advance();
if (Storage::exists($compressed)) {
return;
}
if (!Storage::exists($uncompressed)) {
$r = Resizer::open(Storage::disk('local')->path('media'.$file->path));
$r->resize($w, 0);
$r->save(Storage::disk('local')->path($uncompressed));
$this->info(Storage::url($uncompressed));
}
if (!is_null($tinifyKey)) {
$newUrl = url($this->media->getPathUrl(preg_replace('/^media/', '', $uncompressed)));
try {
$response = $this->http->post('/shrink', [
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic '.base64_encode('api:'.$tinifyKey)
],
'json' => [
'source' => ['url' => $newUrl]
]
]);
$output = json_decode((string) $response->getBody());
$url = $output->output->url;
$response = $this->http->get($output->output->url, [
'headers' => [
'Authorization' => 'Basic '.base64_encode('api:'.$tinifyKey)
]
]);
$image = (string) $response->getBody();
Storage::put($compressed, $image);
Storage::delete($uncompressed);
} catch(ClientException $e) {
return;
}
}
$bar->advance();
});
$bar->finish();
}
public function getMediaOfType($f, $type) {
return array_filter($this->media->listFolderContents($f, 'title', 'image'), function($item) use ($f, $type) {
return $item->type == $type && $item->path != $f.'/c';
});
}
public function start($f, $sizes, $callback) {
$f = '/'.ltrim(rtrim($f, '/'), '/');
$folders = $this->getMediaOfType($f, 'folder');
foreach ($folders as $folder) {
$this->start($folder->path, $sizes, $callback);
}
$files = $this->getMediaOfType($f, 'file');
// Create C-Folder as current subfolder if it doesnt exist yet
if(count($files) && !$this->media->folderExists($f.'/c')) {
$this->media->makeFolder($f.'/c');
}
foreach ($files as $file) {
foreach ($sizes as $w) {
$imagesize = getimagesize(Storage::path('media'.$file->path));
$uncompressedFilename = pathinfo($file->path, PATHINFO_FILENAME).'-'.$w.'.'.pathinfo($file->path, PATHINFO_EXTENSION);
$compressedFilename = pathinfo($file->path, PATHINFO_FILENAME).'-'.$w.'t.'.pathinfo($file->path, PATHINFO_EXTENSION);
$compressed = 'media'.dirname($file->path).'/c/'.$compressedFilename;
$uncompressed = 'media'.dirname($file->path).'/c/'.$uncompressedFilename;
if ($imagesize[0] < $w) {
continue;
}
call_user_func($callback, $file, $w, $compressed, $uncompressed);
}
}
}
/**
* Get the console command arguments.
* @return array
*/
protected function getArguments()
{
return [];
}
/**
* Get the console command options.
* @return array
*/
protected function getOptions()
{
return [];
}
}