Browse Source

Fix resource usages in OC_Image

This makes sure using resource or GdImage (PHP>=8) behaves the same.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
tags/v23.0.0beta3
Côme Chilliet 2 years ago
parent
commit
3631789651
No account linked to committer's email address

+ 5
- 2
lib/private/Avatar/UserAvatar.php View File

/** /**
* Returns an image from several sources. * 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 * @return IImage
*/ */
private function getAvatarImage($data) { private function getAvatarImage($data) {
} }


$img = new OC_Image(); $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); $img->setResource($data);
} elseif (is_resource($data)) { } elseif (is_resource($data)) {
$img->loadFromFileHandle($data); $img->loadFromFileHandle($data);

+ 1
- 1
lib/private/Preview/Bitmap.php View File



//new bitmap image object //new bitmap image object
$image = new \OC_Image(); $image = new \OC_Image();
$image->loadFromData($bp);
$image->loadFromData((string) $bp);
//check if image object is valid //check if image object is valid
return $image->valid() ? $image : null; return $image->valid() ? $image : null;
} }

+ 1
- 1
lib/private/Preview/HEIC.php View File



//new bitmap image object //new bitmap image object
$image = new \OC_Image(); $image = new \OC_Image();
$image->loadFromData($bp);
$image->loadFromData((string) $bp);
//check if image object is valid //check if image object is valid
return $image->valid() ? $image : null; return $image->valid() ? $image : null;
} }

+ 1
- 1
lib/private/Preview/SVG.php View File



//new image object //new image object
$image = new \OC_Image(); $image = new \OC_Image();
$image->loadFromData($svg);
$image->loadFromData((string) $svg);
//check if image object is valid //check if image object is valid
if ($image->valid()) { if ($image->valid()) {
$image->scaleDownToFit($maxX, $maxY); $image->scaleDownToFit($maxX, $maxY);

+ 17
- 17
lib/private/legacy/OC_Image.php View File

if (is_resource($this->resource)) { if (is_resource($this->resource)) {
return true; 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; return true;
} }


} }


/** /**
* @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" * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd"
*/ */
public function setResource($resource) { public function setResource($resource) {
return; return;
} }
// PHP 8 has real objects for GD stuff // 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; $this->resource = $resource;
return; return;
} }
} }


/** /**
* @return resource Returns the image resource in any.
* @return resource|\GdImage Returns the image resource in any.
*/ */
public function resource() { public function resource() {
return $this->resource; return $this->resource;
* It is the responsibility of the caller to position the pointer at the correct place and to close the handle again. * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again.
* *
* @param resource $handle * @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) { public function loadFromFileHandle($handle) {
$contents = stream_get_contents($handle); $contents = stream_get_contents($handle);
* Loads an image from a local file. * Loads an image from a local file.
* *
* @param bool|string $imagePath The path to 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) { public function loadFromFile($imagePath = false) {
// exif_imagetype throws "read error!" if file is less than 12 byte // exif_imagetype throws "read error!" if file is less than 12 byte
* Loads an image from a string of data. * Loads an image from a string of data.
* *
* @param string $str A string of image data as read from a file. * @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) { public function loadFromData($str) {
if (is_resource($str)) {
if (!is_string($str)) {
return false; return false;
} }
$this->resource = @imagecreatefromstring($str); $this->resource = @imagecreatefromstring($str);
if ($this->fileInfo) { if ($this->fileInfo) {
$this->mimeType = $this->fileInfo->buffer($str); $this->mimeType = $this->fileInfo->buffer($str);
} }
if (is_resource($this->resource)) {
if ($this->valid()) {
imagealphablending($this->resource, false); imagealphablending($this->resource, false);
imagesavealpha($this->resource, true); imagesavealpha($this->resource, true);
} }
* Loads an image from a base64 encoded string. * Loads an image from a base64 encoded string.
* *
* @param string $str A string base64 encoded string of image data. * @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) { public function loadFromBase64($str) {
if (!is_string($str)) { if (!is_string($str)) {
* @param string $fileName <p> * @param string $fileName <p>
* Path to the BMP image. * Path to the BMP image.
* </p> * </p>
* @return bool|resource an image resource identifier on success, <b>FALSE</b> on errors.
* @return bool|resource|\GdImage an image resource identifier on success, <b>FALSE</b> on errors.
*/ */
private function imagecreatefrombmp($fileName) { private function imagecreatefrombmp($fileName) {
if (!($fh = fopen($fileName, 'rb'))) { if (!($fh = fopen($fileName, 'rb'))) {
$result = $this->resizeNew($maxSize); $result = $this->resizeNew($maxSize);
imagedestroy($this->resource); imagedestroy($this->resource);
$this->resource = $result; $this->resource = $result;
return is_resource($result);
return $this->valid();
} }


/** /**
* @param $maxSize * @param $maxSize
* @return resource | bool
* @return resource|bool|\GdImage
*/ */
private function resizeNew($maxSize) { private function resizeNew($maxSize) {
if (!$this->valid()) { if (!$this->valid()) {
$result = $this->preciseResizeNew($width, $height); $result = $this->preciseResizeNew($width, $height);
imagedestroy($this->resource); imagedestroy($this->resource);
$this->resource = $result; $this->resource = $result;
return is_resource($result);
return $this->valid();
} }




/** /**
* @param int $width * @param int $width
* @param int $height * @param int $height
* @return resource | bool
* @return resource|bool|\GdImage
*/ */
public function preciseResizeNew(int $width, int $height) { public function preciseResizeNew(int $width, int $height) {
if (!$this->valid()) { if (!$this->valid()) {
$result = $this->cropNew($x, $y, $w, $h); $result = $this->cropNew($x, $y, $w, $h);
imagedestroy($this->resource); imagedestroy($this->resource);
$this->resource = $result; $this->resource = $result;
return is_resource($result);
return $this->valid();
} }


/** /**
* @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm
* @author mgutt <marc@gutt.it> * @author mgutt <marc@gutt.it>
* @version 1.00 * @version 1.00
* @param resource $im
* @param resource|\GdImage $im
* @param string $fileName [optional] <p>The path to save the file to.</p> * @param string $fileName [optional] <p>The path to save the file to.</p>
* @param int $bit [optional] <p>Bit depth, (default is 24).</p> * @param int $bit [optional] <p>Bit depth, (default is 24).</p>
* @param int $compression [optional] * @param int $compression [optional]

+ 1
- 1
lib/public/IImage.php View File

public function save($filePath = null, $mimeType = null); 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 * @since 8.1.0
*/ */
public function resource(); public function resource();

+ 4
- 0
psalm.xml View File

<errorLevel type="suppress"> <errorLevel type="suppress">
<referencedClass name="OCA\GroupFolders\Mount\GroupFolderStorage"/> <referencedClass name="OCA\GroupFolders\Mount\GroupFolderStorage"/>
<referencedClass name="OCA\TwoFactorNextcloudNotification\Controller\APIController"/> <referencedClass name="OCA\TwoFactorNextcloudNotification\Controller\APIController"/>
<!-- Classes from PHP>=8 (needed to be able to use \GdImage::class) -->
<referencedClass name="GdImage" />
</errorLevel> </errorLevel>
</UndefinedClass> </UndefinedClass>
<UndefinedFunction> <UndefinedFunction>
<!-- Helper classes for sharing API integration from other apps --> <!-- Helper classes for sharing API integration from other apps -->
<referencedClass name="OCA\Deck\Sharing\ShareAPIHelper" /> <referencedClass name="OCA\Deck\Sharing\ShareAPIHelper" />
<referencedClass name="OCA\Talk\Share\Helper\DeletedShareAPIController" /> <referencedClass name="OCA\Talk\Share\Helper\DeletedShareAPIController" />
<!-- Classes from PHP>=8 -->
<referencedClass name="GdImage" />
</errorLevel> </errorLevel>
</UndefinedDocblockClass> </UndefinedDocblockClass>
</issueHandlers> </issueHandlers>

Loading…
Cancel
Save