* @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>
* 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.
/**
* 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
*
* @return bool
*/
- public function valid() { // apparently you can't name a method 'empty'...
+ public function valid() {
if (is_resource($this->resource)) {
return true;
}
}
/**
- * @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;
* @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;
* @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;
* @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;
$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;
}
}
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);
* @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;
* @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()) {
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;
}
}
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;
if ($this->valid()) {
imagedestroy($this->resource);
}
- $this->resource = null;
+ $this->resource = false;
}
public function __destruct() {