From 4299c6916ffefe054676c8f45ab6b9f5492de0b7 Mon Sep 17 00:00:00 2001 From: danpros Date: Sun, 30 Jun 2024 13:02:03 +0700 Subject: [PATCH] Accept height parameter --- system/includes/dispatch.php | 30 ++++++++++++++++++++++++++---- system/includes/functions.php | 4 ++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/system/includes/dispatch.php b/system/includes/dispatch.php index f47ac79..35a07b1 100644 --- a/system/includes/dispatch.php +++ b/system/includes/dispatch.php @@ -625,16 +625,31 @@ function flash($key, $msg = null, $now = false) $x[$key] = $msg; } -function create_thumb($src, $desired_width) { +function create_thumb($src, $desired_width = null, $desired_height = null) { $dir = 'content/images/thumbnails'; if (!is_dir($dir)) { mkdir($dir); } + + $w = config('thumbnail.width'); + if (empty($w)) { + $w = 500; + } + + if (is_null($desired_width)) { + $desired_width = $w; + } + + if (!is_null($desired_height)) { + $h = 'x' . $desired_height; + } else { + $h = null; + } $fileName = pathinfo($src, PATHINFO_FILENAME); - $thumbFile = $dir . '/' . $fileName . '-' . $desired_width . '.webp'; + $thumbFile = $dir . '/' . $fileName . '-' . $desired_width . $h .'.webp'; if (file_exists($thumbFile)) { return site_url() . $thumbFile; @@ -646,13 +661,20 @@ function create_thumb($src, $desired_width) { $height = imagesy($source_image); /* find the "desired height" of this thumbnail, relative to the desired width */ - $desired_height = floor($height * ($desired_width / $width)); + if (is_null($desired_height)) { + $desired_height = floor($height * ($desired_width / $width)); + } + + $ratio = max($desired_width/$width, $desired_height/$height); + $height = $desired_height / $ratio; + $x = ($width - $desired_width / $ratio) / 2; + $width = $desired_width / $ratio; /* 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); + imagecopyresampled($virtual_image, $source_image, 0, 0, $x, 0, $desired_width, $desired_height, $width, $height); /* create the physical thumbnail image to its destination */ imagewebp($virtual_image, $thumbFile, 75); diff --git a/system/includes/functions.php b/system/includes/functions.php index a443e7b..3745283 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -2402,7 +2402,7 @@ function get_thumbnail($text, $url = null) } // Get image from post and Youtube thumbnail. -function get_image($text, $width = null) +function get_image($text, $width = null, $height = null) { libxml_use_internal_errors(true); $dom = new DOMDocument(); @@ -2415,7 +2415,7 @@ function get_image($text, $width = null) if(is_null($width)) { return $imgSource; } else { - return create_thumb($imgSource, $width); + return create_thumb($imgSource, $width, $height); } } elseif ($vidTags->length > 0) { $vidElement = $vidTags->item(0);