]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/imagefile.php
rename config option site/logdebug to log/debugtrace
[quix0rs-gnu-social.git] / lib / imagefile.php
index 26663eed7b14d8fafb6b9ce8750576783da8539c..a328df9852bd965f33e0d0107021505f61c56281 100644 (file)
@@ -61,11 +61,16 @@ class ImageFile
     {
         $this->id = $id;
         if (!empty($this->id)) {
-            $this->fileRecord = File::getKV('id', $this->id);
-            if (!$this->fileRecord instanceof File) {
-                throw new ServerException('Expected File object did not exist.');
+            $this->fileRecord = new File();
+            $this->fileRecord->id = $this->id;
+            if (!$this->fileRecord->find(true)) {
+                // If we have set an ID, we need that ID to exist!
+                throw new NoResultException($this->fileRecord);
             }
         }
+
+        // These do not have to be the same as fileRecord->filename for example,
+        // since we may have generated an image source file from something else!
         $this->filepath = $filepath;
         $this->filename = basename($filepath);
 
@@ -90,7 +95,7 @@ class ImageFile
 
         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
@@ -120,9 +125,17 @@ class ImageFile
         $imgPath = null;
         $media = common_get_mime_media($file->mimetype);
         if (Event::handle('CreateFileImageThumbnailSource', array($file, &$imgPath, $media))) {
-            if (empty($file->filename)) {
+            if (empty($file->filename) && !file_exists($imgPath)) {
                 throw new UnsupportedMediaException(_('File without filename could not get a thumbnail source.'));
             }
+
+            // First some mimetype specific exceptions
+            switch ($file->mimetype) {
+            case 'image/svg+xml':
+                throw new UseFileAsThumbnailException($file->id);
+            }
+
+            // And we'll only consider it an image if it has such a media type
             switch ($media) {
             case 'image':
                 $imgPath = $file->getPath();
@@ -137,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;
         }
@@ -176,6 +195,8 @@ class ImageFile
 
          case UPLOAD_ERR_NO_FILE:
             // No file; probably just a non-AJAX submission.
+            throw new ClientException(_('No file uploaded.'));
+
          default:
             common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . $_FILES[$param]['error']);
             // TRANS: Exception thrown when uploading an image fails for an unknown reason.
@@ -193,31 +214,6 @@ class ImageFile
         return new ImageFile(null, $_FILES[$param]['tmp_name']);
     }
 
-    /**
-     * Compat interface for old code generating avatar thumbnails...
-     * Saves the scaled file directly into the avatar area.
-     *
-     * @param int $size target width & height -- must be square
-     * @param int $x (default 0) upper-left corner to crop from
-     * @param int $y (default 0) upper-left corner to crop from
-     * @param int $w (default full) width of image area to crop
-     * @param int $h (default full) height of image area to crop
-     * @return string filename
-     */
-    function resize($size, $x = 0, $y = 0, $w = null, $h = null)
-    {
-        $targetType = $this->preferredType();
-        $outname = Avatar::filename($this->id,
-                                    image_type_to_extension($targetType),
-                                    $size,
-                                    common_timestamp());
-        $outpath = Avatar::path($outname);
-        $this->resizeTo($outpath, array('width'=>$size, 'height'=>$size,
-                                        'x'=>$x,        'y'=>$y,
-                                        'w'=>$w,        'h'=>$h));
-        return $outname;
-    }
-
     /**
      * Copy the image file to the given destination.
      *
@@ -551,7 +547,7 @@ class ImageFile
         return $count > 1;
     }
 
-    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.');
@@ -563,6 +559,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;
@@ -608,12 +613,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);
     }