]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/ImageMagick/ImageMagickPlugin.php
Plugins didn't match lib/plugin.php onPluginVersion function definition
[quix0rs-gnu-social.git] / plugins / ImageMagick / ImageMagickPlugin.php
index e6f2e22080e82f4fb87ab8cbdcfda5fc0f6e0c62..927445211cee75057d69d222c84ebb845e87bcbd 100644 (file)
@@ -46,6 +46,9 @@ if (!defined('GNUSOCIAL')) { exit(1); }
 
 class ImageMagickPlugin extends Plugin
 {
+    public $preview_imageformat = 'PNG';    // Image format strings: http://www.imagemagick.org/script/formats.php#supported
+    public $rasterize_vectors = false;       // Whether we want to turn SVG into PNG etc.
+
     /**
      * @param ImageFile $file An ImageFile object we're getting metadata for
      * @param array $info The response from getimagesize()
@@ -90,11 +93,46 @@ class ImageMagickPlugin extends Plugin
         $fh = fopen($outpath, 'w+');
         $success = $magick->writeImagesFile($fh);
         fclose($fh);
+        $magick->destroy();
 
         return !$success;
     }
 
-    public function onPluginVersion(&$versions)
+    public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media=null)
+    {
+        switch ($file->mimetype) {
+        case 'image/svg+xml':
+            if (!$this->rasterize_vectors) {
+                // ImageMagick seems to be hard to trick into scaling vector graphics...
+                return true;
+            }
+            break;
+        default:
+            // If we don't know the format, let's try not to mess with anything.
+            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);  // Verify that we wrote an understandable image.
+    }
+
+    public function onPluginVersion(array &$versions)
     {
         $versions[] = array('name' => 'ImageMagick',
                             'version' => GNUSOCIAL_VERSION,