href: link to the profile
text: text for the profile
image: mini image for the profile
+
+CreateFileImageThumbnail: Hook to create image thumbnail source from a File
+- $file: MediaFile object with related metadata
+- &$imgPath: Path to image file which can be used as source for our thumbnail algorithm.
+- $media: MIME media type ('image', 'video', 'audio' etc.)
return File_thumbnail::getKV('file_id', $this->id);
}
+ public function getPath()
+ {
+ return self::path($this->filename);
+ }
+
/**
* Blow the cache of notices that link to this URL
*
/**
* Save a thumbnail record for the referenced file record.
*
+ * FIXME: Add error handling
+ *
* @param int $file_id
* @param string $url
* @param int $width
($info[2] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')))) {
// TRANS: Exception thrown when trying to upload an unsupported image file format.
- throw new Exception(_('Unsupported image file format.'));
- return;
+ throw new UnsupportedMediaException(_('Unsupported image format.'), $this->filepath);
}
$this->type = ($info) ? $info[2]:$type;
$this->height = ($info) ? $info[1]:$height;
}
+ public function getPath()
+ {
+ if (!file_exists($this->filepath)) {
+ throw new ServerException('No file in ImageFile filepath');
+ }
+
+ return $this->filepath;
+ }
+
static function fromUpload($param='upload')
{
switch ($_FILES[$param]['error']) {
case UPLOAD_ERR_OK: // success, jump out
break;
+
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
// TRANS: Exception thrown when too large a file is uploaded.
// TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB".
- throw new Exception(sprintf(_('That file is too big. The maximum file size is %s.'),
- ImageFile::maxFileSize()));
- return;
+ throw new Exception(sprintf(_('That file is too big. The maximum file size is %s.'), ImageFile::maxFileSize()));
+
case UPLOAD_ERR_PARTIAL:
@unlink($_FILES[$param]['tmp_name']);
// TRANS: Exception thrown when uploading an image and that action could not be completed.
throw new Exception(_('Partial upload.'));
- return;
+
case UPLOAD_ERR_NO_FILE:
// No file; probably just a non-AJAX submission.
- return;
default:
- common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
- $_FILES[$param]['error']);
+ common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . $_FILES[$param]['error']);
// TRANS: Exception thrown when uploading an image fails for an unknown reason.
throw new Exception(_('System error uploading file.'));
- return;
}
$info = @getimagesize($_FILES[$param]['tmp_name']);
if (!$info) {
@unlink($_FILES[$param]['tmp_name']);
// TRANS: Exception thrown when uploading a file as image that is not an image or is a corrupt file.
- throw new Exception(_('Not an image or corrupt file.'));
- return;
+ throw new UnsupportedMediaException(_('Not an image or corrupt file.'), '[deleted]');
}
return new ImageFile(null, $_FILES[$param]['tmp_name']);
if (!file_exists($this->filepath)) {
// TRANS: Exception thrown during resize when image has been registered as present, but is no longer there.
throw new Exception(_('Lost our file.'));
- return;
}
// Don't crop/scale if it isn't necessary
default:
// TRANS: Exception thrown when trying to resize an unknown file type.
throw new Exception(_('Unknown file type'));
- return;
}
$image_dest = imagecreatetruecolor($width, $height);
default:
// TRANS: Exception thrown when trying resize an unknown file type.
throw new Exception(_('Unknown file type'));
- return;
}
imagedestroy($image_src);
var $fileurl = null;
var $short_fileurl = null;
var $mimetype = null;
+ var $thumbnailRecord = null;
function __construct(Profile $scoped, $filename = null, $mimetype = null)
{
$this->filename = $filename;
$this->mimetype = $mimetype;
$this->fileRecord = $this->storeFile();
- $this->thumbnailRecord = $this->storeThumbnail();
+ try {
+ $this->thumbnailRecord = $this->storeThumbnail();
+ } catch (UnsupportedMediaException $e) {
+ // FIXME: Add "unknown media" icon or something
+ $this->thumbnailRecord = null;
+ }
$this->fileurl = common_local_url('attachment',
array('attachment' => $this->fileRecord->id));
common_local_url('file', array('notice' => $notice->id)));
}
+ public function getPath()
+ {
+ return File::path($this->filename);
+ }
+
function shortUrl()
{
return $this->short_fileurl;
*/
function storeThumbnail()
{
- if (substr($this->mimetype, 0, strlen('image/')) != 'image/') {
- // @fixme video thumbs would be nice!
- return null;
+ $imgPath = null;
+ $media = common_get_mime_media($this->mimetype);
+
+ if (Event::handle('CreateFileImageThumbnail', array($this, &$imgPath, $media))) {
+ switch ($media) {
+ case 'image':
+ $imgPath = $this->getPath();
+ break;
+ default:
+ throw new UnsupportedMediaException(_('Unsupported media format.'), $this->getPath());
+ }
+ }
+ if (!file_exists($imgPath)) {
+ throw new ServerException(sprintf('Thumbnail source is not stored locally: %s', $imgPath));
}
+
try {
- $image = new ImageFile($this->fileRecord->id,
- File::path($this->filename));
- } catch (Exception $e) {
- // Unsupported image type.
- return null;
+ $image = new ImageFile($this->fileRecord->id, $imgPath);
+ } catch (UnsupportedMediaException $e) {
+ // Avoid deleting the original
+ if ($image->getPath() != $this->getPath()) {
+ $image->unlink();
+ }
+ throw $e;
}
$outname = File::filename($this->scoped, 'thumb-' . $this->filename, $this->mimetype);
list($width, $height) = $this->scaleToFit($image->width, $image->height, $maxWidth, $maxHeight);
$image->resizeTo($outpath, $width, $height);
- File_thumbnail::saveThumbnail($this->fileRecord->id,
+
+ // Avoid deleting the original
+ if ($image->getPath() != $this->getPath()) {
+ $image->unlink();
+ }
+ return File_thumbnail::saveThumbnail($this->fileRecord->id,
File::url($outname),
$width,
$height);
// TRANS: Client exception thrown when an uploaded file is larger than set in php.ini.
throw new ClientException(_('The uploaded file exceeds the ' .
'upload_max_filesize directive in php.ini.'));
- return;
case UPLOAD_ERR_FORM_SIZE:
throw new ClientException(
// TRANS: Client exception.
_('The uploaded file exceeds the MAX_FILE_SIZE directive' .
' that was specified in the HTML form.'));
- return;
case UPLOAD_ERR_PARTIAL:
@unlink($_FILES[$param]['tmp_name']);
// TRANS: Client exception.
throw new ClientException(_('The uploaded file was only' .
' partially uploaded.'));
- return;
case UPLOAD_ERR_NO_FILE:
// No file; probably just a non-AJAX submission.
return;
case UPLOAD_ERR_NO_TMP_DIR:
// TRANS: Client exception thrown when a temporary folder is not present to store a file upload.
throw new ClientException(_('Missing a temporary folder.'));
- return;
case UPLOAD_ERR_CANT_WRITE:
// TRANS: Client exception thrown when writing to disk is not possible during a file upload operation.
throw new ClientException(_('Failed to write file to disk.'));
- return;
case UPLOAD_ERR_EXTENSION:
// TRANS: Client exception thrown when a file upload operation has been stopped by an extension.
throw new ClientException(_('File upload stopped by extension.'));
- return;
default:
common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
$_FILES[$param]['error']);
// TRANS: Client exception thrown when a file upload operation has failed with an unknown reason.
throw new ClientException(_('System error uploading file.'));
- return;
}
// Throws exception if additional size does not respect quota
--- /dev/null
+<?php
+/**
+ * GNU social - a federating social network
+ *
+ * class for an exception when a File is of unsupported media type
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category Exception
+ * @package GNUsocial
+ * @author Mikael Nordfeldth <mmn@hethane.se>
+ * @copyright 2014 Free Software Foundation, Inc.
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
+ * @link https://www.gnu.org/software/social/
+ */
+
+if (!defined('GNUSOCIAL')) { exit(1); }
+
+class UnsupportedMediaException extends ServerException
+{
+ public function __construct($msg, $path)
+ {
+ common_debug(sprintf('UnsupportedMediaException "%1$s" for file "%2$s"', $msg, $path));
+ parent::__construct($msg);
+ }
+}
--- /dev/null
+<?php
+/**
+ * GNU social - a federating social network
+ *
+ * Plugin to make thumbnails of video files with ffmpeg
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category Plugin
+ * @package GNUsocial
+ * @author Mikael Nordfeldth <mmn@hethane.se>
+ * @copyright 2014 Free Software Foundation http://fsf.org
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link https://www.gnu.org/software/social/
+ */
+
+if (!defined('GNUSOCIAL')) { exit(1); }
+
+/*
+ * Dependencies:
+ * php5-ffmpeg
+ * php5-gd
+ *
+ * Video support will depend on your ffmpeg.
+ */
+
+class VideoThumbnailsPlugin extends Plugin
+{
+ /*
+ * This function should only extract an image from the video stream
+ * and disregard any scaling aspects in the resulting file, since
+ * this will be handled in the core thumbnail algorithm.
+ */
+ public function onCreateFileImageThumbnail(MediaFile $file, &$imgPath, $media=null)
+ {
+ // This might accidentally pass application/ogg videos.
+ // If that's a problem, let's fix it in the calling function.
+ if ($media !== 'video') {
+ return true;
+ }
+
+ $movie = new ffmpeg_movie($file->getPath(), false);
+
+ $frames = $movie->getFrameCount();
+ if ($frames > 0) {
+ $frame = $movie->getFrame(floor($frames/2));
+ } else {
+ $frame = $movie->getNextKeyFrame();
+ }
+
+ // We failed to get a frame.
+ if ($frame === null) {
+ return true;
+ }
+
+ // Let's save our frame to a temporary file. If we fail, remove it.
+ $imgPath = tempnam(sys_get_temp_dir(), 'socialthumb');
+ if (!imagejpeg($frame->toGDImage(), $imgPath)) {
+ @unlink($imgPath);
+ return true;
+ }
+ return false;
+ }
+
+ public function onPluginVersion(&$versions)
+ {
+ $versions[] = array('name' => 'Video Thumbnails',
+ 'version' => GNUSOCIAL_VERSION,
+ 'author' => 'Mikael Nordfeldth',
+ 'homepage' => 'https://www.gnu.org/software/social/',
+ 'rawdescription' =>
+ // TRANS: Plugin description.
+ _m('HTML5 media attachment support'));
+ return true;
+ }
+}
+