]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/imagefile.php
Merge remote-tracking branch 'upstream/nightly' into nightly
[quix0rs-gnu-social.git] / lib / imagefile.php
index 71074877374e4dc38feaeed032948501846b4596..b0cd13089f64e1579d456473b6601a09801f31cb 100644 (file)
@@ -93,9 +93,9 @@ class ImageFile
         $this->type     = $info[2];
         $this->mimetype = $info['mime'];
 
-        if ($this->type == IMAGETYPE_JPEG && function_exists('exif_read_data')) {
+        if ($this->type === IMAGETYPE_JPEG && function_exists('exif_read_data')) {
             // Orientation value to rotate thumbnails properly
-            $exif = exif_read_data($this->filepath);
+            $exif = @exif_read_data($this->filepath);
             if (is_array($exif) && isset($exif['Orientation'])) {
                 switch ((int)$exif['Orientation']) {
                 case 1: // top is top
@@ -132,7 +132,7 @@ class ImageFile
             // First some mimetype specific exceptions
             switch ($file->mimetype) {
             case 'image/svg+xml':
-                throw new UseFileAsThumbnailException($file->id);
+                throw new UseFileAsThumbnailException($file);
             }
 
             // And we'll only consider it an image if it has such a media type
@@ -150,11 +150,17 @@ class ImageFile
         }
 
         try {
-            $image = new ImageFile($file->id, $imgPath);
+            $image = new ImageFile($file->getID(), $imgPath);
         } catch (UnsupportedMediaException $e) {
             // Avoid deleting the original
-            if ($imgPath != $file->getPath()) {
-                unlink($imgPath);
+            try {
+                if ($imgPath !== $file->getPath()) {
+                    @unlink($imgPath);
+                }
+            } catch (FileNotFoundException $e) {
+                // File reported (via getPath) that the original file
+                // doesn't exist anyway, so it's safe to delete $imgPath
+                @unlink($imgPath);
             }
             throw $e;
         }
@@ -276,7 +282,11 @@ class ImageFile
         }
 
         if (!file_exists($outpath)) {
-            throw new UseFileAsThumbnailException($this->id);
+            if ($this->fileRecord instanceof File) {
+                throw new UseFileAsThumbnailException($this->fileRecord);
+            } else {
+                throw new UnsupportedMediaException('No local File object exists for ImageFile.');
+            }
         }
 
         return $outpath;
@@ -538,10 +548,10 @@ class ImageFile
         }
 
         fclose($fh);
-        return $count > 1;
+        return $count >= 1; // number of animated frames apart from the original image
     }
 
-    public function getFileThumbnail($width, $height, $crop)
+    public function getFileThumbnail($width, $height, $crop, $upscale=false)
     {
         if (!$this->fileRecord instanceof File) {
             throw new ServerException('No File object attached to this ImageFile object.');
@@ -553,6 +563,15 @@ class ImageFile
             $crop = common_config('thumbnail', 'crop');
         }
 
+        if (!$upscale) {
+            if ($width > $this->width) {
+                $width = $this->width;
+            }
+            if (!is_null($height) && $height > $this->height) {
+                $height = $this->height;
+            }
+        }
+
         if ($height === null) {
             $height = $width;
             $crop = true;
@@ -598,12 +617,17 @@ class ImageFile
         // Perform resize and store into file
         $this->resizeTo($outpath, $box);
 
-        // Avoid deleting the original
-        if ($this->getPath() != File_thumbnail::path($this->filename)) {
-            $this->unlink();
+        try {
+            // Avoid deleting the original
+            if (!in_array($this->getPath(), [File::path($this->filename), File_thumbnail::path($this->filename)])) {
+                $this->unlink();
+            }
+        } catch (FileNotFoundException $e) {
+            // $this->getPath() says the file doesn't exist anyway, so no point in trying to delete it!
         }
-        return File_thumbnail::saveThumbnail($this->fileRecord->id,
-                                      File_thumbnail::url($outname),
+
+        return File_thumbnail::saveThumbnail($this->fileRecord->getID(),
+                                      null, // no url since we generated it ourselves and can dynamically generate the url
                                       $width, $height,
                                       $outname);
     }