Просмотр исходного кода

#542 Intervention image from 2.3.7 to 2.3.11

Alex Trofimov 9 лет назад
Родитель
Сommit
208b9414c2

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

@@ -31,7 +31,7 @@ abstract class AbstractDecoder
     /**
      * Initiates new image from Imagick object
      *
-     * @param  Imagick $object
+     * @param \Imagick $object
      * @return \Intervention\Image\Image
      */
     abstract public function initFromImagick(\Imagick $object);
@@ -61,7 +61,19 @@ abstract class AbstractDecoder
      */
     public function initFromUrl($url)
     {
-        if ($data = @file_get_contents($url)) {
+        
+        $options = array(
+            'http' => array(
+                'method'=>"GET",
+                'header'=>"Accept-language: en\r\n".
+                "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2\r\n"
+          )
+        );
+        
+        $context  = stream_context_create($options);
+        
+
+        if ($data = @file_get_contents($url, false, $context)) {
             return $this->initFromBinary($data);
         }
 
@@ -79,9 +91,18 @@ abstract class AbstractDecoder
     public function initFromStream($stream)
     {
         $offset = ftell($stream);
-        rewind($stream);
+        $shouldAndCanSeek = $offset !== 0 && $this->isStreamSeekable($stream);
+
+        if ($shouldAndCanSeek) {
+            rewind($stream);
+        }
+
         $data = @stream_get_contents($stream);
-        fseek($stream, $offset);
+
+        if ($shouldAndCanSeek) {
+            fseek($stream, $offset);
+        }
+
         if ($data) {
             return $this->initFromBinary($data);
         }
@@ -91,6 +112,18 @@ abstract class AbstractDecoder
         );
     }
 
+    /**
+     * Checks if we can move the pointer for this stream
+     *
+     * @param resource $stream
+     * @return bool
+     */
+    private function isStreamSeekable($stream)
+    {
+        $metadata = stream_get_meta_data($stream);
+        return $metadata['seekable'];
+    }
+
     /**
      * Determines if current source data is GD resource
      *

+ 14 - 0
plugins/intervention-image/Intervention/Image/AbstractFont.php

@@ -82,6 +82,8 @@ abstract class AbstractFont
     public function text($text)
     {
         $this->text = $text;
+
+        return $this;
     }
 
     /**
@@ -103,6 +105,8 @@ abstract class AbstractFont
     public function size($size)
     {
         $this->size = $size;
+
+        return $this;
     }
 
     /**
@@ -124,6 +128,8 @@ abstract class AbstractFont
     public function color($color)
     {
         $this->color = $color;
+
+        return $this;
     }
 
     /**
@@ -145,6 +151,8 @@ abstract class AbstractFont
     public function angle($angle)
     {
         $this->angle = $angle;
+
+        return $this;
     }
 
     /**
@@ -166,6 +174,8 @@ abstract class AbstractFont
     public function align($align)
     {
         $this->align = $align;
+
+        return $this;
     }
 
     /**
@@ -187,6 +197,8 @@ abstract class AbstractFont
     public function valign($valign)
     {
         $this->valign = $valign;
+
+        return $this;
     }
 
     /**
@@ -208,6 +220,8 @@ abstract class AbstractFont
     public function file($file)
     {
         $this->file = $file;
+
+        return $this;
     }
 
     /**

+ 8 - 0
plugins/intervention-image/Intervention/Image/Exception/ImageException.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace Intervention\Image\Exception;
+
+class ImageException extends \RuntimeException
+{
+    # nothing to override
+}

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

@@ -2,7 +2,7 @@
 
 namespace Intervention\Image\Exception;
 
-class InvalidArgumentException extends \RuntimeException
+class InvalidArgumentException extends ImageException
 {
     # nothing to override
 }

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

@@ -2,7 +2,7 @@
 
 namespace Intervention\Image\Exception;
 
-class MissingDependencyException extends \RuntimeException
+class MissingDependencyException extends ImageException
 {
     # nothing to override
 }

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

@@ -2,7 +2,7 @@
 
 namespace Intervention\Image\Exception;
 
-class NotFoundException extends \RuntimeException
+class NotFoundException extends ImageException
 {
     # nothing to override
 }

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

@@ -2,7 +2,7 @@
 
 namespace Intervention\Image\Exception;
 
-class NotReadableException extends \RuntimeException
+class NotReadableException extends ImageException
 {
     # nothing to override
 }

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

@@ -2,7 +2,7 @@
 
 namespace Intervention\Image\Exception;
 
-class NotSupportedException extends \RuntimeException
+class NotSupportedException extends ImageException
 {
     # nothing to override
 }

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

@@ -2,7 +2,7 @@
 
 namespace Intervention\Image\Exception;
 
-class NotWritableException extends \RuntimeException
+class NotWritableException extends ImageException
 {
     # nothing to override
 }

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

@@ -2,7 +2,7 @@
 
 namespace Intervention\Image\Exception;
 
-class RuntimeException extends \RuntimeException
+class RuntimeException extends ImageException
 {
     # nothing to override
 }

+ 15 - 5
plugins/intervention-image/Intervention/Image/ImageManager.php

@@ -100,15 +100,25 @@ class ImageManager
      */
     private function createDriver()
     {
-        $drivername = ucfirst($this->config['driver']);
-        $driverclass = sprintf('Intervention\\Image\\%s\\Driver', $drivername);
+        if (is_string($this->config['driver'])) {
+            $drivername = ucfirst($this->config['driver']);
+            $driverclass = sprintf('Intervention\\Image\\%s\\Driver', $drivername);
 
-        if (class_exists($driverclass)) {
-            return new $driverclass;
+            if (class_exists($driverclass)) {
+                return new $driverclass;
+            }
+
+            throw new \Intervention\Image\Exception\NotSupportedException(
+                "Driver ({$drivername}) could not be instantiated."
+            );
+        }
+
+        if ($this->config['driver'] instanceof AbstractDriver) {
+            return $this->config['driver'];
         }
 
         throw new \Intervention\Image\Exception\NotSupportedException(
-            "Driver ({$drivername}) could not be instantiated."
+            "Unknown driver type."
         );
     }
 

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

@@ -47,7 +47,7 @@ class ImageServiceProviderLaravel5 extends ServiceProvider
         );
 
         // create image
-        $app['image'] = $app->share(function ($app) {
+        $app->singleton('image', function ($app) {
             return new ImageManager($app['config']->get('image'));
         });
 

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

@@ -21,8 +21,11 @@ class ImageServiceProviderLumen extends ServiceProvider
           'image'
         );
 
+        // set configuration
+        $app->configure('image');
+
         // create image
-        $app['image'] = $app->share(function ($app) {
+        $app->singleton('image',function ($app) {
             return new ImageManager($app['config']->get('image'));
         });
 

+ 62 - 0
plugins/intervention-image/Intervention/Image/Imagick/Commands/ExifCommand.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace Intervention\Image\Imagick\Commands;
+
+use Intervention\Image\Commands\ExifCommand as BaseCommand;
+
+class ExifCommand extends BaseCommand
+{
+    /**
+     * Prefer extension or not
+     *
+     * @var bool
+     */
+    private $preferExtension = true;
+
+    /**
+     *
+     */
+    public function dontPreferExtension() {
+        $this->preferExtension = false;
+    }
+
+    /**
+     * Read Exif data from the given image
+     *
+     * @param  \Intervention\Image\Image $image
+     * @return boolean
+     */
+    public function execute($image)
+    {
+        if ($this->preferExtension && function_exists('exif_read_data')) {
+            return parent::execute($image);
+        }
+
+        $core = $image->getCore();
+
+        if ( ! method_exists($core, 'getImageProperties')) {
+            throw new \Intervention\Image\Exception\NotSupportedException(
+                "Reading Exif data is not supported by this PHP installation."
+            );
+        }
+
+        $requestedKey = $this->argument(0)->value();
+        if ($requestedKey !== null) {
+            $this->setOutput($core->getImageProperty('exif:' . $requestedKey));
+            return true;
+        }
+
+        $exif = [];
+        $properties = $core->getImageProperties();
+        foreach ($properties as $key => $value) {
+            if (substr($key, 0, 5) !== 'exif:') {
+                continue;
+            }
+
+            $exif[substr($key, 5)] = $value;
+        }
+
+        $this->setOutput($exif);
+        return true;
+    }
+}

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

@@ -18,8 +18,9 @@ class Decoder extends \Intervention\Image\AbstractDecoder
 
         try {
 
+            $core->setBackgroundColor(new \ImagickPixel('transparent'));
             $core->readImage($path);
-            $core->setImageType(\Imagick::IMGTYPE_TRUECOLORMATTE);
+            $core->setImageType(defined('\Imagick::IMGTYPE_TRUECOLORMATTE') ? \Imagick::IMGTYPE_TRUECOLORMATTE : \Imagick::IMGTYPE_TRUECOLORALPHA);
 
         } catch (\ImagickException $e) {
             throw new \Intervention\Image\Exception\NotReadableException(

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

@@ -53,7 +53,7 @@ class Response
 
         if (function_exists('app') && is_a($app = app(), 'Illuminate\Foundation\Application')) {
 
-            $response = \Response::make($data);
+            $response = \Illuminate\Support\Facades\Response::make($data);
             $response->header('Content-Type', $mime);
             $response->header('Content-Length', $length);