improve code

This commit is contained in:
Daniel Seifert 2024-12-04 14:25:51 +01:00
parent db7f3bc29c
commit 5cb8888d59
5 changed files with 105 additions and 59 deletions

View File

@ -121,7 +121,7 @@ class Loader
}
/**
* @return array
* @return string[]
*/
public function getScripts(): array
{
@ -129,8 +129,8 @@ class Loader
return [];
}
$sCopyLongDescFromTinyMCE = file_get_contents(__DIR__.'/../../../assets/out/scripts/copyLongDesc.js');
$sUrlConverter = file_get_contents(__DIR__.'/../../../assets/out/scripts/urlConverter.js');
$sCopyLongDescFromTinyMCE = file_get_contents(__DIR__.'/../../../assets/out/scripts/copyLongDesc.js') ?: '';
$sUrlConverter = file_get_contents(__DIR__.'/../../../assets/out/scripts/urlConverter.js') ?: '';
$sInit = str_replace(
"'CONFIG':'VALUES'",
$this->configuration->getConfig(),
@ -145,7 +145,7 @@ class Loader
}
/**
* @return array
* @return string[]
*/
public function getIncludes(): array
{

View File

@ -41,8 +41,9 @@ class FilemanagerUrl extends AbstractOption
public function get(): string
{
/** @var string $sFilemanagerKey */
$sFilemanagerKey = md5_file(rtrim(Registry::getConfig()->getConfigParam("sShopDir"), '/')."/config.inc.php");
$sFilemanagerKey = md5_file(
rtrim(Registry::getConfig()->getConfigParam("sShopDir"), '/')."/config.inc.php"
) ?: '';
Registry::get(UtilsServer::class)->setOxCookie("filemanagerkey", $sFilemanagerKey);
return str_replace(

View File

@ -40,6 +40,9 @@ class ViewConfig extends ViewConfig_parent
return $loader->getEditorCode();
}
/**
* @return string[]
*/
public function getTinyMceScripts(): array
{
$config = Registry::getConfig();
@ -49,6 +52,9 @@ class ViewConfig extends ViewConfig_parent
return $loader->getScripts();
}
/**
* @return string[]
*/
public function getTinyMceIncludes(): array
{
$config = Registry::getConfig();

View File

@ -459,38 +459,53 @@ class RoxyFile
}
class RoxyImage
{
public static function GetImage(string $path)
public static function GetImage(string $path): GdImage
{
$ext = RoxyFile::GetExtension(basename($path));
switch ($ext) {
case 'png':
return imagecreatefrompng($path);
$img = imagecreatefrompng($path);
break;
case 'gif':
return imagecreatefromgif($path);
$img = imagecreatefromgif($path);
break;
default:
return imagecreatefromjpeg($path);
$img = imagecreatefromjpeg($path);
}
if (!$img) {
throw new RuntimeException('Cannot open image');
}
return $img;
}
public static function OutputImage($img, string $type, ?string $destination = '', int $quality = 90)
public static function OutputImage(
GdImage|string $img,
string $type,
?string $destination = '',
int $quality = 90
): void
{
if (is_string($img)) {
$img = self::GetImage($img);
}
try {
if ( is_string( $img ) ) {
$img = self::GetImage( $img );
}
switch (strtolower($type)) {
case 'png':
imagepng($img, $destination);
break;
case 'gif':
imagegif($img, $destination);
break;
default:
imagejpeg($img, $destination, $quality);
}
switch ( strtolower( $type ) ) {
case 'png':
imagepng( $img, $destination );
break;
case 'gif':
imagegif( $img, $destination );
break;
default:
imagejpeg( $img, $destination, $quality );
}
} catch ( RuntimeException $e ) {}
}
public static function SetAlpha($img, string $path)
public static function SetAlpha(GdImage $img, string $path): GdImage
{
$ext = RoxyFile::GetExtension(basename($path));
if ($ext == "gif" || $ext == "png") {
@ -528,16 +543,27 @@ class RoxyImage
$newWidth = intval($newHeight * $r);
}
$thumbImg = imagecreatetruecolor((int) $newWidth, (int) $newHeight);
$img = self::GetImage($source);
try {
$thumbImg = imagecreatetruecolor( (int) $newWidth, (int) $newHeight );
$img = self::GetImage( $source );
$thumbImg = self::SetAlpha($thumbImg, $source);
$thumbImg = self::SetAlpha( $thumbImg, $source );
imagecopyresampled($thumbImg, $img, 0, 0, 0, 0, (int) $newWidth, (int) $newHeight, $w, $h);
imagecopyresampled( $thumbImg, $img, 0, 0, 0, 0, (int) $newWidth, (int) $newHeight, $w, $h );
self::OutputImage($thumbImg, RoxyFile::GetExtension(basename($source)), $destination, $quality);
self::OutputImage( $thumbImg, RoxyFile::GetExtension( basename( $source ) ), $destination, $quality );
} catch ( RuntimeException $e ) {}
}
/**
* @param string $source
* @param string|null $destination
* @param int<1, max> $width
* @param int<1, max> $height
* @param int $quality
*
* @return void
*/
public static function CropCenter(
string $source,
?string $destination,
@ -548,33 +574,51 @@ class RoxyImage
$tmp = (array) getimagesize($source);
$w = $tmp[0];
$h = $tmp[1];
if (($w <= $width) && (!$height || ($h <= $height))) {
self::OutputImage(self::GetImage($source), RoxyFile::GetExtension(basename($source)), $destination, $quality);
}
$ratio = $width / $height;
$top = $left = 0;
$cropWidth = floor($h * $ratio);
$cropHeight = floor($cropWidth / $ratio);
if ($cropWidth > $w) {
$cropWidth = $w;
$cropHeight = $w / $ratio;
}
if ($cropHeight > $h) {
$cropHeight = $h;
$cropWidth = $h * $ratio;
}
try {
if ( ( $w <= $width ) &&
( $h <= $height)
) {
self::OutputImage( self::GetImage( $source ), RoxyFile::GetExtension( basename( $source ) ), $destination, $quality );
}
$ratio = $width / $height;
$top = $left = 0;
if ($cropWidth < $w) {
$left = floor(($w - $cropWidth) / 2);
}
if ($cropHeight < $h) {
$top = floor(($h - $cropHeight) / 2);
}
$cropWidth = floor( $h * $ratio );
$cropHeight = floor( $cropWidth / $ratio );
if ( $cropWidth > $w ) {
$cropWidth = $w;
$cropHeight = $w / $ratio;
}
if ( $cropHeight > $h ) {
$cropHeight = $h;
$cropWidth = $h * $ratio;
}
self::Crop($source, $destination, (int) $left, (int) $top, $cropWidth, $cropHeight, $width, $height, $quality);
if ( $cropWidth < $w ) {
$left = floor( ( $w - $cropWidth ) / 2 );
}
if ( $cropHeight < $h ) {
$top = floor( ( $h - $cropHeight ) / 2 );
}
self::Crop( $source, $destination, (int) $left, (int) $top, $cropWidth, $cropHeight, $width, $height, $quality );
} catch (RuntimeException $e) {}
}
/**
* @param string $source
* @param string|null $destination
* @param int $x
* @param int $y
* @param int $cropWidth
* @param int $cropHeight
* @param int<1, max> $width
* @param int<1, max> $height
* @param int $quality
*
* @return void
*/
public static function Crop(
string $source,
?string $destination,

View File

@ -7,7 +7,7 @@ parameters:
paths:
- Application
- assets/out/fileman
level: 9
level: 6
phpVersion: 80000
ignoreErrors:
- '#Constant FILES_ROOT not found.#'
@ -19,8 +19,3 @@ parameters:
- '#Constant FILEPERMISSIONS not found.#'
- '#Constant MAX_IMAGE_HEIGHT not found.#'
- '#Constant MAX_IMAGE_WIDTH not found.#'
- '#Method RoxyImage\:\:OutputImage\(\) has no return type specified.#'
- '#Method RoxyImage\:\:OutputImage\(\) has parameter \$img with no type specified.#'
- '#Method RoxyImage\:\:SetAlpha\(\) has no return type specified.#'
- '#Method RoxyImage\:\:SetAlpha\(\) has parameter \$img with no type specified.#'
- '#Method RoxyImage\:\:GetImage\(\) has no return type specified.#'