diff options
author | Robin Appelman <robin@icewind.nl> | 2020-02-16 02:34:09 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2020-04-09 12:50:59 +0200 |
commit | 7d386872e5f4c5530a936465f71b4e1ea85a565e (patch) | |
tree | 78f98df7273c85953350a0365adbdb2e83613e8d /lib/private/legacy | |
parent | 5cd12cd7c39c2ea156ab35266c9359cdd0f4c070 (diff) | |
download | nextcloud-server-7d386872e5f4c5530a936465f71b4e1ea85a565e.tar.gz nextcloud-server-7d386872e5f4c5530a936465f71b4e1ea85a565e.zip |
optimize batch generation of previews
by allowing the generation of multiple previews at once we save on having to find, open and decode the max-preview for every preview of the same file
the main use case for this is the preview generator app (pr for that comming next)
in my local testing this saves about 25% of time when using the preview generator app
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private/legacy')
-rw-r--r-- | lib/private/legacy/OC_Image.php | 102 |
1 files changed, 94 insertions, 8 deletions
diff --git a/lib/private/legacy/OC_Image.php b/lib/private/legacy/OC_Image.php index 7d23ece7ffa..73d380eb99a 100644 --- a/lib/private/legacy/OC_Image.php +++ b/lib/private/legacy/OC_Image.php @@ -39,6 +39,8 @@ * */ +use OCP\IImage; + /** * Class for basic image manipulation */ @@ -845,6 +847,17 @@ class OC_Image implements \OCP\IImage { * @return bool */ public function resize($maxSize) { + $result = $this->resizeNew($maxSize); + imagedestroy($this->resource); + $this->resource = $result; + return is_resource($result); + } + + /** + * @param $maxSize + * @return resource | bool + */ + private function resizeNew($maxSize) { if (!$this->valid()) { $this->logger->error(__METHOD__ . '(): No image loaded', ['app' => 'core']); return false; @@ -861,8 +874,7 @@ class OC_Image implements \OCP\IImage { $newHeight = $maxSize; } - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); - return true; + return $this->preciseResizeNew((int)round($newWidth), (int)round($newHeight)); } /** @@ -871,6 +883,19 @@ class OC_Image implements \OCP\IImage { * @return bool */ public function preciseResize(int $width, int $height): bool { + $result = $this->preciseResizeNew($width, $height); + imagedestroy($this->resource); + $this->resource = $result; + return is_resource($result); + } + + + /** + * @param int $width + * @param int $height + * @return resource | bool + */ + public function preciseResizeNew(int $width, int $height) { if (!$this->valid()) { $this->logger->error(__METHOD__ . '(): No image loaded', ['app' => 'core']); return false; @@ -896,9 +921,7 @@ class OC_Image implements \OCP\IImage { imagedestroy($process); return false; } - imagedestroy($this->resource); - $this->resource = $process; - return true; + return $process; } /** @@ -969,6 +992,22 @@ class OC_Image implements \OCP\IImage { * @return bool for success or failure */ public function crop(int $x, int $y, int $w, int $h): bool { + $result = $this->cropNew($x, $y, $w, $h); + imagedestroy($this->resource); + $this->resource = $result; + return is_resource($result); + } + + /** + * Crops the image from point $x$y with dimension $wx$h. + * + * @param int $x Horizontal position + * @param int $y Vertical position + * @param int $w Width + * @param int $h Height + * @return resource | bool + */ + 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; @@ -993,9 +1032,7 @@ class OC_Image implements \OCP\IImage { imagedestroy($process); return false; } - imagedestroy($this->resource); - $this->resource = $process; - return true; + return $process; } /** @@ -1045,6 +1082,55 @@ class OC_Image implements \OCP\IImage { return false; } + public function copy(): IImage { + $image = new OC_Image(null, $this->logger, $this->config); + $image->resource = imagecreatetruecolor($this->width(), $this->height()); + imagecopy( + $image->resource(), + $this->resource(), + 0, + 0, + 0, + 0, + $this->width(), + $this->height() + ); + + return $image; + } + + public function cropCopy(int $x, int $y, int $w, int $h): IImage { + $image = new OC_Image(null, $this->logger, $this->config); + $image->resource = $this->cropNew($x, $y, $w, $h); + + return $image; + } + + public function preciseResizeCopy(int $width, int $height): IImage { + $image = new OC_Image(null, $this->logger, $this->config); + $image->resource = $this->preciseResizeNew($width, $height); + + return $image; + } + + public function resizeCopy(int $maxSize): IImage { + $image = new OC_Image(null, $this->logger, $this->config); + $image->resource = $this->resizeNew($maxSize); + + return $image; + } + + + /** + * Resizes the image preserving ratio, returning a new copy + * + * @param integer $maxSize The maximum size of either the width or height. + * @return bool + */ + public function copyResize($maxSize): IImage { + + } + /** * Destroys the current image and resets the object */ |