Improve thumbnail handling for image and video search results #2230

This commit is contained in:
Kijin Sung 2024-01-01 16:56:04 +09:00
parent 85fd994156
commit 76034163f9
3 changed files with 139 additions and 19 deletions

View file

@ -706,13 +706,17 @@ function _displayMultimedia(src, width, height, options) {
var clsid = "";
var codebase = "";
var html = "";
var background = "black";
width = parseInt(width, 10);
height = parseInt(height, 10);
if(/\.(gif|jpg|jpeg|bmp|png)$/i.test(src)){
if (/\.(gif|jpe?g|bmp|png|webp)$/i.test(src)){
html = '<img src="'+src+'" width="'+width+'" height="'+height+'" class="thumb" />';
} else {
html = '<span style="position:relative;background:black;width:' + width + 'px;height:' + height + 'px" class="thumb">';
if (options.thumbnail) {
background += " url('" + options.thumbnail + "');background-size:cover;background-position:center center";
}
html = '<span style="position:relative;background:' + background + ';width:' + width + 'px;height:' + height + 'px" class="thumb">';
html += '<img style="width:24px;height:24px;position:absolute;left:50%;top:50%;border:0;margin:-12px 0 0 -12px;padding:0" src="' + request_uri + 'common/img/play.png" alt="" />';
html += '</span>';
}

View file

@ -169,44 +169,40 @@ class integration_searchModel extends module
$args->isvalid = 'Y';
$args->direct_download = $direct_download=='Y'?'Y':'N';
$args->exclude_secret = 'Y';
// Get a list of documents
$oFileAdminModel = getAdminModel('file');
// Get a list of files
$oFileAdminModel = FileAdminModel::getInstance();
$output = $oFileAdminModel->getFileList($args);
if(!$output->toBool() || !$output->data) return $output;
$list = array();
foreach($output->data as $key => $val)
{
$obj = new stdClass;
$obj = new \Rhymix\Modules\Integration_Search\Models\FileSearchResult;
$obj->file_srl = $val->file_srl;
$obj->filename = $val->source_filename;
$obj->uploaded_filename = $val->uploaded_filename;
$obj->download_count = $val->download_count;
if(substr($val->download_url,0,2)=='./') $val->download_url = substr($val->download_url,2);
$obj->download_url = Context::getRequestUri().$val->download_url;
$obj->download_url = \RX_BASEURL . preg_replace('!^\.\/!', '', $val->download_url);
$obj->target_srl = $val->upload_target_srl;
$obj->file_size = $val->file_size;
// Images
if(preg_match('/\.(jpe?g|gif|png|webp)$/i', $val->source_filename))
if(preg_match('/\.(jpe?g|gif|png|bmp|webp)$/i', $val->source_filename))
{
$obj->type = 'image';
$thumbnail_path = sprintf('files/thumbnails/%s',getNumberingPath($val->file_srl, 3));
if(!is_dir($thumbnail_path)) FileHandler::makeDir($thumbnail_path);
$thumbnail_file = sprintf('%s%dx%d.%s.jpg', $thumbnail_path, 120, 120, 'crop');
$thumbnail_url = Context::getRequestUri().$thumbnail_file;
if(!file_exists($thumbnail_file)) FileHandler::createImageFile($val->uploaded_filename, $thumbnail_file, 120, 120, 'jpg', 'crop');
$obj->src = sprintf('<img src="%s" alt="%s" width="%d" height="%d" class="thumb" />', $thumbnail_url, htmlspecialchars($obj->filename, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), 120, 120);
// Videos
}
elseif(Rhymix\Framework\Filters\FilenameFilter::isDirectDownload($val->source_filename))
{
$obj->type = 'multimedia';
$obj->src = sprintf('<script>displayMultimedia("%s",80,80);</script>', $val->uploaded_filename);
// Others
if ($val->thumbnail_filename)
{
$obj->video_thumbnail_url = \RX_BASEURL . preg_replace('!^\.\/!', '', $val->thumbnail_filename);
}
}
else
{
$obj->type = 'binary';
$obj->src = '';
}
$list[] = $obj;

View file

@ -0,0 +1,120 @@
<?php
namespace Rhymix\Modules\Integration_Search\Models;
use Context;
use FileHandler;
#[\AllowDynamicProperties]
class FileSearchResult
{
/**
* Properties of the file.
*/
public $file_srl;
public $file_size;
public $filename;
public $uploaded_filename;
public $download_count;
public $download_url;
public $video_thumbnail_url;
public $target_srl;
public $type;
/**
* Properties of the upload target.
*/
public $url;
public $regdate;
public $nick_name;
/**
* Get a thumbnail.
*
* @param int $width
* @param int $height
* @param string $type
* @return string
*/
public function getThumbnail(int $width = 120, int $height = 0, string $type = 'crop'): string
{
if ($this->type !== 'image')
{
return '';
}
$thumbnail_path = sprintf('files/thumbnails/%s', getNumberingPath($this->file_srl, 3));
if(!is_dir($thumbnail_path))
{
FileHandler::makeDir($thumbnail_path);
}
$thumbnail_file = sprintf('%s%dx%d.%s.jpg', $thumbnail_path, $width, $height ?: $width, $type);
$thumbnail_url = \RX_BASEURL . $thumbnail_file;
if (!file_exists($thumbnail_file))
{
FileHandler::createImageFile($this->uploaded_filename, $thumbnail_file, $width, $height ?: $width, 'jpg', $type, 50);
}
return $thumbnail_url;
}
/**
* Display video.
*
* @param int $width
* @param int $height
* @return string
*/
public function displayVideo(int $width = 120, int $height = 0): string
{
if ($this->type !== 'multimedia')
{
return '';
}
$options = new \stdClass;
if ($this->video_thumbnail_url)
{
$options->thumbnail = $this->video_thumbnail_url;
}
return vsprintf('<script>displayMultimedia(%s, %d, %d, %s);</script>', [
json_encode(\RX_BASEURL . preg_replace('!^\.\/!', '', $this->uploaded_filename)),
$width,
$height ?: $width,
json_encode($options),
]);
}
/**
* Magic method to generate the 'src' attribute for backward compatibility.
*
* For images, it returns a 120x120 thumbnail.
* For videos, it returns a 80x80 preview.
* For other types of files, this method returns an empty string.
*/
public function __get(string $key)
{
if ($key === 'src')
{
if ($this->type === 'image')
{
return vsprintf('<img src="%s" alt="%s" width="120" height="120" class="thumb" />', [
$this->getThumbnail(120, 120),
escape($this->filename, false),
]);
}
elseif ($this->type === 'multimedia')
{
return $this->displayVideo(80, 80);
}
else
{
return '';
}
}
else
{
return null;
}
}
}