Thumbnail

This commit is contained in:
danpros 2024-06-28 14:31:22 +07:00
commit 852ba14d02
5 changed files with 70 additions and 10 deletions

View file

@ -112,6 +112,12 @@ if (empty($defaultFormat)) {
<input type="text" name="-config-default.image" class="form-control" id="default.image" value="<?php echo config('default.image');?>" placeholder="<?php echo site_url();?>system/resources/images/logo-big.png">
</div>
</div>
<div class="form-group row">
<label for="thumbnail.width" class="col-sm-2 col-form-label">Thumbnail Width</label>
<div class="col-sm-10">
<input type="number" name="-config-thumbnail.width" class="form-control" id="thumbnail.width" value="<?php echo config('thumbnail.width');?>">
</div>
</div>
<br>
<h4>Title formats</h4>
<hr>

View file

@ -89,5 +89,6 @@
"views.layout",
"autosave.enable",
"mfa.state",
"show.version"
"show.version",
"thumbnail.width"
]

View file

@ -625,6 +625,44 @@ function flash($key, $msg = null, $now = false)
$x[$key] = $msg;
}
function create_thumb($src, $desired_width) {
$dir = 'content/images/thumbnails';
if (!is_dir($dir)) {
mkdir($dir);
}
$fileName = pathinfo($src, PATHINFO_FILENAME);
$thumbFile = $dir . '/' . $fileName . '-' . $desired_width . '.jpg';
if (file_exists($thumbFile)) {
return site_url() . $thumbFile;
} else {
/* read the source image */
$source_image = imagecreatefromstring(file_get_contents($src));
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $thumbFile);
imagedestroy($virtual_image);
return site_url() . $thumbFile;
}
}
function dispatch()
{
$path = $_SERVER['REQUEST_URI'];

View file

@ -226,7 +226,7 @@ function scan_images() {
static $_images = array();
if (empty($_images)) {
$tmp = array();
$tmp = glob('content/images/*', GLOB_NOSORT);
$tmp = array_filter(glob('content/images/*', GLOB_NOSORT), 'is_file');
if (is_array($tmp)) {
foreach ($tmp as $file) {
$_images[] = pathinfo($file);
@ -2402,7 +2402,7 @@ function get_thumbnail($text, $url = null)
}
// Get image from post and Youtube thumbnail.
function get_image($text)
function get_image($text, $width = null)
{
libxml_use_internal_errors(true);
$dom = new DOMDocument();
@ -2412,7 +2412,11 @@ function get_image($text)
if ($imgTags->length > 0) {
$imgElement = $imgTags->item(0);
$imgSource = $imgElement->getAttribute('src');
return $imgSource;
if(is_null($width)) {
return $imgSource;
} else {
return create_thumb($imgSource, $width);
}
} elseif ($vidTags->length > 0) {
$vidElement = $vidTags->item(0);
$vidSource = $vidElement->getAttribute('src');

View file

@ -15,32 +15,43 @@ if (config('timezone')) {
$whitelist = array('jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png', 'gif', 'webp');
$name = null;
$dir = 'content/images/';
$dirThumb = 'content/images/thumbnails/';
$error = null;
$timestamp = date('YmdHis');
$path = null;
$width = config('thumbnail.width');
if (login()) {
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
if (is_null($width) || empty($width)) {
$width = 500;
}
if (isset($_FILES) && isset($_FILES['file'])) {
$tmp_name = $_FILES['file']['tmp_name'];
$name = basename($_FILES['file']['name']);
$error = $_FILES['file']['error'];
$path = $dir . $timestamp . '-' . $name;
$check = getimagesize($tmp_name);
if($check !== false) {
if ($error === UPLOAD_ERR_OK) {
$extension = pathinfo($name, PATHINFO_EXTENSION);
if (!in_array(strtolower($extension), $whitelist)) {
$error = 'Invalid file type uploaded.';
} else {
move_uploaded_file($tmp_name, $dir . $timestamp . '-' . $name);
move_uploaded_file($tmp_name, $path);
}
$imageFile = pathinfo($path, PATHINFO_FILENAME);
$thumbFile = $dirThumb . $imageFile. '-' . $width . '.jpg';
if (!file_exists($thumbFile)) {
create_thumb($path, $width);
}
}
} else {
$error = "File is not an image.";
@ -53,7 +64,7 @@ if (login()) {
'name' => $name,
'error' => $error,
));
die();
} else {