public function __construct($img, $w = null, $h = null, ColorInterface $color = null, $types = null)
{
parent::__construct($img, $w, $h, $color, $types);
// Check to see if GD is installed.
if (!self::isInstalled()) {
throw new Exception('Error: The GD library extension must be installed to use the Gd adapter.');
}
$this->getInfo();
// If image exists, get image info and store in an array.
if (file_exists($this->fullpath) && $this->size > 0) {
$imgSize = getimagesize($img);
// Set image object properties.
$this->width = $imgSize[0];
$this->height = $imgSize[1];
$this->channels = isset($imgSize['channels']) ? $imgSize['channels'] : null;
$this->depth = isset($imgSize['bits']) ? $imgSize['bits'] : null;
$this->setQuality(100);
// If the image is a GIF
if ($this->mime == 'image/gif') {
$this->mode = 'Indexed';
// Else if the image is a PNG
} else {
if ($this->mime == 'image/png') {
$imgData = $this->read();
$colorType = ord($imgData[25]);
switch ($colorType) {
case 0:
$this->channels = 1;
$this->mode = 'Gray';
break;
case 2:
$this->channels = 3;
$this->mode = 'RGB';
break;
case 3:
$this->channels = 3;
$this->mode = 'Indexed';
break;
case 4:
$this->channels = 1;
$this->mode = 'Gray';
$this->alpha = true;
break;
case 6:
$this->channels = 3;
$this->mode = 'RGB';
$this->alpha = true;
break;
}
// Else if the image is a JPEG.
} else {
if ($this->channels == 1) {
$this->mode = 'Gray';
} else {
if ($this->channels == 3) {
$this->mode = 'RGB';
} else {
if ($this->channels == 4) {
$this->mode = 'CMYK';
}
}
}
}
}
// If image does not exists, check to make sure the width and
// height properties of the new image have been passed.
} else {
if (null === $w || null === $h) {
throw new Exception('Error: You must define a width and height for a new image object.');
}
// Set image object properties.
$this->width = $w;
$this->height = $h;
$this->channels = null;
// Create a new image and allocate the background color.
if ($this->mime == 'image/gif') {
$this->resource = imagecreate($w, $h);
$this->setBackgroundColor(null === $color ? new Rgb(255, 255, 255) : $color);
$clr = $this->setColor($this->backgroundColor);
} else {
$this->resource = imagecreatetruecolor($w, $h);
$this->setBackgroundColor(null === $color ? new Rgb(255, 255, 255) : $color);
$clr = $this->setColor($this->backgroundColor);
imagefill($this->resource, 0, 0, $clr);
}
// Set the quality and create a new, blank image file.
unset($clr);
$this->setQuality(100);
$this->output = $this->resource;
}
}