/** * Resizes, crops, fixes orientation and stores in the cache * * @param int $fileId fileId of the original image */ private function resizeAndStore($fileId) { // Resize and store $this->resizeAndCrop(); // We save a copy in the cache to speed up future calls $cachePath = $this->buildCachePath($fileId); $this->userView->file_put_contents($cachePath, $this->preview->data()); }
/** * Saves a preview in the cache to speed up future calls * * Do not nullify the preview as it might send the whole process in a loop * * @param int $fileId fileId of the original image * @param int $previewWidth * @param int $previewHeight */ private function storePreview($fileId, $previewWidth, $previewHeight) { if (empty($previewWidth) || empty($previewHeight)) { \OCP\Util::writeLog('core', 'Cannot save preview of dimension ' . $previewWidth . 'x' . $previewHeight, \OCP\Util::DEBUG); } else { $cachePath = $this->buildCachePath($fileId, $previewWidth, $previewHeight); $this->userView->file_put_contents($cachePath, $this->preview->data()); } }
/** * Tests if a max size preview of smaller dimensions can be created * * @param \OCP\IImage $preview */ private function doesPreviewFit($preview) { $maxDimRatio = round($this->maxWidth / $this->maxHeight, 2); $previewRatio = round($preview->width() / $preview->height(), 2); // Testing code /*print_r("mw $this->maxWidth "); print_r("mh $this->maxHeight "); print_r("mr $maxDimRatio "); $pw = $preview->width(); $ph = $preview->height(); print_r("pw $pw "); print_r("ph $ph "); print_r("pr $previewRatio ");*/ if ($maxDimRatio < $previewRatio) { $this->assertLessThanOrEqual($this->maxWidth, $preview->width()); $this->assertLessThan($this->maxHeight, $preview->height()); } elseif ($maxDimRatio > $previewRatio) { $this->assertLessThan($this->maxWidth, $preview->width()); $this->assertLessThanOrEqual($this->maxHeight, $preview->height()); } else { // Original had to be resized $this->assertLessThanOrEqual($this->maxWidth, $preview->width()); $this->assertLessThanOrEqual($this->maxHeight, $preview->height()); } }
/** * Mixes a transparent background with a resized foreground preview * * @param \OCP\IImage $previewData * @param int $previewWidth * @param int $previewHeight * @param int $newWidth * @param int $newHeight * @param int $maxWidth * @param int $maxHeight * @param int $newX * @param int $newY * * @return resource */ private function processPreview($previewData, $previewWidth, $previewHeight, $newWidth, $newHeight, $maxWidth, $maxHeight, $newX, $newY) { $fixedPreview = imagecreatetruecolor($maxWidth, $maxHeight); // Creates the canvas // We make the background transparent imagealphablending($fixedPreview, false); $transparency = imagecolorallocatealpha($fixedPreview, 0, 0, 0, 127); imagefill($fixedPreview, 0, 0, $transparency); imagesavealpha($fixedPreview, true); imagecopyresampled($fixedPreview, $previewData->resource(), $newX, $newY, 0, 0, $newWidth, $newHeight, $previewWidth, $previewHeight); return $fixedPreview; }