Merge pull request #7836 from nextcloud/image-type-hinting

Fix float/integer handling in image API
This commit is contained in:
Morris Jobke 2018-01-17 11:40:35 +01:00 committed by GitHub
commit 9222dcd940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 10 deletions

View File

@ -345,9 +345,9 @@ class Generator {
$scaleH = $maxHeight / $widthR; $scaleH = $maxHeight / $widthR;
$scaleW = $width; $scaleW = $width;
} }
$preview->preciseResize(round($scaleW), round($scaleH)); $preview->preciseResize((int)round($scaleW), (int)round($scaleH));
} }
$cropX = floor(abs($width - $preview->width()) * 0.5); $cropX = (int)floor(abs($width - $preview->width()) * 0.5);
$cropY = 0; $cropY = 0;
$preview->crop($cropX, $cropY, $width, $height); $preview->crop($cropX, $cropY, $width, $height);
} else { } else {

View File

@ -27,7 +27,7 @@ use OCP\Files\File;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IImage; use OCP\IImage;
use OCP\Image as img; use OCP\Image as OCPImage;
use OCP\Preview\IProvider; use OCP\Preview\IProvider;
/** /**
@ -79,7 +79,9 @@ class GeneratorHelper {
* @return IImage * @return IImage
*/ */
public function getImage(ISimpleFile $maxPreview) { public function getImage(ISimpleFile $maxPreview) {
return new img($maxPreview->getContent()); $image = new OCPImage();
$image->loadFromData($maxPreview->getContent());
return $image;
} }
/** /**

View File

@ -550,6 +550,16 @@ class OC_Image implements \OCP\IImage {
* @return bool|resource An image resource or false on error * @return bool|resource An image resource or false on error
*/ */
public function loadFromFile($imagePath = false) { public function loadFromFile($imagePath = false) {
try {
// detect if it is a path or maybe the images as string
// needed because the constructor iterates over all load* methods
$result = @realpath($imagePath);
if ($result === false) {
return false;
}
} catch (Error $e) {
return false;
}
// exif_imagetype throws "read error!" if file is less than 12 byte // exif_imagetype throws "read error!" if file is less than 12 byte
if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) { if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) {
return false; return false;
@ -873,7 +883,7 @@ class OC_Image implements \OCP\IImage {
$newHeight = $maxSize; $newHeight = $maxSize;
} }
$this->preciseResize(round($newWidth), round($newHeight)); $this->preciseResize((int)round($newWidth), (int)round($newHeight));
return true; return true;
} }
@ -882,7 +892,7 @@ class OC_Image implements \OCP\IImage {
* @param int $height * @param int $height
* @return bool * @return bool
*/ */
public function preciseResize($width, $height) { public function preciseResize(int $width, int $height): bool {
if (!$this->valid()) { if (!$this->valid()) {
$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
return false; return false;
@ -982,7 +992,7 @@ class OC_Image implements \OCP\IImage {
* @param int $h Height * @param int $h Height
* @return bool for success or failure * @return bool for success or failure
*/ */
public function crop($x, $y, $w, $h) { public function crop(int $x, int $y, int $w, int $h): bool {
if (!$this->valid()) { if (!$this->valid()) {
$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
return false; return false;
@ -1033,7 +1043,7 @@ class OC_Image implements \OCP\IImage {
$newWidth = min($maxWidth, $ratio * $maxHeight); $newWidth = min($maxWidth, $ratio * $maxHeight);
$newHeight = min($maxHeight, $maxWidth / $ratio); $newHeight = min($maxHeight, $maxWidth / $ratio);
$this->preciseResize(round($newWidth), round($newHeight)); $this->preciseResize((int)round($newWidth), (int)round($newHeight));
return true; return true;
} }

View File

@ -147,7 +147,7 @@ interface IImage {
* @return bool * @return bool
* @since 8.1.0 * @since 8.1.0
*/ */
public function preciseResize($width, $height); public function preciseResize(int $width, int $height): bool;
/** /**
* Crops the image to the middle square. If the image is already square it just returns. * Crops the image to the middle square. If the image is already square it just returns.
@ -168,7 +168,7 @@ interface IImage {
* @return bool for success or failure * @return bool for success or failure
* @since 8.1.0 * @since 8.1.0
*/ */
public function crop($x, $y, $w, $h); public function crop(int $x, int $y, int $w, int $h): bool;
/** /**
* Resizes the image to fit within a boundary while preserving ratio. * Resizes the image to fit within a boundary while preserving ratio.