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

0001 <?php
0002 
0003 namespace Intervention\Image\Gd;
0004 
0005 class Driver extends \Intervention\Image\AbstractDriver
0006 {
0007     /**
0008      * Creates new instance of driver
0009      *
0010      * @param Decoder $decoder
0011      * @param Encoder $encoder
0012      */
0013     public function __construct(Decoder $decoder = null, Encoder $encoder = null)
0014     {
0015         if ( ! $this->coreAvailable()) {
0016             throw new \Intervention\Image\Exception\NotSupportedException(
0017                 "GD Library extension not available with this PHP installation."
0018             );
0019         }
0020 
0021         $this->decoder = $decoder ? $decoder : new Decoder;
0022         $this->encoder = $encoder ? $encoder : new Encoder;
0023     }
0024 
0025     /**
0026      * Creates new image instance
0027      *
0028      * @param  int     $width
0029      * @param  int     $height
0030      * @param  mixed   $background
0031      * @return \Intervention\Image\Image
0032      */
0033     public function newImage($width, $height, $background = null)
0034     {
0035         // create empty resource
0036         $core = imagecreatetruecolor($width, $height);
0037         $image = new \Intervention\Image\Image(new static, $core);
0038 
0039         // set background color
0040         $background = new Color($background);
0041         imagefill($image->getCore(), 0, 0, $background->getInt());
0042 
0043         return $image;
0044     }
0045 
0046     /**
0047      * Reads given string into color object
0048      *
0049      * @param  string $value
0050      * @return AbstractColor
0051      */
0052     public function parseColor($value)
0053     {
0054         return new Color($value);
0055     }
0056 
0057     /**
0058      * Checks if core module installation is available
0059      *
0060      * @return boolean
0061      */
0062     protected function coreAvailable()
0063     {
0064         return (extension_loaded('gd') && function_exists('gd_info'));
0065     }
0066 
0067     /**
0068      * Returns clone of given core
0069      *
0070      * @return mixed
0071      */
0072     public function cloneCore($core)
0073     {
0074         $width = imagesx($core);
0075         $height = imagesy($core);
0076         $clone = imagecreatetruecolor($width, $height);
0077         imagealphablending($clone, false);
0078         imagesavealpha($clone, true);
0079         $transparency = imagecolorallocatealpha($clone, 0, 0, 0, 127);
0080         imagefill($clone, 0, 0, $transparency);
0081         
0082         imagecopy($clone, $core, 0, 0, 0, 0, $width, $height);
0083 
0084         return $clone;
0085     }
0086 }