+ if (intval($this->width) < 1 || intval($this->height) < 1) {
+ // Old files may have 0 until migrated with scripts/upgrade.php
+ // For any legitimately unrepresentable ones, we could generate our
+ // own image (like a square with MIME type in text)
+ throw new UnsupportedMediaException('No image geometry available.');
+ }
+
+ if ($width === null) {
+ $width = common_config('thumbnail', 'width');
+ $height = common_config('thumbnail', 'height');
+ $crop = common_config('thumbnail', 'crop');
+ }
+
+ if ($height === null) {
+ $height = $width;
+ $crop = true;
+ }
+
+ // Get proper aspect ratio width and height before lookup
+ list($width, $height, $x, $y, $w2, $h2) =
+ ImageFile::getScalingValues($this->width, $this->height, $width, $height, $crop);
+
+ // Doublecheck that parameters are sane and integers.
+ if ($width < 1 || $width > common_config('thumbnail', 'maxsize')
+ || $height < 1 || $height > common_config('thumbnail', 'maxsize')) {
+ // Fail on bad width parameter. If this occurs, it's due to algorithm in ImageFile::getScalingValues
+ throw new ServerException('Bad thumbnail size parameters.');
+ }
+
+ $params = array('file_id'=> $this->id,
+ 'width' => $width,
+ 'height' => $height);
+ $thumb = File_thumbnail::pkeyGet($params);
+ if ($thumb === null) {
+ // throws exception on failure to generate thumbnail
+ $thumb = $this->generateThumbnail($width, $height, $crop);
+ }
+ return $thumb;
+ }
+
+ /**
+ * Generate and store a thumbnail image for the uploaded file, if applicable.
+ * Call this only if you know what you're doing.
+ *
+ * @param $width int Maximum thumbnail width in pixels
+ * @param $height int Maximum thumbnail height in pixels, if null, crop to $width
+ *
+ * @return File_thumbnail or null
+ */
+ protected function generateThumbnail($width, $height, $crop)
+ {
+ $width = intval($width);
+ if ($height === null) {
+ $height = $width;
+ $crop = true;
+ }
+
+ $image = ImageFile::fromFileObject($this);
+
+ list($width, $height, $x, $y, $w2, $h2) =
+ $image->scaleToFit($width, $height, $crop);
+
+ $outname = "thumb-{$width}x{$height}-" . $this->filename;
+ $outpath = self::path($outname);
+
+ $image->resizeTo($outpath, $width, $height, $x, $y, $w2, $h2);
+
+ // Avoid deleting the original
+ if ($image->getPath() != $this->getPath()) {
+ $image->unlink();
+ }
+ return File_thumbnail::saveThumbnail($this->id,
+ self::url($outname),
+ $width, $height);
+ }
+
+ public function getPath()
+ {
+ return self::path($this->filename);
+ }
+ public function getUrl()
+ {
+ return $this->url;
+ }
+
+ /**
+ * Blow the cache of notices that link to this URL
+ *
+ * @param boolean $last Whether to blow the "last" cache too
+ *
+ * @return void
+ */
+
+ function blowCache($last=false)
+ {
+ self::blow('file:notice-ids:%s', $this->url);
+ if ($last) {
+ self::blow('file:notice-ids:%s;last', $this->url);
+ }
+ self::blow('file:notice-count:%d', $this->id);
+ }
+
+ /**
+ * Stream of notices linking to this URL
+ *
+ * @param integer $offset Offset to show; default is 0
+ * @param integer $limit Limit of notices to show
+ * @param integer $since_id Since this notice
+ * @param integer $max_id Before this notice
+ *
+ * @return array ids of notices that link to this file
+ */
+
+ function stream($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
+ {
+ $stream = new FileNoticeStream($this);
+ return $stream->getNotices($offset, $limit, $since_id, $max_id);
+ }
+
+ function noticeCount()
+ {
+ $cacheKey = sprintf('file:notice-count:%d', $this->id);
+
+ $count = self::cacheGet($cacheKey);
+
+ if ($count === false) {
+
+ $f2p = new File_to_post();
+
+ $f2p->file_id = $this->id;
+
+ $count = $f2p->count();
+
+ self::cacheSet($cacheKey, $count);
+ }
+
+ return $count;