]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Save a thumbnail image when uploading an image file into the file attachments system...
authorBrion Vibber <brion@status.net>
Tue, 9 Nov 2010 01:20:04 +0000 (17:20 -0800)
committerBrion Vibber <brion@status.net>
Tue, 9 Nov 2010 01:20:04 +0000 (17:20 -0800)
classes/File_thumbnail.php
lib/mediafile.php

index edae8ac21a9153119de458a166f39f47a8f12ec2..d371b9e8aa2f783871c147408978edf79ac96593 100644 (file)
@@ -48,12 +48,34 @@ class File_thumbnail extends Memcached_DataObject
         return array(false, false, false);
     }
 
-    function saveNew($data, $file_id) {
+    /**
+     * Save oEmbed-provided thumbnail data
+     *
+     * @param object $data
+     * @param int $file_id
+     */
+    public static function saveNew($data, $file_id) {
+        self::saveThumbnail($file_id,
+                            $data->thumbnail_url,
+                            $data->thumbnail_width,
+                            $data->thumbnail_height);
+    }
+
+    /**
+     * Save a thumbnail record for the referenced file record.
+     *
+     * @param int $file_id
+     * @param string $url
+     * @param int $width
+     * @param int $height
+     */
+    static function saveThumbnail($file_id, $url, $width, $height)
+    {
         $tn = new File_thumbnail;
         $tn->file_id = $file_id;
-        $tn->url = $data->thumbnail_url;
-        $tn->width = intval($data->thumbnail_width);
-        $tn->height = intval($data->thumbnail_height);
+        $tn->url = $url;
+        $tn->width = intval($width);
+        $tn->height = intval($height);
         $tn->insert();
     }
 }
index aad3575d729a900f0faa2102e9c13e234873864a..2c04b46501652cb42196663480f46986542c5964 100644 (file)
@@ -48,11 +48,14 @@ class MediaFile
     {
         if ($user == null) {
             $this->user = common_current_user();
+        } else {
+            $this->user = $user;
         }
 
         $this->filename   = $filename;
         $this->mimetype   = $mimetype;
         $this->fileRecord = $this->storeFile();
+        $this->thumbnailRecord = $this->storeThumbnail();
 
         $this->fileurl = common_local_url('attachment',
                                     array('attachment' => $this->fileRecord->id));
@@ -102,6 +105,38 @@ class MediaFile
         return $file;
     }
 
+    /**
+     * Generate and store a thumbnail image for the uploaded file, if applicable.
+     *
+     * @return File_thumbnail or null
+     */
+    function storeThumbnail()
+    {
+        if (substr($this->mimetype, 0, strlen('image/')) != 'image/') {
+            // @fixme video thumbs would be nice!
+            return null;
+        }
+        try {
+            $image = new ImageFile($this->fileRecord->id,
+                                   File::path($this->filename));
+        } catch (Exception $e) {
+            // Unsupported image type.
+            return null;
+        }
+
+        $outname = File::filename($this->user->getProfile(), 'thumb-' . $this->filename, $this->mimetype);
+        $outpath = File::path($outname);
+
+        $width = 100;
+        $height = 75;
+
+        $image->resizeTo($outpath, $width, $height);
+        File_thumbnail::saveThumbnail($this->fileRecord->id,
+                                      File::url($outname),
+                                      $width,
+                                      $height);
+    }
+
     function rememberFile($file, $short)
     {
         $this->maybeAddRedir($file->id, $short);