From: Mikael Nordfeldth Date: Mon, 21 Apr 2014 09:35:42 +0000 (+0200) Subject: Preparing File for dynamic thumbnail generation. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b3bf036975593db27c6e7e653f18160d086f7b2d;p=quix0rs-gnu-social.git Preparing File for dynamic thumbnail generation. --- diff --git a/actions/attachment.php b/actions/attachment.php index b532b81d88..30499c15f4 100644 --- a/actions/attachment.php +++ b/actions/attachment.php @@ -130,14 +130,11 @@ class AttachmentAction extends Action parent::handle(); if (empty($this->attachment->filename)) { - // if it's not a local file, gtfo - common_redirect($this->attachment->url, 303); - - } else { - $this->showPage(); } + + $this->showPage(); } /** diff --git a/actions/attachment_thumbnail.php b/actions/attachment_thumbnail.php index 93df840d56..6e8baeee7a 100644 --- a/actions/attachment_thumbnail.php +++ b/actions/attachment_thumbnail.php @@ -40,9 +40,18 @@ if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); } */ class Attachment_thumbnailAction extends AttachmentAction { - protected function handle() + protected $thumb_w = null; // max width + protected $thumb_h = null; // max height + protected $thumb_a = null; // "aspect ratio" (more like "square", 1 or 0) + + protected function prepare(array $args=array()) { - $this->showPage(); + parent::prepare($args); + + foreach (array('w', 'h', 'a') as $attr) { + $this->{"thumb_$attr"} = $this->arg($attr); + } + return true; } /** @@ -67,10 +76,8 @@ class Attachment_thumbnailAction extends AttachmentAction */ function showCore() { - $file_thumbnail = File_thumbnail::getKV('file_id', $this->attachment->id); - if (empty($file_thumbnail->url)) { - return; - } - $this->element('img', array('src' => $file_thumbnail->url, 'alt' => 'Thumbnail')); + // Returns a File_thumbnail object or throws exception if not available + $thumbnail = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_a); + $this->element('img', array('src' => $thumbnail->getUrl(), 'alt' => 'Thumbnail')); } } diff --git a/classes/File.php b/classes/File.php index f58a75d9d3..7748fe83f9 100644 --- a/classes/File.php +++ b/classes/File.php @@ -24,9 +24,6 @@ if (!defined('GNUSOCIAL')) { exit(1); } */ class File extends Managed_DataObject { - ###START_AUTOCODE - /* the code below is auto generated do not remove the above tag */ - public $__table = 'file'; // table name public $id; // int(4) primary_key not_null public $url; // varchar(255) unique_key @@ -38,9 +35,6 @@ class File extends Managed_DataObject public $filename; // varchar(255) public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP - /* the code above is auto generated do not remove the tag below */ - ###END_AUTOCODE - public static function schemaDef() { return array( @@ -439,11 +433,29 @@ class File extends Managed_DataObject /** * Get the attachment's thumbnail record, if any. * + * @param $width int Max width of thumbnail in pixels + * @param $height int Max height of thumbnail in pixels. If null, set to $width + * * @return File_thumbnail */ - function getThumbnail() + public function getThumbnail($width=null, $height=null) { - return File_thumbnail::getKV('file_id', $this->id); + if ($width === null) { + $width = common_config('attachments', 'thumb_width'); + $height = common_config('attachments', 'thumb_height'); + $square = common_config('attachments', 'thumb_square'); + } elseif ($height === null) { + $square = true; + } + + $params = array('file_id'=> $this->id, + 'width' => $width, + 'height' => $square ? $width : $height); + $thumb = File_thumbnail::pkeyGet($params); + if ($thumb === null) { + // generate a new thumbnail for desired parameters + } + return $thumb; } public function getPath()