X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fmediafile.php;h=caa902de5dfeb13c18413372769198b9af209241;hb=97af5e195421591265094685d9fa86af8d464794;hp=aad3575d729a900f0faa2102e9c13e234873864a;hpb=5b94d9e86b91707a47b143482775acad8fcf9e78;p=quix0rs-gnu-social.git diff --git a/lib/mediafile.php b/lib/mediafile.php index aad3575d72..caa902de5d 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,52 @@ 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); + + $maxWidth = common_config('attachments', 'thumb_width'); + $maxHeight = common_config('attachments', 'thumb_height'); + list($width, $height) = $this->scaleToFit($image->width, $image->height, $maxWidth, $maxHeight); + + $image->resizeTo($outpath, $width, $height); + File_thumbnail::saveThumbnail($this->fileRecord->id, + File::url($outname), + $width, + $height); + } + + function scaleToFit($width, $height, $maxWidth, $maxHeight) + { + $aspect = $maxWidth / $maxHeight; + $w1 = $maxWidth; + $h1 = intval($height * $maxWidth / $width); + if ($h1 > $maxHeight) { + $w2 = intval($width * $maxHeight / $height); + $h2 = $maxHeight; + return array($w2, $h2); + } + return array($w1, $h1); + } + function rememberFile($file, $short) { $this->maybeAddRedir($file->id, $short); @@ -313,7 +362,9 @@ class MediaFile // we'll try detecting a type from its extension... $unclearTypes = array('application/octet-stream', 'application/vnd.ms-office', - 'application/zip'); + 'application/zip', + // TODO: for XML we could do better content-based sniffing too + 'text/xml'); if ($originalFilename && (!$filetype || in_array($filetype, $unclearTypes))) { $type = $mte->getMIMEType($originalFilename);