Răsfoiți Sursa

#248 Intervention Image v.2.2.2

Alex Trofimov 11 ani în urmă
părinte
comite
c9a9731bdd

+ 37 - 0
plugins/intervention-image/Intervention/Image/AbstractDecoder.php

@@ -70,6 +70,27 @@ abstract class AbstractDecoder
         );
     }
 
+    /**
+     * Init from given stream
+     *
+     * @param $stream
+     * @return \Intervention\Image\Image
+     */
+    public function initFromStream($stream)
+    {
+        $offset = ftell($stream);
+        rewind($stream);
+        $data = @stream_get_contents($stream);
+        fseek($stream, $offset);
+        if ($data) {
+            return $this->initFromBinary($data);
+        }
+
+        throw new \Intervention\Image\Exception\NotReadableException(
+            "Unable to init from given stream"
+        );
+    }
+
     /**
      * Determines if current source data is GD resource
      *
@@ -148,6 +169,19 @@ abstract class AbstractDecoder
         return (bool) filter_var($this->data, FILTER_VALIDATE_URL);
     }
 
+    /**
+     * Determines if current source data is a stream resource
+     *
+     * @return boolean
+     */
+    public function isStream()
+    {
+        if (!is_resource($this->data)) return false;
+        if (get_resource_type($this->data) !== 'stream') return false;
+
+        return true;
+    }
+
     /**
      * Determines if current source data is binary data
      *
@@ -244,6 +278,9 @@ abstract class AbstractDecoder
             case $this->isUrl():
                 return $this->initFromUrl($this->data);
 
+            case $this->isStream():
+                return $this->initFromStream($this->data);
+
             case $this->isFilePath():
                 return $this->initFromPath($this->data);
 

+ 3 - 3
plugins/intervention-image/Intervention/Image/Commands/IptcCommand.php

@@ -33,7 +33,7 @@ class IptcCommand extends AbstractCommand
                 $data['Urgency'] = isset($iptc["2#010"][0]) ? $iptc["2#010"][0] : null;
                 $data['Category'] = isset($iptc["2#015"][0]) ? $iptc["2#015"][0] : null;
                 $data['Subcategories'] = isset($iptc["2#020"][0]) ? $iptc["2#020"][0] : null;
-                $data['Keywords'] = isset($iptc["2#025"][0]) ? $iptc["2#025"][0] : null;
+                $data['Keywords'] = isset($iptc["2#025"][0]) ? $iptc["2#025"] : null;
                 $data['SpecialInstructions'] = isset($iptc["2#040"][0]) ? $iptc["2#040"][0] : null;
                 $data['CreationDate'] = isset($iptc["2#055"][0]) ? $iptc["2#055"][0] : null;
                 $data['AuthorByline'] = isset($iptc["2#080"][0]) ? $iptc["2#080"][0] : null;
@@ -47,7 +47,7 @@ class IptcCommand extends AbstractCommand
                 $data['PhotoSource'] = isset($iptc["2#115"][0]) ? $iptc["2#115"][0] : null;
                 $data['Copyright'] = isset($iptc["2#116"][0]) ? $iptc["2#116"][0] : null;
                 $data['Caption'] = isset($iptc["2#120"][0]) ? $iptc["2#120"][0] : null;
-                $data['CaptionWriter'] = isset($iptc["2#120"][0]) ? $iptc["2#122"][0] : null;
+                $data['CaptionWriter'] = isset($iptc["2#122"][0]) ? $iptc["2#122"][0] : null;
             }
         }
 
@@ -59,4 +59,4 @@ class IptcCommand extends AbstractCommand
 
         return true;
     }
-}
+}

+ 4 - 0
plugins/intervention-image/Intervention/Image/Gd/Commands/BackupCommand.php

@@ -19,6 +19,10 @@ class BackupCommand extends \Intervention\Image\Commands\AbstractCommand
         $clone = imagecreatetruecolor($size->width, $size->height);
         imagealphablending($clone, false);
         imagesavealpha($clone, true);
+        $transparency = imagecolorallocatealpha($clone, 0, 0, 0, 127);
+        imagefill($clone, 0, 0, $transparency);
+
+        // copy image to clone
         imagecopy($clone, $image->getCore(), 0, 0, 0, 0, $size->width, $size->height);
 
         $image->setBackup($clone, $backupName);

+ 1 - 1
plugins/intervention-image/Intervention/Image/Image.php

@@ -6,7 +6,7 @@ namespace Intervention\Image;
  * @method \Intervention\Image\Image backup(string $name = 'default')                                                                                                     Backups current image state as fallback for reset method under an optional name. Overwrites older state on every call, unless a different name is passed.
  * @method \Intervention\Image\Image blur(integer $amount = 1)                                                                                                            Apply a gaussian blur filter with a optional amount on the current image. Use values between 0 and 100.
  * @method \Intervention\Image\Image brightness(integer $level)                                                                                                           Changes the brightness of the current image by the given level. Use values between -100 for min. brightness. 0 for no change and +100 for max. brightness.
- * @method \Intervention\Image\Image cache(\Closure $callback, integer $lifetime = null, boolean $returnObj)                                                              Method to create a new cached image instance from a Closure callback. Pass a lifetime in minutes for the callback and decide whether you want to get an Intervention Image instance as return value or just receive the image stream.
+ * @method \Intervention\Image\Image cache(\Closure $callback, integer $lifetime = null, boolean $returnObj = false)                                                              Method to create a new cached image instance from a Closure callback. Pass a lifetime in minutes for the callback and decide whether you want to get an Intervention Image instance as return value or just receive the image stream.
  * @method \Intervention\Image\Image canvas(integer $width, integer $height, mixed $bgcolor = null)                                                                       Factory method to create a new empty image instance with given width and height. You can define a background-color optionally. By default the canvas background is transparent.
  * @method \Intervention\Image\Image circle(integer $radius, integer $x, integer $y, \Closure $callback = null)                                                           Draw a circle at given x, y, coordinates with given radius. You can define the appearance of the circle by an optional closure callback.
  * @method \Intervention\Image\Image colorize(integer $red, integer $green, integer $blue)                                                                                Change the RGB color values of the current image on the given channels red, green and blue. The input values are normalized so you have to include parameters from 100 for maximum color value. 0 for no change and -100 to take out all the certain color on the image.

+ 7 - 6
plugins/intervention-image/Intervention/Image/ImageServiceProvider.php

@@ -2,6 +2,7 @@
 
 namespace Intervention\Image;
 
+use Illuminate\Foundation\Application;
 use Illuminate\Support\ServiceProvider;
 
 class ImageServiceProvider extends ServiceProvider
@@ -60,13 +61,13 @@ class ImageServiceProvider extends ServiceProvider
      */
     private function getProvider()
     {
-        $app = $this->app;
-        $version = intval($app::VERSION);
-        $provider = sprintf(
-            '\Intervention\Image\ImageServiceProviderLaravel%d', $version
-        );
+        if (version_compare(Application::VERSION, '5.0', '<')) {
+            $provider = '\Intervention\Image\ImageServiceProviderLaravel4';
+        } else {
+            $provider = '\Intervention\Image\ImageServiceProviderLaravel5';
+        }
 
-        return new $provider($app);
+        return new $provider($this->app);
     }
 
     /**

+ 2 - 0
plugins/intervention-image/Intervention/Image/ImageServiceProviderLaravel4.php

@@ -96,5 +96,7 @@ class ImageServiceProviderLaravel4 extends ServiceProvider
         $app['image'] = $app->share(function ($app) {
             return new ImageManager($app['config']->get('image::config'));
         });
+
+        $app->alias('image', 'Intervention\Image\ImageManager');
     }
 }

+ 10 - 55
plugins/intervention-image/Intervention/Image/ImageServiceProviderLaravel5.php

@@ -51,6 +51,8 @@ class ImageServiceProviderLaravel5 extends ServiceProvider
         $app['image'] = $app->share(function ($app) {
             return new ImageManager($app['config']->get('image'));
         });
+
+        $app->alias('image', 'Intervention\Image\ImageManager');
     }
 
     /**
@@ -73,63 +75,16 @@ class ImageServiceProviderLaravel5 extends ServiceProvider
             'imagecache'
         );
 
-        $config = $app['config'];
-
-        // create dynamic manipulation route
-        if (is_string($config->get('imagecache.route'))) {
-
-            // add original to route templates
-            $config->set('imagecache.templates.original', null);
-
-            $app['router']->get($config->get('imagecache.route').'/{template}/{filename}', array('as' => 'imagecache', function ($template, $filename) use ($app, $config) {
-
-                
-                // disable session cookies for image route
-                $app['config']->set('session.driver', 'array');
-
-                // find file
-                foreach ($config->get('imagecache.paths') as $path) {
-                    // don't allow '..' in filenames
-                    $image_path = $path.'/'.str_replace('..', '', $filename);
-                    if (file_exists($image_path) && is_file($image_path)) {
-                        break;
-                    } else {
-                        $image_path = false;
-                    }
-                }
-
-                // abort if file not found
-                if ($image_path === false) {
-                    $app->abort(404);
-                }
-
-                // define template callback
-                $callback = $config->get("imagecache.templates.{$template}");
-
-                if (is_callable($callback)) {
-
-                    // image manipulation based on callback
-                    $content = $app['image']->cache(function ($image) use ($image_path, $callback) {
-                        return $callback($image->make($image_path));
-                    }, $config->get('imagecache.lifetime'));
-
-                } else {
-
-                    // get original image file contents
-                    $content = file_get_contents($image_path);
-                }
-
-                // define mime type
-                $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
+        // imagecache route
+        if (is_string(config('imagecache.route'))) {
 
-                // return http response
-                return new IlluminateResponse($content, 200, array(
-                    'Content-Type' => $mime,
-                    'Cache-Control' => 'max-age='.($config->get('imagecache.lifetime')*60).', public',
-                    'Etag' => md5($content)
-                ));
+            $filename_pattern = '[ \w\\.\\/\\-]+';
 
-            }))->where(array('template' => join('|', array_keys($config->get('imagecache.templates'))), 'filename' => '[ \w\\.\\/\\-]+'));
+            // route to access template applied image file
+            $app['router']->get(config('imagecache.route').'/{template}/{filename}', array(
+                'uses' => 'Intervention\Image\ImageCacheController@getResponse',
+                'as' => 'imagecache'
+            ))->where(array('filename' => $filename_pattern));
         }
     }
 }

+ 1 - 1
plugins/intervention-image/Intervention/Image/Imagick/Commands/FitCommand.php

@@ -33,7 +33,7 @@ class FitCommand extends \Intervention\Image\Commands\AbstractCommand
         );
 
         // resize image
-        $image->getCore()->resizeImage($resized->getWidth(), $resized->getHeight(), \Imagick::FILTER_BOX, 1);
+        $image->getCore()->scaleImage($resized->getWidth(), $resized->getHeight());
         $image->getCore()->setImagePage(0,0,0,0);
 
         return true;

+ 4 - 3
plugins/intervention-image/Intervention/Image/Imagick/Commands/GetSizeCommand.php

@@ -14,11 +14,12 @@ class GetSizeCommand extends \Intervention\Image\Commands\AbstractCommand
      */
     public function execute($image)
     {
-        $info = $image->getCore()->identifyImage();
+        /** @var \Imagick $core */
+        $core = $image->getCore();
 
         $this->setOutput(new Size(
-            $info['geometry']['width'],
-            $info['geometry']['height']
+            $core->getImageWidth(),
+            $core->getImageHeight()
         ));
 
         return true;

+ 1 - 1
plugins/intervention-image/Intervention/Image/Imagick/Commands/ResizeCommand.php

@@ -20,7 +20,7 @@ class ResizeCommand extends \Intervention\Image\Commands\AbstractCommand
         $resized = $image->getSize()->resize($width, $height, $constraints);
 
         // modify image
-        $image->getCore()->resizeImage($resized->getWidth(), $resized->getHeight(), \Imagick::FILTER_BOX, 1);
+        $image->getCore()->scaleImage($resized->getWidth(), $resized->getHeight());
 
         return true;
     }

+ 1 - 1
plugins/intervention-image/Intervention/Image/Imagick/Encoder.php

@@ -17,7 +17,7 @@ class Encoder extends \Intervention\Image\AbstractEncoder
         $imagick = $this->image->getCore();
         $imagick->setImageBackgroundColor('white');
         $imagick->setBackgroundColor('white');
-        $imagick = $imagick->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
+        $imagick = $imagick->mergeImageLayers(\Imagick::LAYERMETHOD_MERGE);
         $imagick->setFormat($format);
         $imagick->setImageFormat($format);
         $imagick->setCompression($compression);