Skip to content

Instantly share code, notes, and snippets.

@Ryuske
Created May 9, 2016 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ryuske/15daf0daddba925c6461605c3fe92462 to your computer and use it in GitHub Desktop.
Save Ryuske/15daf0daddba925c6461605c3fe92462 to your computer and use it in GitHub Desktop.
Laravel 4.2 artisan command for compressing AWS images
<?php
/**
* Command for losslessly compressing Amazon AWS images
*/
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
/**
* Class for losslessly compressing Amazon AWS images
*
* Class CompressAwsImages
* @package App\Commands
*/
class CompressAwsImages extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'images:compress-aws';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Apply lossless compression on Amazon S3 images.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$s3 = AWS::get('s3');
$file = storage_path() . '/tmp/s3_image';
$objects = $s3->getIterator('ListObjects', [
'Bucket' => 'mymv3'
]);
if (NULL !== $this->option('key')) {
$objects = [['Key' => $this->option('key')]];
}
foreach ($objects as $object) {
if (false !== strstr($object['Key'], '.')) {
$image = file_get_contents('https://mymv3.s3.amazonaws.com/' . $object['Key']);
$bytes_written = File::put($file, $image);
if (false === $bytes_written) {
$this->error('There was an error writing the file, ' . $object['Key']);
}
$file_type = exif_imagetype($file);
$file_size = filesize($file);
switch ($file_type) {
case 2: //JPG
system('jpegoptim --strip-all ' . $file);
break;
case 3: //PNG
$request = curl_init();
curl_setopt_array($request, array(
CURLOPT_URL => "https://api.tinypng.com/shrink",
CURLOPT_USERPWD => "api:WDDDbc1k-jqvF_t-E-3dNOkG-L8BSjGx",
CURLOPT_POSTFIELDS => file_get_contents($file),
CURLOPT_BINARYTRANSFER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYPEER => true
));
$response = curl_exec($request);
if (curl_getinfo($request, CURLINFO_HTTP_CODE) === 201) {
$headers = substr($response, 0, curl_getinfo($request, CURLINFO_HEADER_SIZE));
foreach (explode("\r\n", $headers) as $header) {
if (substr($header, 0, 10) === "Location: ") {
$request = curl_init();
curl_setopt_array($request, array(
CURLOPT_URL => substr($header, 10),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true
));
File::put($file, curl_exec($request));
}
}
} else {
$this->error('There was a problem with TinyPNG');
}
break;
default:
$this->error('Unknown Imagetype Constant: ' . $file_type);
}
if ($file_size != filesize($file) || NULL !== $this->option('key')) {
$s3->putObject([
'Bucket' => 'mymv3',
'Key' => $object['Key'],
'SourceFile' => $file,
'ACL' => 'public-read',
dol
]);
$this->info('Optimized file uploaded.');
} else {
$this->info('File sizes were the same.');
}
}
}
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
['key', NULL, InputOption::VALUE_OPTIONAL, 'Optimize a specific object.']
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment