From 852ba14d025e4e2285d528a800379d83d3fd2a7a Mon Sep 17 00:00:00 2001 From: danpros Date: Fri, 28 Jun 2024 14:31:22 +0700 Subject: [PATCH] Thumbnail --- system/admin/views/config-metatags.html.php | 6 ++++ system/configList.json | 3 +- system/includes/dispatch.php | 38 +++++++++++++++++++++ system/includes/functions.php | 10 ++++-- upload.php | 23 +++++++++---- 5 files changed, 70 insertions(+), 10 deletions(-) diff --git a/system/admin/views/config-metatags.html.php b/system/admin/views/config-metatags.html.php index a343759..056e76a 100644 --- a/system/admin/views/config-metatags.html.php +++ b/system/admin/views/config-metatags.html.php @@ -112,6 +112,12 @@ if (empty($defaultFormat)) { +
+ +
+ +
+

Title formats


diff --git a/system/configList.json b/system/configList.json index 0f041c4..c0b3e56 100644 --- a/system/configList.json +++ b/system/configList.json @@ -89,5 +89,6 @@ "views.layout", "autosave.enable", "mfa.state", - "show.version" + "show.version", + "thumbnail.width" ] \ No newline at end of file diff --git a/system/includes/dispatch.php b/system/includes/dispatch.php index 3c78366..6f2ea82 100644 --- a/system/includes/dispatch.php +++ b/system/includes/dispatch.php @@ -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']; diff --git a/system/includes/functions.php b/system/includes/functions.php index d07dff2..6787175 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -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'); diff --git a/upload.php b/upload.php index ab4fbc4..9882d77 100644 --- a/upload.php +++ b/upload.php @@ -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 {