Skip to content

Commit

Permalink
Merge pull request #30115 from nextcloud/backport/30040/stable23
Browse files Browse the repository at this point in the history
[stable23] Avoid calling image* methods on boolean
  • Loading branch information
come-nc authored Dec 6, 2021
2 parents b296792 + f889b25 commit c630c8c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
57 changes: 41 additions & 16 deletions lib/private/legacy/OC_Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @author Bart Visscher <bartv@thisnet.nl>
* @author Björn Schießle <bjoern@schiessle.org>
* @author Byron Marohn <combustible@live.com>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
* @author Christopher Schäpers <kondou@ts.unde.re>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Georg Ehrke <oc.list@georgehrke.com>
Expand Down Expand Up @@ -45,7 +46,7 @@
* Class for basic image manipulation
*/
class OC_Image implements \OCP\IImage {
/** @var false|resource */
/** @var false|resource|\GdImage */
protected $resource = false; // tmp resource.
/** @var int */
protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident.
Expand All @@ -67,7 +68,7 @@ class OC_Image implements \OCP\IImage {
/**
* Constructor.
*
* @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by
* @param resource|string|\GdImage $imageRef The path to a local file, a base64 encoded string or a resource created by
* an imagecreate* function.
* @param \OCP\ILogger $logger
* @param \OCP\IConfig $config
Expand Down Expand Up @@ -97,7 +98,7 @@ public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\
*
* @return bool
*/
public function valid() { // apparently you can't name a method 'empty'...
public function valid() {
if (is_resource($this->resource)) {
return true;
}
Expand All @@ -123,7 +124,13 @@ public function mimeType() {
* @return int
*/
public function width() {
return $this->valid() ? imagesx($this->resource) : -1;
if ($this->valid()) {
$width = imagesx($this->resource);
if ($width !== false) {
return $width;
}
}
return -1;
}

/**
Expand All @@ -132,7 +139,13 @@ public function width() {
* @return int
*/
public function height() {
return $this->valid() ? imagesy($this->resource) : -1;
if ($this->valid()) {
$height = imagesy($this->resource);
if ($height !== false) {
return $height;
}
}
return -1;
}

/**
Expand Down Expand Up @@ -326,7 +339,7 @@ public function setResource($resource) {
}

/**
* @return resource|\GdImage Returns the image resource in any.
* @return false|resource|\GdImage Returns the image resource if any
*/
public function resource() {
return $this->resource;
Expand Down Expand Up @@ -468,6 +481,10 @@ public function readExif($data) {
* @return bool
*/
public function fixOrientation() {
if (!$this->valid()) {
$this->logger->error(__METHOD__ . '(): No image loaded', ['app' => 'core']);
return false;
}
$o = $this->getOrientation();
$this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, ['app' => 'core']);
$rotate = 0;
Expand Down Expand Up @@ -875,6 +892,10 @@ private function imagecreatefrombmp($fileName) {
* @return bool
*/
public function resize($maxSize) {
if (!$this->valid()) {
$this->logger->error(__METHOD__ . '(): No image loaded', ['app' => 'core']);
return false;
}
$result = $this->resizeNew($maxSize);
imagedestroy($this->resource);
$this->resource = $result;
Expand Down Expand Up @@ -911,6 +932,10 @@ private function resizeNew($maxSize) {
* @return bool
*/
public function preciseResize(int $width, int $height): bool {
if (!$this->valid()) {
$this->logger->error(__METHOD__ . '(): No image loaded', ['app' => 'core']);
return false;
}
$result = $this->preciseResizeNew($width, $height);
imagedestroy($this->resource);
$this->resource = $result;
Expand Down Expand Up @@ -990,9 +1015,8 @@ public function centerCrop($size = 0) {
$targetHeight = $height;
}
$process = imagecreatetruecolor($targetWidth, $targetHeight);
if ($process == false) {
if ($process === false) {
$this->logger->error('OC_Image->centerCrop, Error creating true color image', ['app' => 'core']);
imagedestroy($process);
return false;
}

Expand All @@ -1004,9 +1028,8 @@ public function centerCrop($size = 0) {
}

imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
if ($process == false) {
if ($process === false) {
$this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, ['app' => 'core']);
imagedestroy($process);
return false;
}
imagedestroy($this->resource);
Expand All @@ -1024,6 +1047,10 @@ public function centerCrop($size = 0) {
* @return bool for success or failure
*/
public function crop(int $x, int $y, int $w, int $h): bool {
if (!$this->valid()) {
$this->logger->error(__METHOD__ . '(): No image loaded', ['app' => 'core']);
return false;
}
$result = $this->cropNew($x, $y, $w, $h);
imagedestroy($this->resource);
$this->resource = $result;
Expand All @@ -1037,17 +1064,16 @@ public function crop(int $x, int $y, int $w, int $h): bool {
* @param int $y Vertical position
* @param int $w Width
* @param int $h Height
* @return resource | bool
* @return resource|\GdImage|false
*/
public function cropNew(int $x, int $y, int $w, int $h) {
if (!$this->valid()) {
$this->logger->error(__METHOD__ . '(): No image loaded', ['app' => 'core']);
return false;
}
$process = imagecreatetruecolor($w, $h);
if ($process == false) {
if ($process === false) {
$this->logger->error(__METHOD__ . '(): Error creating true color image', ['app' => 'core']);
imagedestroy($process);
return false;
}

Expand All @@ -1059,9 +1085,8 @@ public function cropNew(int $x, int $y, int $w, int $h) {
}

imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h);
if ($process == false) {
if ($process === false) {
$this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, ['app' => 'core']);
imagedestroy($process);
return false;
}
return $process;
Expand Down Expand Up @@ -1168,7 +1193,7 @@ public function destroy() {
if ($this->valid()) {
imagedestroy($this->resource);
}
$this->resource = null;
$this->resource = false;
}

public function __destruct() {
Expand Down
2 changes: 1 addition & 1 deletion lib/public/IImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function show($mimeType = null);
public function save($filePath = null, $mimeType = null);

/**
* @return resource|\GdImage Returns the image resource in any.
* @return false|resource|\GdImage Returns the image resource if any
* @since 8.1.0
*/
public function resource();
Expand Down

0 comments on commit c630c8c

Please sign in to comment.