From 5cb8888d59bfedabe9aa194c94c17abf150688e0 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Wed, 4 Dec 2024 14:25:51 +0100 Subject: [PATCH] improve code --- Application/Core/TinyMCE/Loader.php | 8 +- .../Core/TinyMCE/Options/FilemanagerUrl.php | 5 +- Application/Core/ViewConfig.php | 6 + assets/out/fileman/php/functions.inc.php | 136 ++++++++++++------ phpstan.neon | 9 +- 5 files changed, 105 insertions(+), 59 deletions(-) diff --git a/Application/Core/TinyMCE/Loader.php b/Application/Core/TinyMCE/Loader.php index c6d9273..224f287 100644 --- a/Application/Core/TinyMCE/Loader.php +++ b/Application/Core/TinyMCE/Loader.php @@ -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 { diff --git a/Application/Core/TinyMCE/Options/FilemanagerUrl.php b/Application/Core/TinyMCE/Options/FilemanagerUrl.php index b43ca1f..48876df 100644 --- a/Application/Core/TinyMCE/Options/FilemanagerUrl.php +++ b/Application/Core/TinyMCE/Options/FilemanagerUrl.php @@ -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( diff --git a/Application/Core/ViewConfig.php b/Application/Core/ViewConfig.php index 386ea9a..9b06651 100755 --- a/Application/Core/ViewConfig.php +++ b/Application/Core/ViewConfig.php @@ -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(); diff --git a/assets/out/fileman/php/functions.inc.php b/assets/out/fileman/php/functions.inc.php index 4c7bcac..8c0c7a9 100755 --- a/assets/out/fileman/php/functions.inc.php +++ b/assets/out/fileman/php/functions.inc.php @@ -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, diff --git a/phpstan.neon b/phpstan.neon index eb66e2a..97c7251 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,7 +7,7 @@ parameters: paths: - Application - assets/out/fileman - level: 9 + level: 6 phpVersion: 80000 ignoreErrors: - '#Constant FILES_ROOT not found.#' @@ -18,9 +18,4 @@ parameters: - '#Constant DIRPERMISSIONS not found.#' - '#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.#' \ No newline at end of file + - '#Constant MAX_IMAGE_WIDTH not found.#' \ No newline at end of file