/** * Constructor * * Instantiate an image file object based on either a pre-existing * image file on disk, or a new image file. * * @param string $img * @param int|string $w * @param int|string $h * @param \Pop\Color\Space\ColorInterface $color * @param array $types * @throws Exception * @return \Pop\Image\Gd */ 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; } }
/** * Constructor * * Instantiate an Imagick file object based on either a pre-existing image * file on disk, or a new image file. * * As of July 28th, 2011, stable testing was successful with the * following versions of the required software: * * ImageMagick 6.5.* * Ghostscript 8.70 or 8.71 * Imagick PHP Extension 3.0.1 * * Any variation in the versions of the required software may contribute to * the Pop\Image\Imagick component not functioning properly. * * @param string $img * @param int|string $w * @param int|string $h * @param \Pop\Color\Space\ColorInterface $color * @param array $types * @throws Exception * @return \Pop\Image\Imagick */ public function __construct($img, $w = null, $h = null, ColorInterface $color = null, $types = null) { $imagickFile = null; $imgFile = null; // If image passed is a paged image, like a PDF if (!file_exists($img) && strpos($img, '[') !== false) { $imgFile = trim(substr($img, 0, strpos($img, '['))); $imgFile .= substr($img, strpos($img, ']') + 1); $page = substr($img, strpos($img, '[')); $page = substr($page, 0, strpos($page, ']') + 1); $img = $imgFile; $imagickFile = file_exists($imgFile) ? realpath($imgFile) . $page : $img; // Else, continue } else { $imgFile = $img; $imagickFile = realpath($img); } parent::__construct($img, $w, $h, $color, $types); // Check to see if Imagick is installed. if (!self::isInstalled()) { throw new Exception('Error: The Imagick library extension must be installed to use the Imagick adapter.'); } // If image exists, get image info and store in an array. if (file_exists($this->fullpath) && $this->size > 0) { $this->resource = new \Imagick($imagickFile); $this->setImageInfo(); $this->setQuality(100); // If image does not exists, check to make sure the width and height // properties of the new image have been passed. } else { $this->resource = new \Imagick(); 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; $color = null === $color ? new Rgb(255, 255, 255) : $color; $clr = $this->setColor($color); // Create a new image and allocate the background color. $this->resource->newImage($w, $h, $clr, $this->ext); // Set the quality and create a new, blank image file. $this->setQuality(100); } $this->getInfo(); }