/** * Get the composited canvas. * */ public function getCompositeCanvas() { // Create our target canvas $canvas = new Canvas($this->width, $this->height, 0x0); $crect = $canvas->getRect(); // Precalculate some stuff for performance $samesize = []; foreach ($this->layers as $i => $layer) { $samesize[$i] = $layer->position == $layer->canvas->getRect(); } // Process the composite canvas for ($n = 0; $n < $this->width; $n++) { for ($m = 0; $m < $this->height; $m++) { if ($this->background) { $co = $this->background->getPixel($n, $m); } else { $co = 0x0; } foreach ($this->layers as $i => $layer) { //printf("Pre [%d] (mode=%d): 0x%06x ", $i, $layer->blend, $co); if ($layer->position->isXYWithin($n, $m)) { $co = $this->blend($co, $layer->canvas->getPixel($n, $m), $layer->blend, $layer->opacity); } //printf("Post: 0x%06x\n", $co); } $canvas->setPixel($n, $m, $co); } } return $canvas; }
public function test() { $this->status = "Setting up"; $max = 5 * LayeredCanvas::NUM_BLEND_MODES; $this->doProgress(0, $max); // Set up a layered canvas for compositing our image $sw = 320; // $this->rect->w; $ssw = $sw / 5; $sh = 240; $c = new Canvas($sw * 4, $sh * 4, 0); $f = new BitmapFont(3); $f2 = new BitmapFont(0); $fb = new BitmapFont(7); $fb->drawText($c, $c->width - $sw + 20, $c->height - 50, "Cherry LayeredCanvas Demo", 0xffffff); $f->drawText($c, $c->width - $sw + 20, $c->height - 30, "Showing all " . LayeredCanvas::NUM_BLEND_MODES . " available blend modes", 0xffffff); // Preload our canvases $c2 = Canvas::createFromFile("320x240-1.png"); $c1 = Canvas::createFromFile("320x240-2.png"); $cs1 = new Canvas($ssw, $sh); $cs2 = new Canvas($ssw, $sh); for ($blend = 0; $blend < LayeredCanvas::NUM_BLEND_MODES; $blend++) { $this->status = "Rendering " . LayeredCanvas::$blendmodes[$blend]; $tslice = []; for ($slice = 0; $slice < 5; $slice++) { $c1->draw($cs1, $cs1->getRect(), new Rect($ssw * $slice, 0, $ssw, $sh)); $c2->draw($cs2, $cs2->getRect(), new Rect($ssw * $slice, 0, $ssw, $sh)); $lc = new LayeredCanvas($ssw, $sh, $cs1); $lc->addLayer($cs2, $cs2->getRect(), $blend, $slice * 0.25); $tstart = microtime(true); $co = $lc->getCompositeCanvas(); $tslice[] = (double) (microtime(true) - $tstart); $col = $blend % 4; $row = floor($blend / 4); $ostr = sprintf("%d%%", max(0, min(100, $slice * 25))); $f2->drawTextUp($co, $ssw - 11, $sh - 4, $ostr, 0x0); $f2->drawTextUp($co, $ssw - 12, $sh - 5, $ostr, 0xffffff); $co->draw($c, new Rect($col * $sw + $ssw * $slice, $row * $sh, $ssw, $sh)); $this->doProgress($blend * 5 + $slice, $max); } $modestr = sprintf("%s", LayeredCanvas::$blendmodes[$blend]); $f->drawText($c, $col * $sw + 6, $row * $sh + 6, $modestr, 0x0); $f->drawText($c, $col * $sw + 5, $row * $sh + 5, $modestr, 0xffffff); $tslice = array_sum($tslice); $timestr = sprintf("Mode %d", $blend); $f2->drawText($c, $col * $sw + 6, $row * $sh + 21, $timestr, 0x0); $f2->drawText($c, $col * $sw + 5, $row * $sh + 20, $timestr, 0xffffff); } $this->status = "Done"; $this->doProgress($max, $max, true); $c->save("composite.png"); return; }
function __construct($url, $width) { if (!$url) { throw new \UnexpectedValueException("Thumbalizr: Invalid URL"); } if (strpos($url, "://") === false) { $url = "http://" . $url; } $urlenc = urlencode($url); $apiurl = "http://api.thumbalizr.com/?url={$url}&width={$width}"; \debug("Thumbalizr: %s", "Querying {$apiurl}"); $img = fopen($apiurl, "rb"); parent::__construct(); $this->loadString(stream_get_contents($img)); }
public function drawTextUp(Canvas $image, $x, $y, $text, $color) { \imagestringup($image->himage, $this->font, $x, $y, $text, $image->map($color)); }
public function saveComposite($filename) { // Measure the image and create an image 32x32 pixels wider than the source $ir = $this->img->measure(); $wi = Canvas::createTrueColor($ir->w + SPECTRUM_SIZE + 4, $ir->h + SPECTRUM_SIZE + 4); // Draw the image into the new working image offset by 32x32 puxels $srect = $this->img->measure(); $drect = $this->img->measure(); $drect->move(SPECTRUM_SIZE + 4, SPECTRUM_SIZE + 4); $this->img->draw($wi, $drect, $srect); $wi->drawLine(0, $drect->y - 2, $wi->width, $drect->y - 2, [255, 255, 255]); $wi->drawLine($drect->x - 2, 0, $drect->x - 2, $wi->height, [255, 255, 255]); $wi->drawFilledRect(0, 0, $drect->x - 4, $drect->y - 4, [64, 64, 64]); $this->drawSpectrumX($wi, $this->xbuckets, $drect); $this->drawSpectrumY($wi, $this->ybuckets, $drect); // Save the working image $wi->save($filename); }
#!/usr/bin/php <?php require_once "../../share/include/cherryphp"; use Cherry\Graphics\Canvas; if (!file_exists("image.png")) { die("Make sure a file named image.png is in the same directory.\n"); } $c = new Canvas(); $c->load('image.png'); $c2 = clone $c; $c->resize(1024, 1024); $c->save('image1.png'); $c2->resize(1024, 1024, true); $c2->save('image2.png');
public static function createFromFile($filename) { $c = new Canvas(); $c->load($filename); return $c; }