@@ -261,54 +261,111 @@ private function getConvertedFilePath(Node $file): string {
261261 return $ path ;
262262 }
263263
264- // Create a temporary file *with the correct extension*
265- $ tmpname = $ this ->tempManager ->getTemporaryFile ('.jpg ' );
266-
267- if (!$ this ->previewProvider ->isAvailable ($ file ) && !($ file instanceof File)) {
268- return $ path ;
269- }
270-
271264 try {
272265 $ this ->logger ->debug ('generating preview of ' . $ file ->getId () . ' with dimension ' .self ::TEMP_FILE_DIMENSION );
273- $ image = $ this ->previewProvider ->getPreview ($ file , self ::TEMP_FILE_DIMENSION , self ::TEMP_FILE_DIMENSION );
266+
267+ $ imageType = exif_imagetype ($ path ); //To troubleshoot console errors, GD does not support all formats.
268+ if (0 < $ imageType && $ this ->config ->getSystemValueString ('recognize.preview.gd ' , 'true ' ) === 'true ' ) {
269+ return $ this ->generatePrevieWithGD ($ path );
270+ } else {
271+ if (!$ this ->previewProvider ->isAvailable ($ file )) {
272+ return $ path ;
273+ }
274+ return $ this ->generatePreviewWithProvider ($ file );
275+ }
274276 } catch (\Throwable $ e ) {
275- $ this ->logger ->info ('Failed to generate preview of ' . $ file ->getId () . ' with dimension ' .self ::TEMP_FILE_DIMENSION . ': ' . $ e ->getMessage ());
277+ $ this ->logger ->warning ('Failed to generate preview of ' . $ file ->getId () . ' with dimension ' .self ::TEMP_FILE_DIMENSION . ': ' . $ e ->getMessage ());
276278 return $ path ;
277279 }
280+ }
278281
279- $ tmpfile = fopen ($ tmpname , 'wb ' );
282+ public function cleanUpTmpFiles ():void {
283+ $ this ->tempManager ->clean ();
284+ }
280285
281- if ($ tmpfile === false ) {
282- $ this ->logger ->warning ('Could not open tmpfile ' );
283- return $ path ;
284- }
286+ /**
287+ * @param File $file
288+ * @return string
289+ * @throws \OCA\Recognize\Exception\Exception|NotFoundException
290+ */
291+ public function generatePreviewWithProvider (File $ file ): string {
292+ $ image = $ this ->previewProvider ->getPreview ($ file , self ::TEMP_FILE_DIMENSION , self ::TEMP_FILE_DIMENSION );
285293
286294 try {
287295 $ preview = $ image ->read ();
288296 } catch (NotPermittedException $ e ) {
289- $ this ->logger ->warning ('Could not read preview file ' , ['exception ' => $ e ]);
290- return $ path ;
297+ throw new \OCA \Recognize \Exception \Exception ('Could not read preview file ' , 0 , $ e );
291298 }
292299
293300 if ($ preview === false ) {
294- $ this ->logger ->warning ('Could not open preview file ' );
295- return $ path ;
301+ throw new \OCA \Recognize \Exception \Exception ('Could not open preview file ' );
296302 }
297303
298- $ this ->logger ->debug ('Copying ' . $ file ->getId () . ' preview to tempfolder ' );
299- if (stream_copy_to_stream ($ preview , $ tmpfile ) === false ) {
300- $ this ->logger ->warning ('Could not copy preview file to temp folder ' );
301- fclose ($ preview );
302- fclose ($ tmpfile );
303- return $ path ;
304+ // Create a temporary file *with the correct extension*
305+ $ tmpname = $ this ->tempManager ->getTemporaryFile ('.jpg ' );
306+
307+ $ tmpfile = fopen ($ tmpname , 'wb ' );
308+
309+ if ($ tmpfile === false ) {
310+ throw new \OCA \Recognize \Exception \Exception ('Could not open tmpfile ' );
304311 }
312+
313+ $ copyResult = stream_copy_to_stream ($ preview , $ tmpfile );
305314 fclose ($ preview );
306315 fclose ($ tmpfile );
307316
317+ if ($ copyResult === false ) {
318+ throw new \OCA \Recognize \Exception \Exception ('Could not copy preview file to temp folder ' );
319+ }
320+
321+ $ imagetype = exif_imagetype ($ tmpname );
322+
323+ if (in_array ($ imagetype , [IMAGETYPE_WEBP , IMAGETYPE_AVIF , false ])) { // To troubleshoot if it is a webp or avif.
324+ $ previewImage = imagecreatefromstring (file_get_contents ($ tmpname ));
325+ $ use_gd_quality = (int )$ this ->config ->getSystemValue ('recognize.preview.quality ' , '100 ' );
326+ if (imagejpeg ($ previewImage , $ tmpname , $ use_gd_quality ) === false ) {
327+ throw new \OCA \Recognize \Exception \Exception ('Could not copy preview file to temp folder ' );
328+ }
329+ }
330+
308331 return $ tmpname ;
309332 }
310333
311- public function cleanUpTmpFiles ():void {
312- $ this ->tempManager ->clean ();
334+ /**
335+ * @param string $path
336+ * @return string
337+ * @throws \OCA\Recognize\Exception\Exception
338+ */
339+ public function generatePrevieWithGD (string $ path ): string {
340+ $ image = imagecreatefromstring (file_get_contents ($ path ));
341+ $ width = imagesx ($ image );
342+ $ height = imagesy ($ image );
343+
344+ $ maxWidth = self ::TEMP_FILE_DIMENSION ;
345+ $ maxHeight = self ::TEMP_FILE_DIMENSION ;
346+
347+ if ($ width > $ maxWidth || $ height > $ maxHeight ) {
348+ $ aspectRatio = $ width / $ height ;
349+ if ($ width > $ height ) {
350+ $ newWidth = $ maxWidth ;
351+ $ newHeight = $ maxWidth / $ aspectRatio ;
352+ } else {
353+ $ newHeight = $ maxHeight ;
354+ $ newWidth = $ maxHeight * $ aspectRatio ;
355+ }
356+ $ previewImage = imagescale ($ image , (int )$ newWidth , (int )$ newHeight );
357+ } else {
358+ return $ path ;
359+ }
360+
361+ // Create a temporary file *with the correct extension*
362+ $ tmpname = $ this ->tempManager ->getTemporaryFile ('.jpg ' );
363+
364+ $ use_gd_quality = (int )$ this ->config ->getSystemValue ('recognize.preview.quality ' , '100 ' );
365+ if (imagejpeg ($ previewImage , $ tmpname , $ use_gd_quality ) === false ) {
366+ throw new \OCA \Recognize \Exception \Exception ('Could not copy preview file to temp folder ' );
367+ }
368+
369+ return $ tmpname ;
313370 }
314371}
0 commit comments