Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable21] Fix/images php 8.0 #29520

Merged
merged 5 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/private/Avatar/UserAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function set($data) {
/**
* 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) {
Expand All @@ -131,7 +131,10 @@ private function getAvatarImage($data) {
}

$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);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Preview/Bitmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {

//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;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Preview/HEIC.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {

//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;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Preview/SVG.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {

//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);
Expand Down
34 changes: 17 additions & 17 deletions lib/private/legacy/OC_Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function valid() { // apparently you can't name a method 'empty'...
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;
}

Expand Down Expand Up @@ -309,7 +309,7 @@ public function __invoke() {
}

/**
* @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) {
Expand All @@ -319,15 +319,15 @@ public function setResource($resource) {
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;
}
throw new \InvalidArgumentException('Supplied resource is not of type "gd".');
}

/**
* @return resource Returns the image resource in any.
* @return resource|\GdImage Returns the image resource in any.
*/
public function resource() {
return $this->resource;
Expand Down Expand Up @@ -537,7 +537,7 @@ public function fixOrientation() {
* 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);
Expand All @@ -551,7 +551,7 @@ public function loadFromFileHandle($handle) {
* 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
Expand Down Expand Up @@ -667,17 +667,17 @@ public function loadFromFile($imagePath = false) {
* 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);
}
Expand All @@ -693,7 +693,7 @@ public function loadFromData($str) {
* 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)) {
Expand Down Expand Up @@ -723,7 +723,7 @@ public function loadFromBase64($str) {
* @param string $fileName <p>
* Path to the BMP image.
* </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) {
if (!($fh = fopen($fileName, 'rb'))) {
Expand Down Expand Up @@ -879,12 +879,12 @@ public function resize($maxSize) {
$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()) {
Expand Down Expand Up @@ -915,14 +915,14 @@ public function preciseResize(int $width, int $height): bool {
$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()) {
Expand Down Expand Up @@ -1024,7 +1024,7 @@ public function crop(int $x, int $y, int $w, int $h): bool {
$result = $this->cropNew($x, $y, $w, $h);
imagedestroy($this->resource);
$this->resource = $result;
return is_resource($result);
return $this->valid();
}

/**
Expand Down Expand Up @@ -1192,7 +1192,7 @@ public function __destruct() {
* @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm
* @author mgutt <[email protected]>
* @version 1.00
* @param resource $im
* @param resource|\GdImage $im
* @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 $compression [optional]
Expand Down
2 changes: 1 addition & 1 deletion lib/public/IImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function show($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
*/
public function resource();
Expand Down
8 changes: 8 additions & 0 deletions psalm-ocp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@
<extraFiles>
<directory name="3rdparty"/>
</extraFiles>
<issueHandlers>
<UndefinedDocblockClass>
<errorLevel type="suppress">
<!-- Classes from PHP>=8 -->
<referencedClass name="GdImage" />
</errorLevel>
</UndefinedDocblockClass>
</issueHandlers>
</psalm>
4 changes: 4 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
<errorLevel type="suppress">
<referencedClass name="OCA\GroupFolders\Mount\GroupFolderStorage"/>
<referencedClass name="OCA\TwoFactorNextcloudNotification\Controller\APIController"/>
<!-- Classes from PHP>=8 (needed to be able to use \GdImage::class) -->
<referencedClass name="GdImage" />
</errorLevel>
</UndefinedClass>
<UndefinedFunction>
Expand Down Expand Up @@ -124,6 +126,8 @@
<!-- Helper classes for sharing API integration from other apps -->
<referencedClass name="OCA\Deck\Sharing\ShareAPIHelper" />
<referencedClass name="OCA\Talk\Share\Helper\DeletedShareAPIController" />
<!-- Classes from PHP>=8 -->
<referencedClass name="GdImage" />
</errorLevel>
</UndefinedDocblockClass>
</issueHandlers>
Expand Down
20 changes: 0 additions & 20 deletions tests/lib/Avatar/UserAvatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ public function avatarTextData() {
}

public function testGetNoAvatar() {
if (PHP_MAJOR_VERSION > 7) {
$this->markTestSkipped('Only run on php7');
}

$file = $this->createMock(ISimpleFile::class);
$this->folder->method('newFile')
->willReturn($file);
Expand Down Expand Up @@ -91,10 +87,6 @@ public function testGetNoAvatar() {
}

public function testGetAvatarSizeMatch() {
if (PHP_MAJOR_VERSION > 7) {
$this->markTestSkipped('Only run on php7');
}

$this->folder->method('fileExists')
->willReturnMap([
['avatar.jpg', true],
Expand All @@ -112,10 +104,6 @@ public function testGetAvatarSizeMatch() {
}

public function testGetAvatarSizeMinusOne() {
if (PHP_MAJOR_VERSION > 7) {
$this->markTestSkipped('Only run on php7');
}

$this->folder->method('fileExists')
->willReturnMap([
['avatar.jpg', true],
Expand All @@ -132,10 +120,6 @@ public function testGetAvatarSizeMinusOne() {
}

public function testGetAvatarNoSizeMatch() {
if (PHP_MAJOR_VERSION > 7) {
$this->markTestSkipped('Only run on php7');
}

$this->folder->method('fileExists')
->willReturnMap([
['avatar.png', true],
Expand Down Expand Up @@ -200,10 +184,6 @@ public function testExistsPNG() {
}

public function testSetAvatar() {
if (PHP_MAJOR_VERSION > 7) {
$this->markTestSkipped('Only run on php7');
}

$avatarFileJPG = $this->createMock(File::class);
$avatarFileJPG->method('getName')
->willReturn('avatar.jpg');
Expand Down
Loading