// Try to get a cached preview. Else generate (and store) one
try {
- $file = $this->getCachedPreview($previewFolder, $width, $height, $crop);
+ $file = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType());
} catch (NotFoundException $e) {
$file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
}
continue;
}
- $path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.png';
+ $ext = $this->getExtention($preview->dataMimeType());
+ $path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
try {
$file = $previewFolder->newFile($path);
$file->putContent($preview->data());
* @param int $width
* @param int $height
* @param bool $crop
+ * @param string $mimeType
* @return string
*/
- private function generatePath($width, $height, $crop) {
+ private function generatePath($width, $height, $crop, $mimeType) {
$path = (string)$width . '-' . (string)$height;
if ($crop) {
$path .= '-crop';
}
- $path .= '.png';
+
+ $ext = $this->getExtention($mimeType);
+ $path .= '.' . $ext;
return $path;
}
}
- $path = $this->generatePath($width, $height, $crop);
+ $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
try {
$file = $previewFolder->newFile($path);
$file->putContent($preview->data());
* @param int $width
* @param int $height
* @param bool $crop
+ * @param string $mimeType
* @return ISimpleFile
*
* @throws NotFoundException
*/
- private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop) {
- $path = $this->generatePath($width, $height, $crop);
+ private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) {
+ $path = $this->generatePath($width, $height, $crop, $mimeType);
return $previewFolder->getFile($path);
}
return $folder;
}
+
+ /**
+ * @param string $mimeType
+ * @return null|string
+ */
+ private function getExtention($mimeType) {
+ switch ($mimeType) {
+ case 'image/png':
+ return 'png';
+ case 'image/jpeg':
+ return 'jpg';
+ case 'image/gif':
+ return 'gif';
+ default:
+ return null;
+ }
+ }
}
$maxPreview = $this->createMock(ISimpleFile::class);
$maxPreview->method('getName')
->willReturn('1000-1000-max.png');
+ $maxPreview->method('getMimeType')
+ ->willReturn('image/png');
$previewFolder->method('getDirectoryListing')
->willReturn([$maxPreview]);
$image->method('width')->willReturn(2048);
$image->method('height')->willReturn(2048);
$image->method('valid')->willReturn(true);
+ $image->method('dataMimeType')->willReturn('image/png');
$this->helper->method('getThumbnail')
->will($this->returnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) {
$maxPreview = $this->createMock(ISimpleFile::class);
$maxPreview->method('getName')->willReturn('2048-2048-max.png');
+ $maxPreview->method('getMimeType')->willReturn('image/png');
$previewFile = $this->createMock(ISimpleFile::class);
$image->method('data')
->willReturn('my resized data');
$image->method('valid')->willReturn(true);
+ $image->method('dataMimeType')->willReturn('image/png');
$previewFile->expects($this->once())
->method('putContent')
$maxPreview = $this->createMock(ISimpleFile::class);
$maxPreview->method('getName')
->willReturn($maxX . '-' . $maxY . '-max.png');
+ $maxPreview->method('getMimeType')
+ ->willReturn('image/png');
$previewFolder->method('getDirectoryListing')
->willReturn([$maxPreview]);
$image->method('height')->willReturn($maxY);
$image->method('width')->willReturn($maxX);
$image->method('valid')->willReturn(true);
+ $image->method('dataMimeType')->willReturn('image/png');
$preview = $this->createMock(ISimpleFile::class);
$previewFolder->method('newFile')