commit
c50ac9e462
81
api.php
81
api.php
@ -6,63 +6,72 @@ $js = $_GET['js'];
|
|||||||
$resizewidth = $_GET['width'];
|
$resizewidth = $_GET['width'];
|
||||||
$cache = preg_replace("/[^A-Za-z0-9 ]/", '', $_GET['cache']);
|
$cache = preg_replace("/[^A-Za-z0-9 ]/", '', $_GET['cache']);
|
||||||
$onfail = rawurldecode($_GET['onfail']);
|
$onfail = rawurldecode($_GET['onfail']);
|
||||||
if(!$onfail)
|
|
||||||
|
if (!$onfail) {
|
||||||
$onfail = 'https://http2pic.haschek.at/img/failed.jpg';
|
$onfail = 'https://http2pic.haschek.at/img/failed.jpg';
|
||||||
|
}
|
||||||
|
|
||||||
$url = rawurldecode($_GET['url']);
|
$url = rawurldecode($_GET['url']);
|
||||||
if(isBase64($url))
|
if (isBase64($url)) {
|
||||||
$url = base64_decode($url);
|
$url = base64_decode($url);
|
||||||
|
}
|
||||||
|
|
||||||
if(!$timeout || !is_numeric($timeout) || ($timeout>30 || $timeout<1))
|
if (!$timeout || !is_numeric($timeout) || ($timeout > 30 || $timeout < 1)) {
|
||||||
$timeout = 10;
|
$timeout = 10;
|
||||||
|
}
|
||||||
|
|
||||||
if($viewport)
|
if ($viewport) {
|
||||||
{
|
|
||||||
$a = explode('x', $viewport);
|
$a = explode('x', $viewport);
|
||||||
$w = $a[0];
|
$w = $a[0];
|
||||||
$h = $a[1];
|
$h = $a[1];
|
||||||
if($w)
|
if ($w) {
|
||||||
$vp = "--width $w ";
|
$vp = "--width $w ";
|
||||||
if($h)
|
}
|
||||||
|
if ($h) {
|
||||||
$vp .= "--height $h ";
|
$vp .= "--height $h ";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($js=='no')
|
if ($js == 'no') {
|
||||||
$jsp = '-n ';
|
$jsp = '-n ';
|
||||||
|
}
|
||||||
|
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case 'png':
|
case 'png':
|
||||||
$ft = $type;
|
$fileType = $type;
|
||||||
header('Content-Type: image/png');
|
header('Content-Type: image/png');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'jpg':
|
case 'jpg':
|
||||||
case 'jpeg':
|
case 'jpeg':
|
||||||
default:
|
default:
|
||||||
$ft = 'jpg';
|
$fileType = 'jpg';
|
||||||
header('Content-Type: image/jpeg');
|
header('Content-Type: image/jpeg');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$hash = $cache.'-'.preg_replace("/[^A-Za-z0-9 ]/", '', $url).'.'.$fileType;
|
||||||
|
|
||||||
|
if (!$cache) {
|
||||||
$hash = $cache.'-'.preg_replace("/[^A-Za-z0-9 ]/", '', $url).'.'.$ft;
|
|
||||||
if(!$cache)
|
|
||||||
$hash = md5(time().rand(1,2000)).$hash;
|
$hash = md5(time().rand(1,2000)).$hash;
|
||||||
|
}
|
||||||
|
|
||||||
if(!is_dir(__DIR__.'/cache/'))
|
if (!is_dir(__DIR__.'/cache/')) {
|
||||||
mkdir(__DIR__.'/cache/');
|
mkdir(__DIR__.'/cache/');
|
||||||
|
}
|
||||||
|
|
||||||
$file = __DIR__.'/cache/'.$hash;
|
$file = __DIR__.'/cache/'.$hash;
|
||||||
if(!file_exists($file))
|
|
||||||
shell_exec('timeout '.$timeout.' /usr/sbin/wkhtmltoimage '.escapeshellcmd($vp.$jsp.'-f '.$ft.' '.$url.' '.$file));
|
|
||||||
|
|
||||||
if(filesize($file)==0 && $onfail)
|
if (!file_exists($file)) {
|
||||||
|
shell_exec('timeout '.$timeout.' /usr/sbin/wkhtmltoimage '.escapeshellcmd($vp.$jsp.'-f '.$fileType.' '.$url.' '.$file));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filesize($file) == 0 && $onfail) {
|
||||||
@file_put_contents($file, file_get_contents($onfail));
|
@file_put_contents($file, file_get_contents($onfail));
|
||||||
|
}
|
||||||
|
|
||||||
if($resizewidth)
|
if ($resizewidth) {
|
||||||
{
|
|
||||||
list($width_orig, $height_orig) = getimagesize($file);
|
list($width_orig, $height_orig) = getimagesize($file);
|
||||||
if($width_orig!=$resizewidth)
|
|
||||||
{
|
if ($width_orig != $resizewidth) {
|
||||||
$ratio_orig = $width_orig/$height_orig;
|
$ratio_orig = $width_orig/$height_orig;
|
||||||
$height = $resizewidth/$ratio_orig;
|
$height = $resizewidth/$ratio_orig;
|
||||||
|
|
||||||
@ -75,27 +84,25 @@ if($resizewidth)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if($ft=='jpg')
|
if ($fileType === 'jpg') {
|
||||||
{
|
$result = imagecreatefromjpeg($file);
|
||||||
$res = imagecreatefromjpeg($file);
|
imagejpeg($result, NULL, 100);
|
||||||
imagejpeg($res,NULL,100);
|
|
||||||
}
|
}
|
||||||
else if($ft=='png')
|
else if ($fileType === 'png') {
|
||||||
{
|
$result = imagecreatefrompng($file);
|
||||||
$res = imagecreatefrompng($file);
|
imagepng($result, NULL, 9);
|
||||||
imagepng($res,NULL,9);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
imagedestroy($res);
|
imagedestroy($result);
|
||||||
|
|
||||||
if(!$cache)
|
if (!$cache) {
|
||||||
unlink($file);
|
unlink($file);
|
||||||
|
}
|
||||||
|
|
||||||
function isBase64($data)
|
function isBase64($data) {
|
||||||
{
|
if (base64_encode(base64_decode($data, true)) === $data) {
|
||||||
if ( base64_encode(base64_decode($data, true)) === $data)
|
|
||||||
return true;
|
return true;
|
||||||
else
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user