summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--build/psalm-baseline.xml17
-rw-r--r--lib/private/StreamImage.php34
-rw-r--r--lib/private/legacy/OC_Image.php389
-rw-r--r--lib/public/IImage.php67
-rw-r--r--tests/lib/ImageTest.php2
5 files changed, 100 insertions, 409 deletions
diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml
index ea4b4f7ba62..d1909a62264 100644
--- a/build/psalm-baseline.xml
+++ b/build/psalm-baseline.xml
@@ -3679,23 +3679,6 @@
<code>count($obd_values) &gt; 0</code>
</RedundantCondition>
</file>
- <file src="lib/private/legacy/OC_Image.php">
- <ImplementedReturnTypeMismatch occurrences="1">
- <code>null|string</code>
- </ImplementedReturnTypeMismatch>
- <InvalidArrayOffset occurrences="2">
- <code>$data[floor($p)]</code>
- <code>$data[floor($p)]</code>
- </InvalidArrayOffset>
- <InvalidScalarArgument occurrences="3">
- <code>$this-&gt;bitDepth</code>
- <code>$x</code>
- <code>$y</code>
- </InvalidScalarArgument>
- <RedundantCondition occurrences="1">
- <code>$isWritable</code>
- </RedundantCondition>
- </file>
<file src="lib/private/legacy/OC_User.php">
<UndefinedClass occurrences="1">
<code>\Test\Util\User\Dummy</code>
diff --git a/lib/private/StreamImage.php b/lib/private/StreamImage.php
index 38fd114df17..33078310d27 100644
--- a/lib/private/StreamImage.php
+++ b/lib/private/StreamImage.php
@@ -37,7 +37,7 @@ class StreamImage implements IStreamImage {
/** @var resource The internal stream */
private $stream;
- /** @var string */
+ /** @var null|string */
private $mimeType;
/** @var int */
@@ -55,38 +55,38 @@ class StreamImage implements IStreamImage {
}
/** @inheritDoc */
- public function valid() {
+ public function valid(): bool {
return is_resource($this->stream);
}
/** @inheritDoc */
- public function mimeType() {
+ public function mimeType(): ?string {
return $this->mimeType;
}
/** @inheritDoc */
- public function width() {
+ public function width(): int {
return $this->width;
}
/** @inheritDoc */
- public function height() {
+ public function height(): int {
return $this->height;
}
- public function widthTopLeft() {
+ public function widthTopLeft(): int {
throw new \BadMethodCallException('Not implemented');
}
- public function heightTopLeft() {
+ public function heightTopLeft(): int {
throw new \BadMethodCallException('Not implemented');
}
- public function show($mimeType = null) {
+ public function show(?string $mimeType = null): bool {
throw new \BadMethodCallException('Not implemented');
}
- public function save($filePath = null, $mimeType = null) {
+ public function save(?string $filePath = null, ?string $mimeType = null): bool {
throw new \BadMethodCallException('Not implemented');
}
@@ -94,23 +94,23 @@ class StreamImage implements IStreamImage {
return $this->stream;
}
- public function dataMimeType() {
+ public function dataMimeType(): ?string {
return $this->mimeType;
}
- public function data() {
+ public function data(): ?string {
return '';
}
- public function getOrientation() {
+ public function getOrientation(): int {
throw new \BadMethodCallException('Not implemented');
}
- public function fixOrientation() {
+ public function fixOrientation(): bool {
throw new \BadMethodCallException('Not implemented');
}
- public function resize($maxSize) {
+ public function resize(int $maxSize): bool {
throw new \BadMethodCallException('Not implemented');
}
@@ -118,7 +118,7 @@ class StreamImage implements IStreamImage {
throw new \BadMethodCallException('Not implemented');
}
- public function centerCrop($size = 0) {
+ public function centerCrop(int $size = 0): bool {
throw new \BadMethodCallException('Not implemented');
}
@@ -126,11 +126,11 @@ class StreamImage implements IStreamImage {
throw new \BadMethodCallException('Not implemented');
}
- public function fitIn($maxWidth, $maxHeight) {
+ public function fitIn(int $maxWidth, int $maxHeight): bool {
throw new \BadMethodCallException('Not implemented');
}
- public function scaleDownToFit($maxWidth, $maxHeight) {
+ public function scaleDownToFit(int $maxWidth, int $maxHeight): bool {
throw new \BadMethodCallException('Not implemented');
}
diff --git a/lib/private/legacy/OC_Image.php b/lib/private/legacy/OC_Image.php
index b48d13c4d5b..9ccc6409ba0 100644
--- a/lib/private/legacy/OC_Image.php
+++ b/lib/private/legacy/OC_Image.php
@@ -1,4 +1,7 @@
<?php
+
+declare(strict_types=1);
+
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@@ -50,14 +53,15 @@ class OC_Image implements \OCP\IImage {
// Default memory limit for images to load (128 MBytes).
protected const DEFAULT_MEMORY_LIMIT = 128;
+ // Default quality for jpeg images
+ protected const DEFAULT_JPEG_QUALITY = 80;
+
/** @var false|resource|\GdImage */
protected $resource = false; // tmp resource.
/** @var int */
protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident.
- /** @var string */
+ /** @var null|string */
protected $mimeType = 'image/png'; // Default to png
- /** @var int */
- protected $bitDepth = 24;
/** @var null|string */
protected $filePath = null;
/** @var finfo */
@@ -102,7 +106,7 @@ class OC_Image implements \OCP\IImage {
*
* @return bool
*/
- public function valid() {
+ public function valid(): bool {
if ((is_resource($this->resource) && get_resource_type($this->resource) === 'gd') ||
(is_object($this->resource) && get_class($this->resource) === \GdImage::class)) {
return true;
@@ -112,12 +116,12 @@ class OC_Image implements \OCP\IImage {
}
/**
- * Returns the MIME type of the image or an empty string if no image is loaded.
+ * Returns the MIME type of the image or null if no image is loaded.
*
* @return string
*/
- public function mimeType() {
- return $this->valid() ? $this->mimeType : '';
+ public function mimeType(): ?string {
+ return $this->valid() ? $this->mimeType : null;
}
/**
@@ -125,7 +129,7 @@ class OC_Image implements \OCP\IImage {
*
* @return int
*/
- public function width() {
+ public function width(): int {
if ($this->valid()) {
$width = imagesx($this->resource);
if ($width !== false) {
@@ -140,7 +144,7 @@ class OC_Image implements \OCP\IImage {
*
* @return int
*/
- public function height() {
+ public function height(): int {
if ($this->valid()) {
$height = imagesy($this->resource);
if ($height !== false) {
@@ -155,7 +159,7 @@ class OC_Image implements \OCP\IImage {
*
* @return int
*/
- public function widthTopLeft() {
+ public function widthTopLeft(): int {
$o = $this->getOrientation();
$this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, ['app' => 'core']);
switch ($o) {
@@ -179,7 +183,7 @@ class OC_Image implements \OCP\IImage {
*
* @return int
*/
- public function heightTopLeft() {
+ public function heightTopLeft(): int {
$o = $this->getOrientation();
$this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, ['app' => 'core']);
switch ($o) {
@@ -204,7 +208,7 @@ class OC_Image implements \OCP\IImage {
* @param string $mimeType
* @return bool
*/
- public function show($mimeType = null) {
+ public function show(string $mimeType = null): bool {
if ($mimeType === null) {
$mimeType = $this->mimeType();
}
@@ -220,7 +224,7 @@ class OC_Image implements \OCP\IImage {
* @return bool
*/
- public function save($filePath = null, $mimeType = null) {
+ public function save(?string $filePath = null, ?string $mimeType = null): bool {
if ($mimeType === null) {
$mimeType = $this->mimeType();
}
@@ -243,7 +247,7 @@ class OC_Image implements \OCP\IImage {
* @return bool
* @throws Exception
*/
- private function _output($filePath = null, $mimeType = null) {
+ private function _output(?string $filePath = null, ?string $mimeType = null): bool {
if ($filePath) {
if (!file_exists(dirname($filePath))) {
mkdir(dirname($filePath), 0777, true);
@@ -252,7 +256,7 @@ class OC_Image implements \OCP\IImage {
if (!$isWritable) {
$this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', ['app' => 'core']);
return false;
- } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) {
+ } elseif (file_exists($filePath) && !is_writable($filePath)) {
$this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', ['app' => 'core']);
return false;
}
@@ -309,7 +313,7 @@ class OC_Image implements \OCP\IImage {
$retVal = imagewbmp($this->resource, $filePath);
break;
case IMAGETYPE_BMP:
- $retVal = imagebmp($this->resource, $filePath, $this->bitDepth);
+ $retVal = imagebmp($this->resource, $filePath);
break;
default:
$retVal = imagepng($this->resource, $filePath);
@@ -350,12 +354,11 @@ class OC_Image implements \OCP\IImage {
}
/**
- * @return string Returns the mimetype of the data. Returns the empty string
- * if the data is not valid.
+ * @return string Returns the mimetype of the data. Returns null if the data is not valid.
*/
- public function dataMimeType() {
+ public function dataMimeType(): ?string {
if (!$this->valid()) {
- return '';
+ return null;
}
switch ($this->mimeType) {
@@ -371,7 +374,7 @@ class OC_Image implements \OCP\IImage {
/**
* @return null|string Returns the raw image data.
*/
- public function data() {
+ public function data(): ?string {
if (!$this->valid()) {
return null;
}
@@ -384,11 +387,7 @@ class OC_Image implements \OCP\IImage {
/** @psalm-suppress InvalidScalarArgument */
imageinterlace($this->resource, (PHP_VERSION_ID >= 80000 ? true : 1));
$quality = $this->getJpegQuality();
- if ($quality !== null) {
- $res = imagejpeg($this->resource, null, $quality);
- } else {
- $res = imagejpeg($this->resource);
- }
+ $res = imagejpeg($this->resource, null, $quality);
break;
case "image/gif":
$res = imagegif($this->resource);
@@ -412,14 +411,15 @@ class OC_Image implements \OCP\IImage {
}
/**
- * @return int|null
+ * @return int
*/
- protected function getJpegQuality() {
- $quality = $this->config->getAppValue('preview', 'jpeg_quality', '80');
- if ($quality !== null) {
- $quality = min(100, max(10, (int) $quality));
+ protected function getJpegQuality(): int {
+ $quality = $this->config->getAppValue('preview', 'jpeg_quality', (string) self::DEFAULT_JPEG_QUALITY);
+ // TODO: remove when getAppValue is type safe
+ if ($quality === null) {
+ $quality = self::DEFAULT_JPEG_QUALITY;
}
- return $quality;
+ return min(100, max(10, (int) $quality));
}
/**
@@ -428,7 +428,7 @@ class OC_Image implements \OCP\IImage {
*
* @return int The orientation or -1 if no EXIF data is available.
*/
- public function getOrientation() {
+ public function getOrientation(): int {
if ($this->exif !== null) {
return $this->exif['Orientation'];
}
@@ -460,7 +460,7 @@ class OC_Image implements \OCP\IImage {
return $exif['Orientation'];
}
- public function readExif($data) {
+ public function readExif($data): void {
if (!is_callable('exif_read_data')) {
$this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', ['app' => 'core']);
return;
@@ -486,7 +486,7 @@ class OC_Image implements \OCP\IImage {
*
* @return bool
*/
- public function fixOrientation() {
+ public function fixOrientation(): bool {
if (!$this->valid()) {
$this->logger->debug(__METHOD__ . '(): No image loaded', ['app' => 'core']);
return false;
@@ -716,7 +716,7 @@ class OC_Image implements \OCP\IImage {
}
break;
case IMAGETYPE_BMP:
- $this->resource = $this->imagecreatefrombmp($imagePath);
+ $this->resource = imagecreatefrombmp($imagePath);
break;
case IMAGETYPE_WEBP:
if (imagetypes() & IMG_WEBP) {
@@ -778,10 +778,7 @@ class OC_Image implements \OCP\IImage {
* @param string $str A string of image data as read from a file.
* @return bool|resource|\GdImage An image resource or false on error
*/
- public function loadFromData($str) {
- if (!is_string($str)) {
- return false;
- }
+ public function loadFromData(string $str) {
if (!$this->checkImageDataSize($str)) {
return false;
}
@@ -807,10 +804,7 @@ class OC_Image implements \OCP\IImage {
* @param string $str A string base64 encoded string of image data.
* @return bool|resource|\GdImage An image resource or false on error
*/
- public function loadFromBase64($str) {
- if (!is_string($str)) {
- return false;
- }
+ public function loadFromBase64(string $str) {
$data = base64_decode($str);
if ($data) { // try to load from string data
if (!$this->checkImageDataSize($data)) {
@@ -831,170 +825,12 @@ class OC_Image implements \OCP\IImage {
}
/**
- * Create a new image from file or URL
- *
- * @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm
- * @version 1.00
- * @param string $fileName <p>
- * Path to the BMP image.
- * </p>
- * @return bool|resource|\GdImage an image resource identifier on success, <b>FALSE</b> on errors.
- */
- private function imagecreatefrombmp($fileName) {
- if (!($fh = fopen($fileName, 'rb'))) {
- $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, ['app' => 'core']);
- return false;
- }
- // read file header
- $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14));
- // check for bitmap
- if ($meta['type'] != 19778) {
- fclose($fh);
- $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', ['app' => 'core']);
- return false;
- }
- // read image header
- $meta += unpack('Vheadersize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vcolors/Vimportant', fread($fh, 40));
- // read additional 16bit header
- if ($meta['bits'] == 16) {
- $meta += unpack('VrMask/VgMask/VbMask', fread($fh, 12));
- }
- // set bytes and padding
- $meta['bytes'] = $meta['bits'] / 8;
- $this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call
- $meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4)));
- if ($meta['decal'] == 4) {
- $meta['decal'] = 0;
- }
- // obtain imagesize
- if ($meta['imagesize'] < 1) {
- $meta['imagesize'] = $meta['filesize'] - $meta['offset'];
- // in rare cases filesize is equal to offset so we need to read physical size
- if ($meta['imagesize'] < 1) {
- $meta['imagesize'] = @filesize($fileName) - $meta['offset'];
- if ($meta['imagesize'] < 1) {
- fclose($fh);
- $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', ['app' => 'core']);
- return false;
- }
- }
- }
- // calculate colors
- $meta['colors'] = !$meta['colors'] ? pow(2, $meta['bits']) : $meta['colors'];
- // read color palette
- $palette = [];
- if ($meta['bits'] < 16) {
- $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4));
- // in rare cases the color value is signed
- if ($palette[1] < 0) {
- foreach ($palette as $i => $color) {
- $palette[$i] = $color + 16777216;
- }
- }
- }
- if (!$this->checkImageMemory($meta['width'], $meta['height'])) {
- fclose($fh);
- return false;
- }
- // create gd image
- $im = imagecreatetruecolor($meta['width'], $meta['height']);
- if ($im == false) {
- fclose($fh);
- $this->logger->warning(
- 'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'],
- ['app' => 'core']);
- return false;
- }
-
- $data = fread($fh, $meta['imagesize']);
- $p = 0;
- $vide = chr(0);
- $y = $meta['height'] - 1;
- $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!';
- // loop through the image data beginning with the lower left corner
- while ($y >= 0) {
- $x = 0;
- while ($x < $meta['width']) {
- switch ($meta['bits']) {
- case 32:
- case 24:
- if (!($part = substr($data, $p, 3))) {
- $this->logger->warning($error, ['app' => 'core']);
- return $im;
- }
- $color = @unpack('V', $part . $vide);
- break;
- case 16:
- if (!($part = substr($data, $p, 2))) {
- fclose($fh);
- $this->logger->warning($error, ['app' => 'core']);
- return $im;
- }
- $color = @unpack('v', $part);
- $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3);
- break;
- case 8:
- $color = @unpack('n', $vide . ($data[$p] ?? ''));
- $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1];
- break;
- case 4:
- $color = @unpack('n', $vide . ($data[floor($p)] ?? ''));
- $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F;
- $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1];
- break;
- case 1:
- $color = @unpack('n', $vide . ($data[floor($p)] ?? ''));
- switch (($p * 8) % 8) {
- case 0:
- $color[1] = $color[1] >> 7;
- break;
- case 1:
- $color[1] = ($color[1] & 0x40) >> 6;
- break;
- case 2:
- $color[1] = ($color[1] & 0x20) >> 5;
- break;
- case 3:
- $color[1] = ($color[1] & 0x10) >> 4;
- break;
- case 4:
- $color[1] = ($color[1] & 0x8) >> 3;
- break;
- case 5:
- $color[1] = ($color[1] & 0x4) >> 2;
- break;
- case 6:
- $color[1] = ($color[1] & 0x2) >> 1;
- break;
- case 7:
- $color[1] = ($color[1] & 0x1);
- break;
- }
- $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1];
- break;
- default:
- fclose($fh);
- $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', ['app' => 'core']);
- return false;
- }
- imagesetpixel($im, $x, $y, $color[1]);
- $x++;
- $p += $meta['bytes'];
- }
- $y--;
- $p += $meta['decal'];
- }
- fclose($fh);
- return $im;
- }
-
- /**
* Resizes the image preserving ratio.
*
- * @param integer $maxSize The maximum size of either the width or height.
+ * @param int $maxSize The maximum size of either the width or height.
* @return bool
*/
- public function resize($maxSize) {
+ public function resize(int $maxSize): bool {
if (!$this->valid()) {
$this->logger->debug(__METHOD__ . '(): No image loaded', ['app' => 'core']);
return false;
@@ -1009,7 +845,7 @@ class OC_Image implements \OCP\IImage {
* @param $maxSize
* @return resource|bool|\GdImage
*/
- private function resizeNew($maxSize) {
+ private function resizeNew(int $maxSize) {
if (!$this->valid()) {
$this->logger->debug(__METHOD__ . '(): No image loaded', ['app' => 'core']);
return false;
@@ -1045,7 +881,6 @@ class OC_Image implements \OCP\IImage {
return $this->valid();
}
-
/**
* @param int $width
* @param int $height
@@ -1090,7 +925,7 @@ class OC_Image implements \OCP\IImage {
* @param int $size maximum size for the result (optional)
* @return bool for success or failure
*/
- public function centerCrop($size = 0) {
+ public function centerCrop(int $size = 0): bool {
if (!$this->valid()) {
$this->logger->debug('OC_Image->centerCrop, No image loaded', ['app' => 'core']);
return false;
@@ -1104,10 +939,10 @@ class OC_Image implements \OCP\IImage {
$width = $height = min($widthOrig, $heightOrig);
if ($ratioOrig > 1) {
- $x = ($widthOrig / 2) - ($width / 2);
+ $x = (int) (($widthOrig / 2) - ($width / 2));
$y = 0;
} else {
- $y = ($heightOrig / 2) - ($height / 2);
+ $y = (int) (($heightOrig / 2) - ($height / 2));
$x = 0;
}
if ($size > 0) {
@@ -1200,11 +1035,11 @@ class OC_Image implements \OCP\IImage {
*
* Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up
*
- * @param integer $maxWidth
- * @param integer $maxHeight
+ * @param int $maxWidth
+ * @param int $maxHeight
* @return bool
*/
- public function fitIn($maxWidth, $maxHeight) {
+ public function fitIn(int $maxWidth, int $maxHeight): bool {
if (!$this->valid()) {
$this->logger->debug(__METHOD__ . '(): No image loaded', ['app' => 'core']);
return false;
@@ -1223,11 +1058,11 @@ class OC_Image implements \OCP\IImage {
/**
* Shrinks larger images to fit within specified boundaries while preserving ratio.
*
- * @param integer $maxWidth
- * @param integer $maxHeight
+ * @param int $maxWidth
+ * @param int $maxHeight
* @return bool
*/
- public function scaleDownToFit($maxWidth, $maxHeight) {
+ public function scaleDownToFit(int $maxWidth, int $maxHeight): bool {
if (!$this->valid()) {
$this->logger->debug(__METHOD__ . '(): No image loaded', ['app' => 'core']);
return false;
@@ -1263,7 +1098,6 @@ class OC_Image implements \OCP\IImage {
$image = new OC_Image(null, $this->logger, $this->config);
$image->imageType = $this->imageType;
$image->mimeType = $this->mimeType;
- $image->bitDepth = $this->bitDepth;
$image->resource = $this->cropNew($x, $y, $w, $h);
return $image;
@@ -1273,7 +1107,6 @@ class OC_Image implements \OCP\IImage {
$image = new OC_Image(null, $this->logger, $this->config);
$image->imageType = $this->imageType;
$image->mimeType = $this->mimeType;
- $image->bitDepth = $this->bitDepth;
$image->resource = $this->preciseResizeNew($width, $height);
return $image;
@@ -1283,7 +1116,6 @@ class OC_Image implements \OCP\IImage {
$image = new OC_Image(null, $this->logger, $this->config);
$image->imageType = $this->imageType;
$image->mimeType = $this->mimeType;
- $image->bitDepth = $this->bitDepth;
$image->resource = $this->resizeNew($maxSize);
return $image;
@@ -1292,7 +1124,7 @@ class OC_Image implements \OCP\IImage {
/**
* Destroys the current image and resets the object
*/
- public function destroy() {
+ public function destroy(): void {
if ($this->valid()) {
imagedestroy($this->resource);
}
@@ -1304,123 +1136,6 @@ class OC_Image implements \OCP\IImage {
}
}
-if (!function_exists('imagebmp')) {
- /**
- * Output a BMP image to either the browser or a file
- *
- * @link http://www.ugia.cn/wp-data/imagebmp.php
- * @author legend <legendsky@hotmail.com>
- * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm
- * @author mgutt <marc@gutt.it>
- * @version 1.00
- * @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]
- * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
- */
- function imagebmp($im, $fileName = '', $bit = 24, $compression = 0) {
- if (!in_array($bit, [1, 4, 8, 16, 24, 32])) {
- $bit = 24;
- } elseif ($bit == 32) {
- $bit = 24;
- }
- $bits = (int)pow(2, $bit);
- imagetruecolortopalette($im, true, $bits);
- $width = imagesx($im);
- $height = imagesy($im);
- $colorsNum = imagecolorstotal($im);
- $rgbQuad = '';
- if ($bit <= 8) {
- for ($i = 0; $i < $colorsNum; $i++) {
- $colors = imagecolorsforindex($im, $i);
- $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0";
- }
- $bmpData = '';
- if ($compression == 0 || $bit < 8) {
- $compression = 0;
- $extra = '';
- $padding = 4 - ceil($width / (8 / $bit)) % 4;
- if ($padding % 4 != 0) {
- $extra = str_repeat("\0", $padding);
- }
- for ($j = $height - 1; $j >= 0; $j--) {
- $i = 0;
- while ($i < $width) {
- $bin = 0;
- $limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0;
- for ($k = 8 - $bit; $k >= $limit; $k -= $bit) {
- $index = imagecolorat($im, $i, $j);
- $bin |= $index << $k;
- $i++;
- }
- $bmpData .= chr($bin);
- }
- $bmpData .= $extra;
- }
- } // RLE8
- elseif ($compression == 1 && $bit == 8) {
- for ($j = $height - 1; $j >= 0; $j--) {
- $lastIndex = null;
- $sameNum = 0;
- for ($i = 0; $i <= $width; $i++) {
- $index = imagecolorat($im, $i, $j);
- if ($index !== $lastIndex || $sameNum > 255) {
- if ($sameNum != 0) {
- $bmpData .= chr($sameNum) . chr($lastIndex);
- }
- $lastIndex = $index;
- $sameNum = 1;
- } else {
- $sameNum++;
- }
- }
- $bmpData .= "\0\0";
- }
- $bmpData .= "\0\1";
- }
- $sizeQuad = strlen($rgbQuad);
- $sizeData = strlen($bmpData);
- } else {
- $extra = '';
- $padding = 4 - ($width * ($bit / 8)) % 4;
- if ($padding % 4 != 0) {
- $extra = str_repeat("\0", $padding);
- }
- $bmpData = '';
- for ($j = $height - 1; $j >= 0; $j--) {
- for ($i = 0; $i < $width; $i++) {
- $index = imagecolorat($im, $i, $j);
- $colors = imagecolorsforindex($im, $index);
- if ($bit == 16) {
- $bin = 0 << $bit;
- $bin |= ($colors['red'] >> 3) << 10;
- $bin |= ($colors['green'] >> 3) << 5;
- $bin |= $colors['blue'] >> 3;
- $bmpData .= pack("v", $bin);
- } else {
- $bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']);
- }
- }
- $bmpData .= $extra;
- }
- $sizeQuad = 0;
- $sizeData = strlen($bmpData);
- $colorsNum = 0;
- }
- $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad);
- $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0);
- if ($fileName != '') {
- $fp = fopen($fileName, 'wb');
- fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData);
- fclose($fp);
- return true;
- }
- echo $fileHeader . $infoHeader . $rgbQuad . $bmpData;
- return true;
- }
-}
-
if (!function_exists('exif_imagetype')) {
/**
* Workaround if exif_imagetype does not exist
@@ -1429,7 +1144,7 @@ if (!function_exists('exif_imagetype')) {
* @param string $fileName
* @return string|boolean
*/
- function exif_imagetype($fileName) {
+ function exif_imagetype(string $fileName) {
if (($info = getimagesize($fileName)) !== false) {
return $info[2];
}
diff --git a/lib/public/IImage.php b/lib/public/IImage.php
index 659cd24720d..f1ac3bf1a50 100644
--- a/lib/public/IImage.php
+++ b/lib/public/IImage.php
@@ -1,4 +1,7 @@
<?php
+
+declare(strict_types=1);
+
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@@ -33,69 +36,60 @@ interface IImage {
/**
* Determine whether the object contains an image resource.
*
- * @return bool
* @since 8.1.0
*/
- public function valid();
+ public function valid(): bool;
/**
- * Returns the MIME type of the image or an empty string if no image is loaded.
+ * Returns the MIME type of the image or null if no image is loaded.
*
- * @return string
* @since 8.1.0
*/
- public function mimeType();
+ public function mimeType(): ?string;
/**
* Returns the width of the image or -1 if no image is loaded.
*
- * @return int
* @since 8.1.0
*/
- public function width();
+ public function width(): int;
/**
* Returns the height of the image or -1 if no image is loaded.
*
- * @return int
* @since 8.1.0
*/
- public function height();
+ public function height(): int;
/**
* Returns the width when the image orientation is top-left.
*
- * @return int
* @since 8.1.0
*/
- public function widthTopLeft();
+ public function widthTopLeft(): int;
/**
* Returns the height when the image orientation is top-left.
*
- * @return int
* @since 8.1.0
*/
- public function heightTopLeft();
+ public function heightTopLeft(): int;
/**
* Outputs the image.
*
- * @param string $mimeType
- * @return bool
* @since 8.1.0
*/
- public function show($mimeType = null);
+ public function show(?string $mimeType = null): bool;
/**
* Saves the image.
*
* @param string $filePath
* @param string $mimeType
- * @return bool
* @since 8.1.0
*/
- public function save($filePath = null, $mimeType = null);
+ public function save(?string $filePath = null, ?string $mimeType = null): bool;
/**
* @return false|resource|\GdImage Returns the image resource if any
@@ -104,16 +98,17 @@ interface IImage {
public function resource();
/**
- * @return string Returns the raw data mimetype
+ * @return string Returns the mimetype of the data. Returns null
+ * if the data is not valid.
* @since 13.0.0
*/
- public function dataMimeType();
+ public function dataMimeType(): ?string;
/**
* @return string Returns the raw image data.
* @since 8.1.0
*/
- public function data();
+ public function data(): ?string;
/**
* (I'm open for suggestions on better method name ;)
@@ -122,25 +117,23 @@ interface IImage {
* @return int The orientation or -1 if no EXIF data is available.
* @since 8.1.0
*/
- public function getOrientation();
+ public function getOrientation(): int;
/**
* (I'm open for suggestions on better method name ;)
* Fixes orientation based on EXIF data.
*
- * @return bool
* @since 8.1.0
*/
- public function fixOrientation();
+ public function fixOrientation(): bool;
/**
* Resizes the image preserving ratio.
*
* @param integer $maxSize The maximum size of either the width or height.
- * @return bool
* @since 8.1.0
*/
- public function resize($maxSize);
+ public function resize(int $maxSize): bool;
/**
* @param int $width
@@ -157,7 +150,7 @@ interface IImage {
* @return bool for success or failure
* @since 8.1.0
*/
- public function centerCrop($size = 0);
+ public function centerCrop(int $size = 0): bool;
/**
* Crops the image from point $x$y with dimension $wx$h.
@@ -174,22 +167,22 @@ interface IImage {
/**
* Resizes the image to fit within a boundary while preserving ratio.
*
- * @param integer $maxWidth
- * @param integer $maxHeight
- * @return bool
+ * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up
+ *
+ * @param int $maxWidth
+ * @param int $maxHeight
* @since 8.1.0
*/
- public function fitIn($maxWidth, $maxHeight);
+ public function fitIn(int $maxWidth, int $maxHeight): bool;
/**
* Shrinks the image to fit within a boundary while preserving ratio.
*
- * @param integer $maxWidth
- * @param integer $maxHeight
- * @return bool
+ * @param int $maxWidth
+ * @param int $maxHeight
* @since 8.1.0
*/
- public function scaleDownToFit($maxWidth, $maxHeight);
+ public function scaleDownToFit(int $maxWidth, int $maxHeight): bool;
/**
* create a copy of this image
@@ -222,9 +215,9 @@ interface IImage {
public function preciseResizeCopy(int $width, int $height): IImage;
/**
- * create a new resized copy of this image
+ * Resizes the image preserving ratio, returning a new copy
*
- * @param integer $maxSize The maximum size of either the width or height.
+ * @param int $maxSize The maximum size of either the width or height.
* @return IImage
* @since 19.0.0
*/
diff --git a/tests/lib/ImageTest.php b/tests/lib/ImageTest.php
index ace3f1abcec..2a753b01550 100644
--- a/tests/lib/ImageTest.php
+++ b/tests/lib/ImageTest.php
@@ -152,7 +152,7 @@ class ImageTest extends \Test\TestCase {
/** @psalm-suppress InvalidScalarArgument */
imageinterlace($raw, (PHP_VERSION_ID >= 80000 ? true : 1));
ob_start();
- imagejpeg($raw);
+ imagejpeg($raw, null, 80);
$expected = ob_get_clean();
$this->assertEquals($expected, $img->data());