+
+ /**
+ * Animated GIF test, courtesy of frank at huddler dot com et al:
+ * http://php.net/manual/en/function.imagecreatefromgif.php#104473
+ * Modified so avoid landing inside of a header (and thus not matching our regexp).
+ */
+ protected function isAnimatedGif()
+ {
+ if (!($fh = @fopen($this->filepath, 'rb'))) {
+ return false;
+ }
+
+ $count = 0;
+ //an animated gif contains multiple "frames", with each frame having a
+ //header made up of:
+ // * a static 4-byte sequence (\x00\x21\xF9\x04)
+ // * 4 variable bytes
+ // * a static 2-byte sequence (\x00\x2C)
+ // In total the header is maximum 10 bytes.
+
+ // We read through the file til we reach the end of the file, or we've found
+ // at least 2 frame headers
+ while(!feof($fh) && $count < 2) {
+ $chunk = fread($fh, 1024 * 100); //read 100kb at a time
+ $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00\x2C#s', $chunk, $matches);
+ // rewind in case we ended up in the middle of the header, but avoid
+ // infinite loop (i.e. don't rewind if we're already in the end).
+ if (!feof($fh) && ftell($fh) >= 9) {
+ fseek($fh, -9, SEEK_CUR);
+ }
+ }
+
+ fclose($fh);
+ return $count > 1;
+ }
+
+ public function getFileThumbnail($width, $height, $crop, $upscale=false)
+ {
+ if (!$this->fileRecord instanceof File) {
+ throw new ServerException('No File object attached to this ImageFile object.');
+ }
+
+ if ($width === null) {
+ $width = common_config('thumbnail', 'width');
+ $height = common_config('thumbnail', 'height');
+ $crop = common_config('thumbnail', 'crop');
+ }
+
+ if (!$upscale) {
+ if ($width > $this->width) {
+ $width = $this->width;
+ }
+ if (!is_null($height) && $height > $this->height) {
+ $height = $this->height;
+ }
+ }
+
+ if ($height === null) {
+ $height = $width;
+ $crop = true;
+ }
+
+ // 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
+ // which we don't want to do because we like original, untouched data!
+ list($width, $height, $x, $y, $w, $h) = $this->scaleToFit($width, $height, $crop);
+
+ $thumb = File_thumbnail::pkeyGet(array(
+ 'file_id'=> $this->fileRecord->id,
+ 'width' => $width,
+ 'height' => $height,
+ ));
+ if ($thumb instanceof File_thumbnail) {
+ return $thumb;
+ }
+
+ $filename = $this->fileRecord->filehash ?: $this->filename; // Remote files don't have $this->filehash
+ $extension = File::guessMimeExtension($this->mimetype);
+ $outname = "thumb-{$this->fileRecord->id}-{$width}x{$height}-{$filename}." . $extension;
+ $outpath = File_thumbnail::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.');
+ }
+
+ common_debug(sprintf('Generating a thumbnail of File id==%u of size %ux%u', $this->fileRecord->id, $width, $height));
+
+ // Perform resize and store into file
+ $this->resizeTo($outpath, $box);
+
+ try {
+ // Avoid deleting the original
+ if (!in_array($this->getPath(), [File::path($this->filename), File_thumbnail::path($this->filename)])) {
+ $this->unlink();
+ }
+ } catch (FileNotFoundException $e) {
+ // $this->getPath() says the file doesn't exist anyway, so no point in trying to delete it!
+ }
+
+ return File_thumbnail::saveThumbnail($this->fileRecord->getID(),
+ null, // no url since we generated it ourselves and can dynamically generate the url
+ $width, $height,
+ $outname);
+ }