Refactored thumbnail creation 🌄 (#1249)

* Refactored thumbnail creation

* Updated

* Updated

* Updated

* Updated

* Updated

* Update functions_thumbs.php

* Update functions_thumbs.php

* Update displaying.php

* Update mysql.sql

* Update mysql.sql

* Update CHANGELOG.md
This commit is contained in:
Roman Kelesidis 2023-12-23 00:42:59 +07:00 committed by GitHub
commit 65c7903b21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 160 additions and 477 deletions

View file

@ -1,248 +0,0 @@
<?php
/**
* TorrentPier Bull-powered BitTorrent tracker engine
*
* @copyright Copyright (c) 2005-2024 TorrentPier (https://torrentpier.com)
* @link https://github.com/torrentpier/torrentpier for the canonical source repository
* @license https://github.com/torrentpier/torrentpier/blob/master/LICENSE MIT License
*/
/**
* All Attachment Functions needed to determine Special Files/Dimensions
*/
/**
* Read Long Int (4 Bytes) from File
*/
function read_longint($fp)
{
$data = fread($fp, 4);
$value = ord($data[0]) + (ord($data[1]) << 8) + (ord($data[2]) << 16) + (ord($data[3]) << 24);
if ($value >= 4294967294) {
$value -= 4294967296;
}
return $value;
}
/**
* Read Word (2 Bytes) from File - Note: It's an Intel Word
*/
function read_word($fp)
{
$data = fread($fp, 2);
return ord($data[1]) * 256 + ord($data[0]);
}
/**
* Read Byte
*/
function read_byte($fp)
{
$data = fread($fp, 1);
return ord($data);
}
/**
* Get Image Dimensions
*/
function image_getdimension($file)
{
$xmax = null;
$xmin = null;
$ymax = null;
$ymin = null;
$size = @getimagesize($file);
if ($size[0] != 0 || $size[1] != 0) {
return $size;
}
// Try to get the Dimension manually, depending on the mimetype
$fp = @fopen($file, 'rb');
if (!$fp) {
return $size;
}
$error = false;
// BMP - IMAGE
$tmp_str = fread($fp, 2);
if ($tmp_str == 'BM') {
$length = read_longint($fp);
if ($length <= 6) {
$error = true;
}
if (!$error) {
$i = read_longint($fp);
if ($i != 0) {
$error = true;
}
}
if (!$error) {
$i = read_longint($fp);
if ($i != 0x3E && $i != 0x76 && $i != 0x436 && $i != 0x36) {
$error = true;
}
}
if (!$error) {
$tmp_str = fread($fp, 4);
$width = read_longint($fp);
$height = read_longint($fp);
if ($width > 3000 || $height > 3000) {
$error = true;
}
}
} else {
$error = true;
}
if (!$error) {
fclose($fp);
return [
$width,
$height,
6
];
}
$error = false;
fclose($fp);
// GIF - IMAGE
$fp = @fopen($file, 'rb');
$tmp_str = fread($fp, 3);
if ($tmp_str == 'GIF') {
$width = read_word($fp);
$height = read_word($fp);
$info_byte = fread($fp, 1);
$info_byte = ord($info_byte);
if (($info_byte & 0x80) != 0x80 && ($info_byte & 0x80) != 0) {
$error = true;
}
if (!$error) {
if (($info_byte & 8) != 0) {
$error = true;
}
}
} else {
$error = true;
}
if (!$error) {
fclose($fp);
return [
$width,
$height,
1
];
}
$error = false;
fclose($fp);
// JPG - IMAGE
$fp = @fopen($file, 'rb');
$w1 = read_word($fp);
if ((int)$w1 < 16) {
$error = true;
}
if (!$error) {
$tmp_str = fread($fp, 4);
if ($tmp_str == 'JFIF') {
$o_byte = fread($fp, 1);
if ((int)$o_byte != 0) {
$error = true;
}
if (!$error) {
$b = read_byte($fp);
if ($b != 0 && $b != 1 && $b != 2) {
$error = true;
}
}
if (!$error) {
$width = read_word($fp);
$height = read_word($fp);
if ($width <= 0 || $height <= 0) {
$error = true;
}
}
}
} else {
$error = true;
}
if (!$error) {
fclose($fp);
return [
$width,
$height,
2
];
}
$error = false;
fclose($fp);
// PCX - IMAGE
$fp = @fopen($file, 'rb');
$tmp_str = fread($fp, 3);
if ((ord($tmp_str[0]) == 10) && (ord($tmp_str[1]) == 0 || ord($tmp_str[1]) == 2 || ord($tmp_str[1]) == 3 || ord($tmp_str[1]) == 4 || ord($tmp_str[1]) == 5) && (ord($tmp_str[2]) == 1)) {
$b = fread($fp, 1);
if (ord($b) != 1 && ord($b) != 2 && ord($b) != 4 && ord($b) != 8 && ord($b) != 24) {
$error = true;
}
if (!$error) {
$xmin = read_word($fp);
$ymin = read_word($fp);
$xmax = read_word($fp);
$ymax = read_word($fp);
$b = fread($fp, 1);
if ($b != 0) {
$error = true;
}
}
if (!$error) {
$width = $xmax - $xmin + 1;
$height = $ymax - $ymin + 1;
}
} else {
$error = true;
}
if (!$error) {
fclose($fp);
return [
$width,
$height,
7
];
}
fclose($fp);
return $size;
}

View file

@ -11,177 +11,46 @@ if (!defined('BB_ROOT')) {
die(basename(__FILE__));
}
$imagick = '';
/**
* Calculate the needed size for Thumbnail
*/
function get_img_size_format($width, $height)
{
// Maximum Width the Image can take
$max_width = 400;
if ($width > $height) {
return [
round($width * ($max_width / $width)),
round($height * ($max_width / $width))
];
}
return [
round($width * ($max_width / $height)),
round($height * ($max_width / $height))
];
}
/**
* Check if imagick is present
*/
function is_imagick()
{
global $imagick, $attach_config;
if ($attach_config['img_imagick'] != '') {
$imagick = $attach_config['img_imagick'];
return true;
}
return false;
}
/**
* Get supported image types
*/
function get_supported_image_types($type)
{
// Check GD extension installed
if (!extension_loaded('gd')) {
return ['gd' => false];
}
$format = imagetypes();
$new_type = 0;
switch ($type) {
case IMAGETYPE_GIF:
$new_type = ($format & IMG_GIF) ? IMG_GIF : 0;
break;
case IMAGETYPE_JPEG:
$new_type = ($format & IMG_JPG) ? IMG_JPG : 0;
break;
case IMAGETYPE_PNG:
$new_type = ($format & IMG_PNG) ? IMG_PNG : 0;
break;
case IMAGETYPE_BMP:
$new_type = ($format & IMG_BMP) ? IMG_BMP : 0;
break;
case IMAGETYPE_WEBP:
$new_type = ($format & IMG_WEBP) ? IMG_WEBP : 0;
break;
}
return [
'gd' => (bool)$new_type,
'format' => $new_type,
'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1
];
}
/**
* Create thumbnail
*
* @param string $source
* @param string $newFile
* @param string $mimeType
* @return bool
* @throws Exception
*/
function create_thumbnail($source, $new_file, $mimetype)
function createThumbnail(string $source, string $newFile, string $mimeType): bool
{
global $attach_config, $imagick;
$image = null;
global $attach_config;
// Get the file information
$source = amod_realpath($source);
$min_filesize = (int)$attach_config['img_min_thumb_filesize'];
$img_filesize = (@file_exists($source)) ? @filesize($source) : false;
$img_filesize = file_exists($source) ? filesize($source) : false;
// Checks the max allowed filesize
if (!$img_filesize || $img_filesize <= $min_filesize) {
return false;
}
[$width, $height, $type,] = getimagesize($source);
// Making the thumbnail image
try {
$image = new \claviska\SimpleImage();
$image
->fromFile($source)
->autoOrient()
->resize(150)
->toFile($newFile, $mimeType, ['quality' => 85]);
} catch (Exception $e) {
// Handle errors
throw new Exception($e->getMessage());
}
if (!$width || !$height) {
// Check the thumbnail existence after creating
if (!file_exists($newFile)) {
return false;
}
[$new_width, $new_height] = get_img_size_format($width, $height);
$used_imagick = false;
if (is_imagick()) {
passthru($imagick . ' -quality 85 -antialias -sample ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $source) . '" +profile "*" "' . str_replace('\\', '/', $new_file) . '"');
if (@file_exists($new_file)) {
$used_imagick = true;
}
}
if (!$used_imagick) {
$type = get_supported_image_types($type);
if ($type['gd']) {
switch ($type['format']) {
case IMG_GIF:
$image = imagecreatefromgif($source);
break;
case IMG_JPG:
$image = imagecreatefromjpeg($source);
break;
case IMG_PNG:
$image = imagecreatefrompng($source);
break;
case IMG_BMP:
$image = imagecreatefrombmp($source);
break;
case IMG_WEBP:
$image = imagecreatefromwebp($source);
break;
default:
throw new Exception('Unknown file format: ' . $type['format']);
}
if ($type['version'] == 1 || !$attach_config['use_gd2']) {
$new_image = imagecreate($new_width, $new_height);
imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
} else {
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
}
switch ($type['format']) {
case IMG_GIF:
imagegif($new_image, $new_file);
break;
case IMG_JPG:
imagejpeg($new_image, $new_file, 90);
break;
case IMG_PNG:
imagepng($new_image, $new_file);
break;
case IMG_BMP:
imagebmp($new_image, $new_file);
break;
case IMG_WEBP:
imagewebp($new_image, $new_file);
break;
default:
throw new Exception('Unknown file format: ' . $type['format']);
}
imagedestroy($new_image);
}
}
if (!@file_exists($new_file)) {
return false;
}
@chmod($new_file, 0664);
return true;
}