From b5371dd984a126cb338265777ff92fd9c1540cc4 Mon Sep 17 00:00:00 2001 From: Daniel Butum Date: Wed, 28 Dec 2016 21:14:11 +0200 Subject: [PATCH] Move Image related functions from Util to SImage --- composer.lock | 10 +- image.php | 2 +- include/AddonViewer.class.php | 18 +-- include/File.class.php | 1 - include/FileSystem.class.php | 7 ++ include/SImage.class.php | 200 +++++++++++++++++++++++++++++++++- include/Util.class.php | 184 ------------------------------- 7 files changed, 218 insertions(+), 204 deletions(-) create mode 100644 include/FileSystem.class.php diff --git a/composer.lock b/composer.lock index 805c09a9..c8760f94 100644 --- a/composer.lock +++ b/composer.lock @@ -469,16 +469,16 @@ }, { "name": "phpmailer/phpmailer", - "version": "v5.2.19", + "version": "v5.2.20", "source": { "type": "git", "url": "https://github.com/PHPMailer/PHPMailer.git", - "reference": "9e4b8fb3deb7d9cfa515c04cec41f71bc37ce9a9" + "reference": "efde5edb3da8e1d257e030e3c2d922c4de6e5d09" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/9e4b8fb3deb7d9cfa515c04cec41f71bc37ce9a9", - "reference": "9e4b8fb3deb7d9cfa515c04cec41f71bc37ce9a9", + "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/efde5edb3da8e1d257e030e3c2d922c4de6e5d09", + "reference": "efde5edb3da8e1d257e030e3c2d922c4de6e5d09", "shasum": "" }, "require": { @@ -525,7 +525,7 @@ } ], "description": "PHPMailer is a full-featured email creation and transfer class for PHP", - "time": "2016-12-26T10:09:10+00:00" + "time": "2016-12-28T13:36:42+00:00" }, { "name": "smarty-gettext/smarty-gettext", diff --git a/image.php b/image.php index 5b669432..b4c70a55 100644 --- a/image.php +++ b/image.php @@ -21,4 +21,4 @@ $size = empty($_GET['size']) ? null : $_GET['size']; $file = empty($_GET['pic']) ? null : $_GET['pic']; -Util::resizeImage($file, (int)$size); +SImage::resizeImage($file, (int)$size); diff --git a/include/AddonViewer.class.php b/include/AddonViewer.class.php index 7db52884..089324ca 100755 --- a/include/AddonViewer.class.php +++ b/include/AddonViewer.class.php @@ -275,20 +275,20 @@ public function fillTemplate($template) { $config = [ "status" => [ - "alpha_img" => Util::getImageLabel(_h('Alpha')), - "beta_img" => Util::getImageLabel(_h('Beta')), - "rc_img" => Util::getImageLabel(_h('Release-Candidate')), - "latest_img" => Util::getImageLabel(_h('Latest')), - "invalid_img" => Util::getImageLabel(_h('Invalid Textures')) + "alpha_img" => SImage::getImageLabel(_h('Alpha')), + "beta_img" => SImage::getImageLabel(_h('Beta')), + "rc_img" => SImage::getImageLabel(_h('Release-Candidate')), + "latest_img" => SImage::getImageLabel(_h('Latest')), + "invalid_img" => SImage::getImageLabel(_h('Invalid Textures')) ] ]; if ($this->user_has_permission) { - $config["status"]["approve_img"] = Util::getImageLabel(_h('Approved')); - $config["status"]["invisible_img"] = Util::getImageLabel(_h('Invisible')); - $config["status"]["dfsg_img"] = Util::getImageLabel(_h('DFSG Compliant')); - $config["status"]["featured_img"] = Util::getImageLabel(_h('Featured')); + $config["status"]["approve_img"] = SImage::getImageLabel(_h('Approved')); + $config["status"]["invisible_img"] = SImage::getImageLabel(_h('Invisible')); + $config["status"]["dfsg_img"] = SImage::getImageLabel(_h('DFSG Compliant')); + $config["status"]["featured_img"] = SImage::getImageLabel(_h('Featured')); } $tpl['config'] = $config; diff --git a/include/File.class.php b/include/File.class.php index 6e119b94..3aeb83a3 100644 --- a/include/File.class.php +++ b/include/File.class.php @@ -696,7 +696,6 @@ public static function ls($path, $has_dots = false) return $has_dots ? $files : array_diff($files, ['..', '.']); } - /** * Tells whether the path is a directory * diff --git a/include/FileSystem.class.php b/include/FileSystem.class.php new file mode 100644 index 00000000..5a7adaec --- /dev/null +++ b/include/FileSystem.class.php @@ -0,0 +1,7 @@ +path); - $this->format = IMAGETYPE_PNG; + $this->format = static::TYPE_PNG; break; case IMAGETYPE_JPEG: $source = imagecreatefromjpeg($this->path); - $this->format = IMAGETYPE_JPEG; + $this->format = static::TYPE_JPEG; break; default: @@ -132,11 +132,11 @@ public function setPath($path) */ public function save($file_path) { - if ($this->format === IMAGETYPE_PNG) + if ($this->format === static::TYPE_PNG) { imagepng($this->imageData, $file_path); } - elseif ($this->format === IMAGETYPE_JPEG) + elseif ($this->format === static::TYPE_JPEG) { imagejpeg($this->imageData, $file_path); } @@ -269,6 +269,198 @@ public function scale($max_x, $max_y, $min_x = 1, $min_y = 1) $this->imageData = $destination; } + /** + * Resize an image, and send the new resized image to the user with http headers + * + * @param string $file_path + * @param int $size_type the image size type, see SImage::SIZE_ + * + * @return null + */ + public static function resizeImage($file_path, $size_type = null) + { + // file is invalid + Debug::addMessage('Called Util::resizeImage'); + + if (!$file_path) + { + header('HTTP/1.1 404 Not Found'); + Debug::addMessage('Called Util::resizeImage with an empty file_path'); + + return; + } + + $size = SImage::sizeToInt($size_type); + $cache_name = Cache::getCachePrefix($size_type) . basename($file_path); + $local_path = UP_PATH . $file_path; // all images should be in our upload directory + + // Check if image exists in the database + try + { + $file = File::getFromPath($file_path); + } + catch (FileException $e) + { + Debug::addMessage(sprintf("%s does not exist in the database", $file_path)); + header('HTTP/1.1 404 Not Found'); + + return; + } + + // file does not exist on disk + if (!file_exists($local_path)) + { + Debug::addMessage(sprintf("%s does not exist on the disk", $file_path)); + header('HTTP/1.1 404 Not Found'); + + return; + } + + // Check if a cached version is available + if (Cache::fileExists($cache_name)) + { + header('Cached-Image: true'); + header('Location: ' . CACHE_LOCATION . $cache_name); + + return; + } + + // Start processing the original file + $image_info = getimagesize($local_path); + switch ($image_info[2]) + { + case IMAGETYPE_PNG: + $source = imagecreatefrompng($local_path); + $format = 'png'; + break; + + case IMAGETYPE_JPEG: + $source = imagecreatefromjpeg($local_path); + $format = 'jpg'; + break; + + default: + $source = imagecreatefrompng(IMG_LOCATION . 'notfound.png'); + $format = 'png'; + break; + } + + // Get length and width of original image + $width_source = imagesx($source); + $height_source = imagesy($source); + if ($width_source > $height_source) + { + $width_destination = $size; + $height_destination = $size * $height_source / $width_source; + } + else // $width_source <= $height_source + { + $height_destination = $size; + $width_destination = $size * $width_source / $height_source; + } + + // Create new canvas + $destination = imagecreatetruecolor($width_destination, $height_destination); + + // Preserve transparency + imagealphablending($destination, false); + imagesavealpha($destination, true); + $transparent_bg = imagecolorallocatealpha($destination, 255, 255, 255, 127); + imagefilledrectangle($destination, 0, 0, $width_destination, $height_destination, $transparent_bg); + + // Resize image + imagecopyresampled( + $destination, + $source, + 0, + 0, + 0, + 0, + $width_destination, + $height_destination, + $width_source, + $height_source + ); + + // Display image and cache the result + header('Cached-Image: false'); + if ($format === 'png') + { + header('Content-Type: image/png'); + imagepng($destination, CACHE_PATH . $cache_name, 9); + imagepng($destination, null, 9); + } + else + { + header("Content-Type: image/jpeg"); + imagejpeg($destination, CACHE_PATH . $cache_name, 90); + imagejpeg($destination, null, 90); + } + + // Create a record of the cached file + Cache::createFile( + $cache_name, + $file->getAddonId(), + sprintf('w=%d,h=%d', $width_destination, $height_destination) + ); + } + + public static function createImageLabel($text) + { + $write_dir = CACHE_PATH; + + $text_no_accent = preg_replace('/\W/s', '_', $text); // remove some accents + $image_name = 'im_' . $text_no_accent . '.png'; + + if (!file_exists($write_dir . $image_name)) + { + $text_size = 11; + $text_angle = 90; + $font = FONTS_PATH . 'DejaVuSans.ttf'; + $bbox = imagettfbbox($text_size, $text_angle, $font, $text); + + $min_x = min([$bbox[0], $bbox[2], $bbox[4], $bbox[6]]); + $max_x = max([$bbox[0], $bbox[2], $bbox[4], $bbox[6]]); + $min_y = min([$bbox[1], $bbox[3], $bbox[5], $bbox[7]]); + $max_y = max([$bbox[1], $bbox[3], $bbox[5], $bbox[7]]); + + $width = $max_x - $min_x + 2; + $height = $max_y - $min_y + 2; + + $image = imagecreatetruecolor($width, $height); + + // set color and transparency + $bg_color = imagecolorallocate($image, 0, 0, 0); + imagecolortransparent($image, $bg_color); + $text_color = imagecolorallocate($image, 2, 2, 2); + + // set text + imagettftext($image, $text_size, $text_angle, $width, $height, $text_color, $font, $text); + + // create the image in the write dir + imagepng($image, $write_dir . $image_name); + imagedestroy($image); + } + + return [ + 'dir' => CACHE_LOCATION, + 'file' => $image_name, + ]; + } + + /** + * Get the image label of some text, if the image label for that text does not exist, then create it + * + * @param string $text the label text + * + * @return string the img tag that points to our image text + */ + public static function getImageLabel($text) + { + $path = static::createImageLabel($text); + return sprintf('%s', $path['dir'] . $path['file'], $text); + } + /** * @param int $size see SIZE_* * diff --git a/include/Util.class.php b/include/Util.class.php index 424c9070..0dece9e3 100644 --- a/include/Util.class.php +++ b/include/Util.class.php @@ -711,190 +711,6 @@ public static function getRandomString( return $string; } - /** - * Resize an image, and send the new resized image to the user with http headers - * - * @param string $file_path - * @param int $size_type the image size type, see SImage::SIZE_ - * - * @return null - */ - public static function resizeImage($file_path, $size_type = null) - { - // file is invalid - Debug::addMessage('Called Util::resizeImage'); - - if (!$file_path) - { - header('HTTP/1.1 404 Not Found'); - Debug::addMessage('Called Util::resizeImage with an empty file_path'); - - return; - } - - $size = SImage::sizeToInt($size_type); - $cache_name = Cache::getCachePrefix($size_type) . basename($file_path); - $local_path = UP_PATH . $file_path; // all images should be in our upload directory - - // Check if image exists in the database - try - { - $file = File::getFromPath($file_path); - } - catch (FileException $e) - { - Debug::addMessage(sprintf("%s does not exist in the database", $file_path)); - header('HTTP/1.1 404 Not Found'); - - return; - } - - // file does not exist on disk - if (!file_exists($local_path)) - { - Debug::addMessage(sprintf("%s does not exist on the disk", $file_path)); - header('HTTP/1.1 404 Not Found'); - - return; - } - - // Check if a cached version is available - if (Cache::fileExists($cache_name)) - { - header('Cached-Image: true'); - header('Location: ' . CACHE_LOCATION . $cache_name); - - return; - } - - // Start processing the original file - $image_info = getimagesize($local_path); - switch ($image_info[2]) - { - case IMAGETYPE_PNG: - $source = imagecreatefrompng($local_path); - $format = 'png'; - break; - - case IMAGETYPE_JPEG: - $source = imagecreatefromjpeg($local_path); - $format = 'jpg'; - break; - - default: - $source = imagecreatefrompng(IMG_LOCATION . 'notfound.png'); - $format = 'png'; - break; - } - - // Get length and width of original image - $width_source = imagesx($source); - $height_source = imagesy($source); - if ($width_source > $height_source) - { - $width_destination = $size; - $height_destination = $size * $height_source / $width_source; - } - else // $width_source <= $height_source - { - $height_destination = $size; - $width_destination = $size * $width_source / $height_source; - } - - // Create new canvas - $destination = imagecreatetruecolor($width_destination, $height_destination); - - // Preserve transparency - imagealphablending($destination, false); - imagesavealpha($destination, true); - $transparent_bg = imagecolorallocatealpha($destination, 255, 255, 255, 127); - imagefilledrectangle($destination, 0, 0, $width_destination, $height_destination, $transparent_bg); - - // Resize image - imagecopyresampled( - $destination, - $source, - 0, - 0, - 0, - 0, - $width_destination, - $height_destination, - $width_source, - $height_source - ); - - // Display image and cache the result - header('Cached-Image: false'); - if ($format === 'png') - { - header('Content-Type: image/png'); - imagepng($destination, CACHE_PATH . $cache_name, 9); - imagepng($destination, null, 9); - } - else - { - header("Content-Type: image/jpeg"); - imagejpeg($destination, CACHE_PATH . $cache_name, 90); - imagejpeg($destination, null, 90); - } - - // Create a record of the cached file - Cache::createFile( - $cache_name, - $file->getAddonId(), - sprintf('w=%d,h=%d', $width_destination, $height_destination) - ); - } - - /** - * Get the image label of some text, if the image label for that text does not exist, then create it - * - * @param string $text the label text - * - * @return string the img tag that points to our image text - */ - public static function getImageLabel($text) - { - $write_dir = CACHE_PATH; - $read_dir = CACHE_LOCATION; - - $text_no_accent = preg_replace('/\W/s', '_', $text); // remove some accents - $image_name = 'im_' . $text_no_accent . '.png'; - - if (!file_exists($write_dir . $image_name)) - { - $text_size = 11; - $text_angle = 90; - $font = FONTS_PATH . 'DejaVuSans.ttf'; - $bbox = imagettfbbox($text_size, $text_angle, $font, $text); - - $min_x = min([$bbox[0], $bbox[2], $bbox[4], $bbox[6]]); - $max_x = max([$bbox[0], $bbox[2], $bbox[4], $bbox[6]]); - $min_y = min([$bbox[1], $bbox[3], $bbox[5], $bbox[7]]); - $max_y = max([$bbox[1], $bbox[3], $bbox[5], $bbox[7]]); - - $width = $max_x - $min_x + 2; - $height = $max_y - $min_y + 2; - - $image = imagecreatetruecolor($width, $height); - - // set color and transparency - $bg_color = imagecolorallocate($image, 0, 0, 0); - imagecolortransparent($image, $bg_color); - $text_color = imagecolorallocate($image, 2, 2, 2); - - // set text - imagettftext($image, $text_size, $text_angle, $width, $height, $text_color, $font, $text); - - // create the image in the write dir - imagepng($image, $write_dir . $image_name); - imagedestroy($image); - } - - return '' . $text . ''; - } - /** * Get the stk version formatted *