Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Created February 1, 2014 18:31
Show Gist options
  • Save clouddueling/8756469 to your computer and use it in GitHub Desktop.
Save clouddueling/8756469 to your computer and use it in GitHub Desktop.
<?php
use Aws\S3\S3Client;
use Gregwar\Image\Image;
use Gregwar\Image\GarbageCollect;
class Image_Controller extends Base_Controller
{
public function get_index()
{
$s = Api::inputs(array(
'image' => null,
'width' => 0,
'height' => 0,
'size' => null,
'type' => 'auto',
'quality' => 100,
'x' => 0,
'y' => 0,
'cache' => 'true'
));
$cache = path('storage') . "cache/";
// Set height and width if the size is set.
if ($s['width'] === 0 && $s['height'] === 0 && $s['size'] !== null) {
$s['width'] = $s['height'] = $s['size'];
}
// if not size options are set default the size
if ($s['width'] === 0 && $s['height'] === 0 && $s['size'] === null) {
$s['width'] = $s['height'] = $s['size'] = 70;
}
if ($s['image'] === null || empty($s['image'])) {
$image = Image::open(path('public') . 'img/no_user_image.png');
return Api::response($image->get(), 200, [
'Content-Type' => 'image/png'
]);
}
//GarbageCollect::dropOldFiles(path('storage') . 'cache', 5);
$image_name = "{$s['width']}x{$s['height']}-{$s['size']}-{$s['type']}-{$s['image']}";
// Affects the header below.
if ($s['cache'] === 'false') {
if (is_file($cache . $s['image'])) {
unlink($cache . $s['image']);
}
if (is_file($cache . $image_name)) {
unlink($cache . $image_name);
}
}
if (! is_file($cache . $image_name)) {
if (! is_file($cache . $s['image'])) {
try {
$bucket = Config::get('aws.public_bucket');
$key = 'uploads/' . $s['image'];
$client = S3Client::factory([
'key' => Config::get('aws.key'),
'secret' => Config::get('aws.secret'),
]);
$result = $client->getObject(array(
'Bucket' => $bucket,
'Key' => $key,
'SaveAs' => $cache . $s['image']
));
} catch (Aws\S3\Exception\NoSuchKeyException $e) {
$image = Image::open(path('public') . 'img/no_user_image.png')
->save($cache . $s['image']);
}
}
$image = Image::open($cache . $s['image'])
->cropResize($s['width'], $s['height'], 'transparent');
}
return Api::response($image->get(), 200, [
'Content-Type' => 'image/png'
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment