protected function storeRemoteFileThumbnail(File_thumbnail $thumbnail) { if (!empty($thumbnail->filename) && file_exists($thumbnail->getPath())) { throw new AlreadyFulfilledException(sprintf('A thumbnail seems to already exist for remote file with id==%u', $thumbnail->file_id)); } $url = $thumbnail->getUrl(); $this->checkWhitelist($url); // First we download the file to memory and test whether it's actually an image file // FIXME: To support remote video/whatever files, this needs reworking. common_debug(sprintf('Downloading remote thumbnail for file id==%u with thumbnail URL: %s', $thumbnail->file_id, $url)); $imgData = HTTPClient::quickGet($url); $info = @getimagesizefromstring($imgData); if ($info === false) { throw new UnsupportedMediaException(_('Remote file format was not identified as an image.'), $url); } elseif (!$info[0] || !$info[1]) { throw new UnsupportedMediaException(_('Image file had impossible geometry (0 width or height)')); } // We'll trust sha256 not to have collision issues any time soon :) $filename = hash('sha256', $imgData) . '.' . common_supported_mime_to_ext($info['mime']); $fullpath = File_thumbnail::path($filename); // Write the file to disk. Throw Exception on failure if (!file_exists($fullpath) && file_put_contents($fullpath, $imgData) === false) { throw new ServerException(_('Could not write downloaded file to disk.')); } // Get rid of the file from memory unset($imgData); // Updated our database for the file record $orig = clone $thumbnail; $thumbnail->filename = $filename; $thumbnail->width = $info[0]; // array indexes documented on php.net: $thumbnail->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php // Throws exception on failure. $thumbnail->updateWithKeys($orig, 'file_id'); }
public function delete($useWhere = false) { if (!empty($this->filename) && file_exists(File_thumbnail::path($this->filename))) { $deleted = @unlink(self::path($this->filename)); if (!$deleted) { common_log(LOG_ERR, sprintf('Could not unlink existing file: "%s"', self::path($this->filename))); } } return parent::delete($useWhere); }
public function getFileThumbnail($width, $height, $crop) { 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 ($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); // Avoid deleting the original if ($this->getPath() != File_thumbnail::path($this->filename)) { $this->unlink(); } return File_thumbnail::saveThumbnail($this->fileRecord->id, File_thumbnail::url($outname), $width, $height, $outname); }