1
0
Fork 0
mirror of synced 2024-06-24 01:00:35 +12:00

Added route for cropping remote images URLs

This commit is contained in:
eldadfux 2019-07-09 23:20:39 +03:00
parent 7ae7601e4e
commit 4bf990420b
2 changed files with 77 additions and 8 deletions

View file

@ -122,6 +122,75 @@ $utopia->get('/v1/avatars/flags/:code')
->label('sdk.description', 'You can use this endpoint to show different country flags icons to your users, The code argument receives the a 2 letter country code. Use width, height and quality arguments to change the output settings.')
->action(function ($code, $width, $height, $quality) use ($avatarCallback) {return $avatarCallback('flags', $code, $width, $height, $quality);});
$utopia->get('/v1/avatars/image')
->desc('Get image from and HTTP URL and crop to any size.')
->param('url', '', function () {return new URL();}, 'Image URL which you want to crop.')
->param('width', 400, function () {return new Range(0, 2000);}, 'Resize preview image width, Pass an integer between 0 to 4000', true)
->param('height', 400, function () {return new Range(0, 2000);}, 'Resize preview image height, Pass an integer between 0 to 4000', true)
->label('scope', 'avatars.read')
->label('sdk.namespace', 'avatars')
->label('sdk.method', 'getImage')
->label('sdk.description', 'Use this endpoint to fetch a remote image URL and crop it to any image size you want.')
->action(
function($url, $width, $height) use ($response, $request, $version)
{
$quality = 80;
$output = 'png';
$date = date('D, d M Y H:i:s', time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache
$key = md5('/v2/avatars/images-' . $url . '-' . $width . '/' . $height . '/' . $quality);
$type = 'png';
$cache = new Cache(new Filesystem('/storage/cache/app-0')); // Limit file number or size
$data = $cache->load($key, 60 * 60 * 24 * 7 /* 1 week */);
if($data) {
$response
->setContentType('image/png')
->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'hit')
->send($data, 0)
;
}
if (!extension_loaded('imagick')) {
throw new Exception('Imagick extension is missing', 500);
}
$fetch = @file_get_contents($url, false);
if(!$fetch) {
throw new Exception('Image not found', 404);
}
try {
$resize = new Resize($fetch);
}
catch (\Exception $exception) {
throw new Exception('Unable to parse image', 500);
}
$resize->crop((int)$width, (int)$height);
$output = (empty($output)) ? $type : $output;
$response
->setContentType('image/png')
->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'miss')
->send('', null)
;
$data = $resize->output($output, $quality);
$cache->save($key, $data);
echo $data;
unset($resize);
exit(0);
}
);
$utopia->get('/v1/avatars/favicon')
->desc('Get Favicon')
->param('url', '', function () {return new URL();}, 'Website URL which you want to fetch the favicon from.')
@ -191,7 +260,7 @@ $utopia->get('/v1/avatars/favicon')
switch (strtolower($rel)) {
case 'icon':
case 'shortcut icon':
//case 'apple-touch-icon':
//case 'apple-touch-icon':
$ext = pathinfo(parse_url($absolute, PHP_URL_PATH), PATHINFO_EXTENSION);
switch ($ext) {
@ -210,10 +279,10 @@ $utopia->get('/v1/avatars/favicon')
$outputExt = $ext;
}
break;
break;
}
break;
break;
}
}

View file

@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS `template.abuse.abuse` (
PRIMARY KEY (`id`),
UNIQUE KEY `unique1` (`_key`,`_time`),
KEY `index1` (`_key`,`_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `template.audit.audit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS `template.audit.audit` (
KEY `index_1` (`userId`,`userType`),
KEY `index_2` (`event`),
KEY `index_3` (`resource`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `template.database.documents` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Unique ID for each node',
@ -43,7 +43,7 @@ CREATE TABLE IF NOT EXISTS `template.database.documents` (
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `index2` (`uid`),
KEY `index3` (`signature`,`uid`,`revision`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `template.database.properties` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
@ -58,7 +58,7 @@ CREATE TABLE IF NOT EXISTS `template.database.properties` (
KEY `index1` (`documentUid`),
KEY `index2` (`key`,`value`(5)),
FULLTEXT KEY `index3` (`value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `template.database.relationships` (
`id` int(11) NOT NULL AUTO_INCREMENT,
@ -72,7 +72,7 @@ CREATE TABLE IF NOT EXISTS `template.database.relationships` (
PRIMARY KEY (`id`),
KEY `relationships_start_nodes_id_idx` (`start`),
KEY `relationships_end_nodes_id_idx` (`end`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/* Default App */