123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class ImageMagickPlugin extends Plugin
- {
- const PLUGIN_VERSION = '2.0.0';
- public $preview_imageformat = 'PNG';
- public $rasterize_vectors = false;
-
- public function onFillImageFileMetadata(ImageFile $imagefile) {
- if (is_null($imagefile->animated) && $imagefile->mimetype === 'image/gif') {
- $magick = new Imagick($imagefile->filepath);
- $magick = $magick->coalesceImages();
- $imagefile->animated = $magick->getNumberImages()>1;
- }
- return true;
- }
- public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box)
- {
- switch ($imagefile->mimetype) {
- case 'image/gif':
-
- if ($imagefile->animated && common_config('thumbnail', 'animated')) {
- return $this->resizeImageFileAnimatedGif($imagefile, $outpath, $box);
- }
- break;
- }
- return true;
- }
- protected function resizeImageFileAnimatedGif(ImageFile $imagefile, $outpath, array $box)
- {
- $magick = new Imagick($imagefile->filepath);
- $magick = $magick->coalesceImages();
- $magick->setIteratorIndex(0);
- do {
- $magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']);
- $magick->thumbnailImage($box['width'], $box['height']);
- $magick->setImagePage($box['width'], $box['height'], 0, 0);
- } while ($magick->nextImage());
- $magick = $magick->deconstructImages();
-
-
- $fh = fopen($outpath, 'w+');
- $success = $magick->writeImagesFile($fh);
- fclose($fh);
- $magick->destroy();
- return !$success;
- }
- public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media=null)
- {
- switch ($file->mimetype) {
- case 'image/svg+xml':
- if (!$this->rasterize_vectors) {
-
- return true;
- }
- break;
- default:
-
- return true;
- }
- $imgPath = tempnam(sys_get_temp_dir(), 'socialthumb-');
- if (!$this->createImagePreview($file, $imgPath)) {
- common_debug('Could not create ImageMagick preview of File id=='.$file->id);
- @unlink($imgPath);
- $imgPath = null;
- return true;
- }
- return false;
- }
- protected function createImagePreview(File $file, $outpath)
- {
- $magick = new Imagick($file->getPath());
- $magick->setImageFormat($this->preview_imageformat);
- $magick->writeImage($outpath);
- $magick->destroy();
- return getimagesize($outpath);
- }
- public function onPluginVersion(array &$versions)
- {
- $versions[] = array('name' => 'ImageMagick',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Mikael Nordfeldth',
- 'homepage' => 'http://gnu.io/social',
- 'rawdescription' =>
-
- _m('Use ImageMagick for some more image support.'));
- return true;
- }
- }
|