NanoGal/public/createthumb.php

67 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2024-08-23 16:13:41 +02:00
<?php
use Utils\Utils;
error_reporting(-1);
require_once '../vendor/stefangabos/zebra_image/Zebra_Image.php';
require_once '../app/Utils.php';
$config = [
'thumbSize' => 250
];
if (file_exists('../datas/config.php')) {
include '../datas/config.php';
$config = array_merge($config, $userConfig);
}
2024-10-04 16:25:50 +02:00
$getFilename = $_GET['filename'];
2024-08-23 16:13:41 +02:00
$baseDir = getcwd();
2024-10-04 16:25:50 +02:00
if (!Utils::isPathAuthorized($getFilename)) {
2024-08-23 16:13:41 +02:00
die("ERROR 01: Unauthorized access!");
}
if (!is_dir('../cache/thumbs') && is_writable('.')) {
mkdir('../cache/thumbs', 0700);
}
2024-10-04 16:25:50 +02:00
$pathInfos = pathinfo($getFilename);
2024-08-23 16:13:41 +02:00
$dirname = '../cache/' . str_replace('photos', 'thumbs', $pathInfos['dirname']);
$filename = $pathInfos['filename'];
$thumbname = '../cache/' . $dirname . '/' . $filename . '.' . $pathInfos['extension'] . '.webp';
if (file_exists($thumbname)) {
$fd = fopen($thumbname, "r");
$cacheContent = fread($fd, filesize($thumbname));
fclose($fd);
header('Content-type: image/webp');
echo ($cacheContent);
exit;
}
2024-10-04 16:25:50 +02:00
if (!is_readable($getFilename) || !is_file($getFilename)) {
2024-08-23 16:13:41 +02:00
header('Content-type: image/svg+xml');
$cannotopenImg = file_get_contents('assets/images/cannotopen.svg');
echo $cannotopenImg;
exit;
}
if (!file_exists($dirname)) {
if (!mkdir($dirname, 0700, true)) {
die('We can\'t create dir, check permission of parent directory.');
}
}
$image = new Zebra_Image();
$image->auto_handle_exif_orientation = true;
2024-10-04 16:25:50 +02:00
$image->source_path = $getFilename;
2024-08-23 16:13:41 +02:00
$image->target_path = $thumbname;
if (!$image->resize($config['thumbSize'], $config['thumbSize'], ZEBRA_IMAGE_CROP_CENTER)) {
} else {
header('Content-type: image/webp');
echo file_get_contents($thumbname);
exit();
}