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

0001 <?php
0002 
0003 namespace Intervention\Image;
0004 
0005 abstract class AbstractDriver
0006 {
0007     /**
0008      * Decoder instance to init images from
0009      *
0010      * @var \Intervention\Image\AbstractDecoder
0011      */
0012     public $decoder;
0013 
0014     /**
0015      * Image encoder instance
0016      *
0017      * @var \Intervention\Image\AbstractEncoder
0018      */
0019     public $encoder;
0020 
0021     /**
0022      * Creates new image instance
0023      *
0024      * @param  int     $width
0025      * @param  int     $height
0026      * @param  string  $background
0027      * @return \Intervention\Image\Image
0028      */
0029     abstract public function newImage($width, $height, $background);
0030 
0031     /**
0032      * Reads given string into color object
0033      *
0034      * @param  string $value
0035      * @return AbstractColor
0036      */
0037     abstract public function parseColor($value);
0038 
0039     /**
0040      * Checks if core module installation is available
0041      *
0042      * @return boolean
0043      */
0044     abstract protected function coreAvailable();
0045 
0046     /**
0047      * Returns clone of given core
0048      *
0049      * @return mixed
0050      */
0051     public function cloneCore($core)
0052     {
0053         return clone $core;
0054     }
0055 
0056     /**
0057      * Initiates new image from given input
0058      *
0059      * @param  mixed $data
0060      * @return \Intervention\Image\Image
0061      */
0062     public function init($data)
0063     {
0064         return $this->decoder->init($data);
0065     }
0066 
0067     /**
0068      * Encodes given image
0069      *
0070      * @param  Image   $image
0071      * @param  string  $format
0072      * @param  int     $quality
0073      * @return \Intervention\Image\Image
0074      */
0075     public function encode($image, $format, $quality)
0076     {
0077         return $this->encoder->process($image, $format, $quality);
0078     }
0079 
0080     /**
0081      * Executes named command on given image
0082      *
0083      * @param  Image  $image
0084      * @param  string $name
0085      * @param  array $arguments
0086      * @return \Intervention\Image\Commands\AbstractCommand
0087      */
0088     public function executeCommand($image, $name, $arguments)
0089     {
0090         $commandName = $this->getCommandClassName($name);
0091         $command = new $commandName($arguments);
0092         $command->execute($image);
0093 
0094         return $command;
0095     }
0096 
0097     /**
0098      * Returns classname of given command name
0099      *
0100      * @param  string $name
0101      * @return string
0102      */
0103     private function getCommandClassName($name)
0104     {
0105         $name = mb_convert_case($name[0], MB_CASE_UPPER, 'utf-8') . mb_substr($name, 1, mb_strlen($name));
0106         
0107         $drivername = $this->getDriverName();
0108         $classnameLocal = sprintf('\Intervention\Image\%s\Commands\%sCommand', $drivername, ucfirst($name));
0109         $classnameGlobal = sprintf('\Intervention\Image\Commands\%sCommand', ucfirst($name));
0110 
0111         if (class_exists($classnameLocal)) {
0112             return $classnameLocal;
0113         } elseif (class_exists($classnameGlobal)) {
0114             return $classnameGlobal;
0115         }
0116 
0117         throw new \Intervention\Image\Exception\NotSupportedException(
0118             "Command ({$name}) is not available for driver ({$drivername})."
0119         );
0120     }
0121 
0122     /**
0123      * Returns name of current driver instance
0124      *
0125      * @return string
0126      */
0127     public function getDriverName()
0128     {
0129         $reflect = new \ReflectionClass($this);
0130         $namespace = $reflect->getNamespaceName();
0131 
0132         return substr(strrchr($namespace, "\\"), 1);
0133     }
0134 }