summaryrefslogtreecommitdiff
path: root/lib/mediafile.php
diff options
context:
space:
mode:
authorBrion Vibber <brion@status.net>2010-11-08 17:20:04 -0800
committerBrion Vibber <brion@status.net>2010-11-08 17:20:04 -0800
commitc36fecb79431218016615cde45215337d67fee67 (patch)
treedaedc113463f47c3d22410499c89b186d431580c /lib/mediafile.php
parentdc497ed090d94561db195983a4346a2f66503422 (diff)
Save a thumbnail image when uploading an image file into the file attachments system. Currently hardcoded to 100x75, needs aspect-sensitivity etc.
Diffstat (limited to 'lib/mediafile.php')
-rw-r--r--lib/mediafile.php35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/mediafile.php b/lib/mediafile.php
index aad3575d7..2c04b4650 100644
--- a/lib/mediafile.php
+++ b/lib/mediafile.php
@@ -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);