<?php
/**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
namespace Friendica\Object;
use Exception;
-use Friendica\Core\System;
use Friendica\DI;
use Friendica\Util\Images;
use Imagick;
$this->image->setFormat($format);
// Always coalesce, if it is not a multi-frame image it won't hurt anyway
- $this->image = $this->image->coalesceImages();
+ try {
+ $this->image = $this->image->coalesceImages();
+ } catch (Exception $e) {
+ return false;
+ }
/*
* setup the compression here, so we'll do it only once
switch ($this->getType()) {
case "image/png":
$quality = DI::config()->get('system', 'png_quality');
- if ((! $quality) || ($quality > 9)) {
- $quality = PNG_QUALITY;
- }
/*
* From http://www.imagemagick.org/script/command-line-options.php#quality:
*
break;
case "image/jpeg":
$quality = DI::config()->get('system', 'jpeg_quality');
- if ((! $quality) || ($quality > 100)) {
- $quality = JPEG_QUALITY;
- }
$this->image->setCompressionQuality($quality);
}
}
$this->valid = false;
- $this->image = @imagecreatefromstring($data);
- if ($this->image !== false) {
- $this->width = imagesx($this->image);
- $this->height = imagesy($this->image);
- $this->valid = true;
- imagealphablending($this->image, false);
- imagesavealpha($this->image, true);
-
- return true;
+ try {
+ $this->image = @imagecreatefromstring($data);
+ if ($this->image !== false) {
+ $this->width = imagesx($this->image);
+ $this->height = imagesy($this->image);
+ $this->valid = true;
+ imagealphablending($this->image, false);
+ imagesavealpha($this->image, true);
+
+ return true;
+ }
+ } catch (\Throwable $error) {
+ /** @see https://github.com/php/doc-en/commit/d09a881a8e9059d11e756ee59d75bf404d6941ed */
+ if (strstr($error->getMessage(), "gd-webp cannot allocate temporary buffer")) {
+ DI::logger()->notice('Image is probably animated and therefore unsupported', ['error' => $error]);
+ } else {
+ DI::logger()->warning('Unexpected throwable.', ['error' => $error]);
+ }
}
return false;
}
if ($this->isImagick()) {
- /* Clean it */
- $this->image = $this->image->deconstructImages();
- return $this->image;
+ try {
+ /* Clean it */
+ $this->image = $this->image->deconstructImages();
+ return $this->image;
+ } catch (Exception $e) {
+ return false;
+ }
}
return $this->image;
}
do {
// FIXME - implement horizontal bias for scaling as in following GD functions
// to allow very tall images to be constrained only horizontally.
- $this->image->scaleImage($dest_width, $dest_height);
+ try {
+ $this->image->scaleImage($dest_width, $dest_height);
+ } catch (Exception $e) {
+ // Imagick couldn't use the data
+ return false;
+ }
} while ($this->image->nextImage());
// These may not be necessary anymore
return true;
}
+ /**
+ * Convert a GIF to a PNG to make it static
+ */
+ public function toStatic()
+ {
+ if ($this->type != 'image/gif') {
+ return;
+ }
+
+ if ($this->isImagick()) {
+ $this->type == 'image/png';
+ $this->image->setFormat('png');
+ }
+ }
+
/**
* @param integer $max maximum
* @param integer $x x coordinate
$this->height = imagesy($this->image);
}
- /**
- * @param string $path file path
- * @return mixed
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- */
- public function saveToFilePath($path)
- {
- if (!$this->isValid()) {
- return false;
- }
-
- $string = $this->asString();
-
- $stamp1 = microtime(true);
- file_put_contents($path, $string);
- DI::profiler()->saveTimestamp($stamp1, "file");
- }
-
/**
* Magic method allowing string casting of an Image object
*
}
if ($this->isImagick()) {
- /* Clean it */
- $this->image = $this->image->deconstructImages();
- $string = $this->image->getImagesBlob();
- return $string;
+ try {
+ /* Clean it */
+ $this->image = $this->image->deconstructImages();
+ $string = $this->image->getImagesBlob();
+ return $string;
+ } catch (Exception $e) {
+ return false;
+ }
}
ob_start();
switch ($this->getType()) {
case "image/png":
$quality = DI::config()->get('system', 'png_quality');
- if ((!$quality) || ($quality > 9)) {
- $quality = PNG_QUALITY;
- }
imagepng($this->image, null, $quality);
break;
case "image/jpeg":
$quality = DI::config()->get('system', 'jpeg_quality');
- if ((!$quality) || ($quality > 100)) {
- $quality = JPEG_QUALITY;
- }
imagejpeg($this->image, null, $quality);
}
$string = ob_get_contents();
return $string;
}
-
- /**
- * supported mimetypes and corresponding file extensions
- *
- * @return array
- * @deprecated in version 2019.12 please use Util\Images::supportedTypes() instead.
- */
- public static function supportedTypes()
- {
- return Images::supportedTypes();
- }
-
- /**
- * Maps Mime types to Imagick formats
- *
- * @return array With with image formats (mime type as key)
- * @deprecated in version 2019.12 please use Util\Images::getFormatsMap() instead.
- */
- public static function getFormatsMap()
- {
- return Images::getFormatsMap();
- }
-
- /**
- * @param string $url url
- * @return array
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- * @deprecated in version 2019.12 please use Util\Images::getInfoFromURLCached() instead.
- */
- public static function getInfoFromURL($url)
- {
- return Images::getInfoFromURLCached($url);
- }
-
- /**
- * @param integer $width width
- * @param integer $height height
- * @param integer $max max
- * @return array
- * @deprecated in version 2019.12 please use Util\Images::getScalingDimensions() instead.
- */
- public static function getScalingDimensions($width, $height, $max)
- {
- return Images::getScalingDimensions($width, $height, $max);
- }
}