/** * Add a certain image to the classification. * * @param wsgen\MetaImage $image */ public function classify(wsgen\MetaImage $image) { $this->logger->log(E_NOTICE, "Classifying image '%s'.", $image->getFilename()); $colortable = array(); list($width, $height) = $image->getResolution(); for ($y = 0; $y < $height; ++$y) { for ($x = 0; $x < $width; ++$x) { $color = $image->getColorAt($x, $y); if ($color[3] > 127) { // Skip all transparent pixels with a transparency of more // than 50% continue; } if (($matchIndex = $this->findMatch($color, $colortable, self::INTER_COLOR_THRESHOLD)) === false) { $colortable[] = array('color' => $color, 'matches' => 1); } else { ++$colortable[$matchIndex]['matches']; $this->updateMatch($matchIndex, $colortable, $color); } } } usort($colortable, function ($a, $b) { return $b['matches'] - $a['matches']; }); $primaryColor = $colortable[0]['color']; // Search for an already defined color in the classification, which // matches the primary color of our image. if (($matchIndex = $this->findMatch($primaryColor, $this->classification, self::INTER_PICTURE_THRESHOLD)) === false) { $this->classification[] = array('color' => $primaryColor, 'images' => array($image)); } else { $this->classification[$matchIndex]['images'][] = $image; $this->updateMatch($matchIndex, $this->classification, $primaryColor); } }
/** * Layout the given image. * * This method may be called more than once with the same imagepath. This * layout manager takes care of this to ensure every image is only rendered * once. * * @param wsgen\MetaImage $image * @return void */ public function layoutImage(wsgen\MetaImage $image) { if (array_key_exists($image->getFilename(), $this->layout)) { // The image has already been processed. return; } $this->logger->log(E_NOTICE, "Adding image '%s' to layout.", basename($image->getFilename())); $resolution = $image->getResolution(); $filename = $image->getFilename(); $this->layout[$filename] = array(array(0, $this->currentY), $resolution); $this->images[$filename] = $image; $this->width = max($this->width, $resolution[0]); $this->height += $resolution[1]; $this->currentY += $resolution[1]; }
/** * Construct the object taking the image to provide information for as * argument. * * @param string $filename */ public function __construct($filename) { parent::__construct($filename); $this->resource = $this->loadImage($filename); }
public function __construct($filename, $resolution) { parent::__construct($filename); $this->resource = imagecreatefrompng($filename); $this->resolution = $resolution; }
/** * Draw an image to the provided coordinates. * * @param wsgen\MetaImage\GD $image * @param int $x * @param int $y * @throws RuntimeException if the image could not be drawn. */ public function drawImage(wsgen\MetaImage $image, $x, $y) { $this->logger->log(E_NOTICE, "Drawing image '%s' to sprite.", basename($image->getFilename())); $srcDimensions = $image->getResolution(); imagecopy($this->image, $image->getResource(), $x, $y, 0, 0, $srcDimensions[0], $srcDimensions[1]); }