+ public function getThumbnail($width=null, $height=null, $crop=false)
+ {
+ 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()