+ 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 < 1) {
+ $width = common_config('thumbnail', 'width');
+ $height = common_config('thumbnail', 'height');
+ $crop = common_config('thumbnail', 'crop');
+ }
+
+ if ($height === null || $height < 1) {
+ $height = $width;
+ $crop = true;
+ }
+
+ // Debug log (convert crop to int to have TRUE being displayed as 1 and FALSE as 0)
+ common_debug('[' . __METHOD__ . ':' . __LINE__ . ']: width=' . $width . ',height=' . $height . ',crop=' . intval($crop));
+
+ // Get proper aspect ratio width and height before lookup
+ // We have to do it through an ImageFile object because of orientation etc.
+ // Only other solution would've been to rotate + rewrite uploaded files.
+ $image = ImageFile::fromFileObject($this);
+ list($width, $height, $x, $y, $w, $h) =
+ $image->scaleToFit($width, $height, $crop);
+
+ $params = array('file_id'=> $this->id,
+ 'width' => $width,
+ 'height' => $height);
+ $thumb = File_thumbnail::pkeyGet($params);
+ if ($thumb instanceof File_thumbnail) {
+ return $thumb;
+ }
+
+ // throws exception on failure to generate thumbnail
+ $outname = "thumb-{$width}x{$height}-" . $this->filename;
+ $outpath = self::path($outname);
+
+ // The boundary box for our resizing
+ $box = array('width'=>$width, 'height'=>$height,
+ 'x'=>$x, 'y'=>$y,
+ 'w'=>$w, 'h'=>$h);
+
+ // Doublecheck that parameters are sane and integers.
+ if ($box['width'] < 1 || $box['width'] > common_config('thumbnail', 'maxsize')
+ || $box['height'] < 1 || $box['height'] > common_config('thumbnail', 'maxsize')
+ || $box['w'] < 1 || $box['x'] >= $this->width
+ || $box['h'] < 1 || $box['y'] >= $this->height) {
+ // Fail on bad width parameter. If this occurs, it's due to algorithm in ImageFile->scaleToFit
+ common_debug("Boundary box parameters for resize of {$this->filepath} : ".var_export($box,true));
+ throw new ServerException('Bad thumbnail size parameters. maxsize=' .
+ common_config('thumbnail', 'maxsize') .
+ ',box[width]=' . $box['width'] .
+ ',box[height]=' . $box['height'] .
+ ',box[w]=' . $box['w'] .
+ ',box[h]=' . $box['h'] .
+ ',box[x]=' . $box['x'] .
+ ',box[y]=' . $box['y'] .
+ ',this->width=' . $this->width .
+ ',this->heigh=' . $this->height .
+ ',this->filepath=' . $this->filepath .
+ ',this->filename=' . $this->filename
+ );
+ }
+
+ // Perform resize and store into file
+ $image->resizeTo($outpath, $box);
+
+ // Avoid deleting the original
+ if ($image->getPath() != $this->getPath()) {
+ $image->unlink();
+ }
+ return File_thumbnail::saveThumbnail($this->id,
+ self::url($outname),
+ $width, $height,
+ $outname);
+ }
+
+ public function getPath()