87 lines
2.9 KiB
PHP
87 lines
2.9 KiB
PHP
<?php
|
|
// include composer autoload
|
|
require 'vendor/autoload.php';
|
|
ini_set('memory_limit', '4G');
|
|
ini_set( 'user_agent', 'CoolBot/0.0 (https://example.org/coolbot/; coolbot@example.org)' );
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
// import the Intervention Image Manager Class
|
|
use Intervention\Image\Exceptions\DecoderException;
|
|
use Intervention\Image\ImageManager;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
// create an image manager instance with favored driver
|
|
$manager = new ImageManager(\Intervention\Image\Drivers\Imagick\Driver::class);
|
|
if(php_sapi_name() == 'cli') {
|
|
$args = NULL;
|
|
parse_str($argv[1], $args);
|
|
} else {
|
|
$args = $_GET;
|
|
}
|
|
$options = [
|
|
'url' => $args['url'],
|
|
'height' => $args['h'] ?? NULL,
|
|
'width' => $args['w'] ?? NULL,
|
|
'canvasheight' => $args['ch'] ?? NULL,
|
|
'canvaswidth' => $args['cw'] ?? NULL,
|
|
'format' => $args['format'] ?? NULL,
|
|
'fill' => $args['fill'] ?? NULL,
|
|
];
|
|
$cachefolder = [];
|
|
foreach($options as $key => $option) {
|
|
if($key != 'url' && $option != NULL) $cachefolder[] = $key . '-' . $option;
|
|
}
|
|
$cachefolder = implode('_', $cachefolder);
|
|
$cachefile = preg_replace("/[^A-Za-z0-9]/", '-', $options['url']);
|
|
|
|
if(is_dir('cache/' . $cachefolder)) {
|
|
if(is_file('cache/'. $cachefolder . '/' . $cachefile) && (filemtime('cache/'. $cachefolder . '/' . $cachefile) > (time() - (60*60*24*30)))) {
|
|
sendFile('cache/' . $cachefolder . '/' . $cachefile);
|
|
exit;
|
|
}
|
|
};
|
|
$imagecontent = file_get_contents($options['url']);
|
|
if(isSvg($imagecontent)) {
|
|
$image = new Imagick();
|
|
$image->setBackgroundColor('none');
|
|
$image->readImageBlob($imagecontent);
|
|
$image->setImageFormat("png64");
|
|
$image->resizeImage(2000,2000, imagick::FILTER_QUADRATIC, 0.7, true);
|
|
$imagecontent = $image->getImageBlob();
|
|
}
|
|
|
|
$transparent = (new \Intervention\Image\Colors\Rgb\Color(0,0,0,0));
|
|
// to finally create image instances
|
|
$image = $manager
|
|
->read($imagecontent)
|
|
->pad($options['width'], $options['height'], $transparent)
|
|
->resizeCanvas($options['canvaswidth'], $options['canvasheight'], $transparent);
|
|
|
|
if (isset($options['fill'])) {
|
|
$image = $image->blendTransparency($options['fill']);
|
|
}
|
|
|
|
// send HTTP header and output image data
|
|
if(!is_dir('cache/' . $cachefolder)) mkdir('cache/' . $cachefolder);
|
|
if(isset($args['format'])) {
|
|
$image = $image->encodeByExtension($args['format']);
|
|
}
|
|
$image->save('cache/' . $cachefolder . '/' . $cachefile);
|
|
sendFile('cache/' . $cachefolder . '/' . $cachefile);
|
|
|
|
function sendFile($filepath) {
|
|
$filecontents = file_get_contents($filepath);
|
|
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $filecontents);
|
|
$length = strlen($filecontents);
|
|
|
|
$response = new Response($filecontents, Response::HTTP_OK, ['Content-Type' => $mime, 'Content-Length' => $length]);
|
|
$response->send();
|
|
}
|
|
|
|
function isSvg($content)
|
|
{
|
|
// Controleer of de inhoud begint met de kenmerkende SVG-declaratie
|
|
return strpos($content, '<svg') !== false;
|
|
}
|
|
|