From 36317896513ce229938d3cba96d32db19d2510d2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Mon, 25 Oct 2021 16:13:50 +0200 Subject: [PATCH] Fix resource usages in OC_Image MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This makes sure using resource or GdImage (PHP>=8) behaves the same. Signed-off-by: Côme Chilliet --- lib/private/Avatar/UserAvatar.php | 7 +++++-- lib/private/Preview/Bitmap.php | 2 +- lib/private/Preview/HEIC.php | 2 +- lib/private/Preview/SVG.php | 2 +- lib/private/legacy/OC_Image.php | 34 +++++++++++++++---------------- lib/public/IImage.php | 2 +- psalm.xml | 4 ++++ 7 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/private/Avatar/UserAvatar.php b/lib/private/Avatar/UserAvatar.php index 027c83172c2..b46e4816fa2 100644 --- a/lib/private/Avatar/UserAvatar.php +++ b/lib/private/Avatar/UserAvatar.php @@ -122,7 +122,7 @@ class UserAvatar extends Avatar { /** * Returns an image from several sources. * - * @param IImage|resource|string $data An image object, imagedata or path to the avatar + * @param IImage|resource|string|\GdImage $data An image object, imagedata or path to the avatar * @return IImage */ private function getAvatarImage($data) { @@ -131,7 +131,10 @@ class UserAvatar extends Avatar { } $img = new OC_Image(); - if (is_resource($data) && get_resource_type($data) === 'gd') { + if ( + (is_resource($data) && get_resource_type($data) === 'gd') || + (is_object($data) && get_class($data) === \GdImage::class) + ) { $img->setResource($data); } elseif (is_resource($data)) { $img->loadFromFileHandle($data); diff --git a/lib/private/Preview/Bitmap.php b/lib/private/Preview/Bitmap.php index 969fb948d94..57451da5725 100644 --- a/lib/private/Preview/Bitmap.php +++ b/lib/private/Preview/Bitmap.php @@ -60,7 +60,7 @@ abstract class Bitmap extends ProviderV2 { //new bitmap image object $image = new \OC_Image(); - $image->loadFromData($bp); + $image->loadFromData((string) $bp); //check if image object is valid return $image->valid() ? $image : null; } diff --git a/lib/private/Preview/HEIC.php b/lib/private/Preview/HEIC.php index f2d43564c99..6601de238a9 100644 --- a/lib/private/Preview/HEIC.php +++ b/lib/private/Preview/HEIC.php @@ -81,7 +81,7 @@ class HEIC extends ProviderV2 { //new bitmap image object $image = new \OC_Image(); - $image->loadFromData($bp); + $image->loadFromData((string) $bp); //check if image object is valid return $image->valid() ? $image : null; } diff --git a/lib/private/Preview/SVG.php b/lib/private/Preview/SVG.php index cee0f341716..690a6b50ffe 100644 --- a/lib/private/Preview/SVG.php +++ b/lib/private/Preview/SVG.php @@ -69,7 +69,7 @@ class SVG extends ProviderV2 { //new image object $image = new \OC_Image(); - $image->loadFromData($svg); + $image->loadFromData((string) $svg); //check if image object is valid if ($image->valid()) { $image->scaleDownToFit($maxX, $maxY); diff --git a/lib/private/legacy/OC_Image.php b/lib/private/legacy/OC_Image.php index 1e00b606ea0..c30ceae9916 100644 --- a/lib/private/legacy/OC_Image.php +++ b/lib/private/legacy/OC_Image.php @@ -101,7 +101,7 @@ class OC_Image implements \OCP\IImage { if (is_resource($this->resource)) { return true; } - if (is_object($this->resource) && get_class($this->resource) === 'GdImage') { + if (is_object($this->resource) && get_class($this->resource) === \GdImage::class) { return true; } @@ -308,7 +308,7 @@ class OC_Image implements \OCP\IImage { } /** - * @param resource Returns the image resource in any. + * @param resource|\GdImage $resource * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd" */ public function setResource($resource) { @@ -318,7 +318,7 @@ class OC_Image implements \OCP\IImage { return; } // PHP 8 has real objects for GD stuff - if (is_object($resource) && get_class($resource) === 'GdImage') { + if (is_object($resource) && get_class($resource) === \GdImage::class) { $this->resource = $resource; return; } @@ -326,7 +326,7 @@ class OC_Image implements \OCP\IImage { } /** - * @return resource Returns the image resource in any. + * @return resource|\GdImage Returns the image resource in any. */ public function resource() { return $this->resource; @@ -536,7 +536,7 @@ class OC_Image implements \OCP\IImage { * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again. * * @param resource $handle - * @return resource|false An image resource or false on error + * @return resource|\GdImage|false An image resource or false on error */ public function loadFromFileHandle($handle) { $contents = stream_get_contents($handle); @@ -550,7 +550,7 @@ class OC_Image implements \OCP\IImage { * Loads an image from a local file. * * @param bool|string $imagePath The path to a local file. - * @return bool|resource An image resource or false on error + * @return bool|resource|\GdImage An image resource or false on error */ public function loadFromFile($imagePath = false) { // exif_imagetype throws "read error!" if file is less than 12 byte @@ -666,17 +666,17 @@ class OC_Image implements \OCP\IImage { * Loads an image from a string of data. * * @param string $str A string of image data as read from a file. - * @return bool|resource An image resource or false on error + * @return bool|resource|\GdImage An image resource or false on error */ public function loadFromData($str) { - if (is_resource($str)) { + if (!is_string($str)) { return false; } $this->resource = @imagecreatefromstring($str); if ($this->fileInfo) { $this->mimeType = $this->fileInfo->buffer($str); } - if (is_resource($this->resource)) { + if ($this->valid()) { imagealphablending($this->resource, false); imagesavealpha($this->resource, true); } @@ -692,7 +692,7 @@ class OC_Image implements \OCP\IImage { * Loads an image from a base64 encoded string. * * @param string $str A string base64 encoded string of image data. - * @return bool|resource An image resource or false on error + * @return bool|resource|\GdImage An image resource or false on error */ public function loadFromBase64($str) { if (!is_string($str)) { @@ -722,7 +722,7 @@ class OC_Image implements \OCP\IImage { * @param string $fileName

* Path to the BMP image. *

- * @return bool|resource an image resource identifier on success, FALSE on errors. + * @return bool|resource|\GdImage an image resource identifier on success, FALSE on errors. */ private function imagecreatefrombmp($fileName) { if (!($fh = fopen($fileName, 'rb'))) { @@ -878,12 +878,12 @@ class OC_Image implements \OCP\IImage { $result = $this->resizeNew($maxSize); imagedestroy($this->resource); $this->resource = $result; - return is_resource($result); + return $this->valid(); } /** * @param $maxSize - * @return resource | bool + * @return resource|bool|\GdImage */ private function resizeNew($maxSize) { if (!$this->valid()) { @@ -914,14 +914,14 @@ class OC_Image implements \OCP\IImage { $result = $this->preciseResizeNew($width, $height); imagedestroy($this->resource); $this->resource = $result; - return is_resource($result); + return $this->valid(); } /** * @param int $width * @param int $height - * @return resource | bool + * @return resource|bool|\GdImage */ public function preciseResizeNew(int $width, int $height) { if (!$this->valid()) { @@ -1023,7 +1023,7 @@ class OC_Image implements \OCP\IImage { $result = $this->cropNew($x, $y, $w, $h); imagedestroy($this->resource); $this->resource = $result; - return is_resource($result); + return $this->valid(); } /** @@ -1181,7 +1181,7 @@ if (!function_exists('imagebmp')) { * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm * @author mgutt * @version 1.00 - * @param resource $im + * @param resource|\GdImage $im * @param string $fileName [optional]

The path to save the file to.

* @param int $bit [optional]

Bit depth, (default is 24).

* @param int $compression [optional] diff --git a/lib/public/IImage.php b/lib/public/IImage.php index 459553de799..9d2b31e0e28 100644 --- a/lib/public/IImage.php +++ b/lib/public/IImage.php @@ -98,7 +98,7 @@ interface IImage { public function save($filePath = null, $mimeType = null); /** - * @return resource Returns the image resource in any. + * @return resource|\GdImage Returns the image resource in any. * @since 8.1.0 */ public function resource(); diff --git a/psalm.xml b/psalm.xml index a075ceb24a7..4f75af61dde 100644 --- a/psalm.xml +++ b/psalm.xml @@ -81,6 +81,8 @@ + + @@ -124,6 +126,8 @@ + + -- 2.39.5