File indexing completed on 2025-01-26 05:29:15

0001 <?php
0002 
0003 namespace Intervention\Image;
0004 
0005 use Closure;
0006 
0007 class ImageManager
0008 {
0009     /**
0010      * Config
0011      *
0012      * @var array
0013      */
0014     public $config = [
0015         'driver' => 'gd'
0016     ];
0017 
0018     /**
0019      * Creates new instance of Image Manager
0020      *
0021      * @param array $config
0022      */
0023     public function __construct(array $config = [])
0024     {
0025         $this->checkRequirements();
0026         $this->configure($config);
0027     }
0028 
0029     /**
0030      * Overrides configuration settings
0031      *
0032      * @param array $config
0033      *
0034      * @return self
0035      */
0036     public function configure(array $config = [])
0037     {
0038         $this->config = array_replace($this->config, $config);
0039 
0040         return $this;
0041     }
0042 
0043     /**
0044      * Initiates an Image instance from different input types
0045      *
0046      * @param  mixed $data
0047      *
0048      * @return \Intervention\Image\Image
0049      */
0050     public function make($data)
0051     {
0052         return $this->createDriver()->init($data);
0053     }
0054 
0055     /**
0056      * Creates an empty image canvas
0057      *
0058      * @param  int   $width
0059      * @param  int   $height
0060      * @param  mixed $background
0061      *
0062      * @return \Intervention\Image\Image
0063      */
0064     public function canvas($width, $height, $background = null)
0065     {
0066         return $this->createDriver()->newImage($width, $height, $background);
0067     }
0068 
0069     /**
0070      * Create new cached image and run callback
0071      * (requires additional package intervention/imagecache)
0072      *
0073      * @param Closure $callback
0074      * @param int     $lifetime
0075      * @param boolean $returnObj
0076      *
0077      * @return Image
0078      */
0079     public function cache(Closure $callback, $lifetime = null, $returnObj = false)
0080     {
0081         if (class_exists('Intervention\\Image\\ImageCache')) {
0082             // create imagecache
0083             $imagecache = new ImageCache($this);
0084 
0085             // run callback
0086             if (is_callable($callback)) {
0087                 $callback($imagecache);
0088             }
0089 
0090             return $imagecache->get($lifetime, $returnObj);
0091         }
0092 
0093         throw new \Intervention\Image\Exception\MissingDependencyException(
0094             "Please install package intervention/imagecache before running this function."
0095         );
0096     }
0097 
0098     /**
0099      * Creates a driver instance according to config settings
0100      *
0101      * @return \Intervention\Image\AbstractDriver
0102      */
0103     private function createDriver()
0104     {
0105         if (is_string($this->config['driver'])) {
0106             $drivername = ucfirst($this->config['driver']);
0107             $driverclass = sprintf('Intervention\\Image\\%s\\Driver', $drivername);
0108 
0109             if (class_exists($driverclass)) {
0110                 return new $driverclass;
0111             }
0112 
0113             throw new \Intervention\Image\Exception\NotSupportedException(
0114                 "Driver ({$drivername}) could not be instantiated."
0115             );
0116         }
0117 
0118         if ($this->config['driver'] instanceof AbstractDriver) {
0119             return $this->config['driver'];
0120         }
0121 
0122         throw new \Intervention\Image\Exception\NotSupportedException(
0123             "Unknown driver type."
0124         );
0125     }
0126 
0127     /**
0128      * Check if all requirements are available
0129      *
0130      * @return void
0131      */
0132     private function checkRequirements()
0133     {
0134         if ( ! function_exists('finfo_buffer')) {
0135             throw new \Intervention\Image\Exception\MissingDependencyException(
0136                 "PHP Fileinfo extension must be installed/enabled to use Intervention Image."
0137             );
0138         }
0139     }
0140 }