Example #1
0
 function applyFilter(Canvas $canvas)
 {
     $himage = $canvas->getImage();
     // If not we need to do some enumeration
     $total = imagecolorstotal($himage);
     if ($total > 0) {
         // This works for indexed images but not for truecolor
         for ($i = 0; $i < $total; $i++) {
             $index = imagecolorsforindex($himage, $i);
             $rgb = rgb($index['red'], $index['green'], $index['blue']);
             $hsv = hsv($rgb);
             $hsv->hue = $this->hue;
             $rgb = rgb($hsv);
             $red = $rgb->red;
             $green = $rgb->green;
             $blue = $rgb->blue;
             imagecolorset($himage, $i, $red, $green, $blue);
         }
     } else {
         // For truecolor we need to enum it all
         for ($x = 0; $x < imagesx($himage); $x++) {
             for ($y = 0; $y < imagesy($himage); $y++) {
                 $index = imagecolorat($himage, $x, $y);
                 $rgb = rgb($index['red'], $index['green'], $index['blue'], $index['alpha']);
                 $hsv = hsv($rgb);
                 $hsv->hue = $this->hue;
                 $rgb = rgb($hsv);
                 $red = $rgb->red;
                 $green = $rgb->green;
                 $blue = $rgb->blue;
                 imagesetpixel($himage, $x, $y, $red << 16 | $green < 8 | $blue);
             }
         }
     }
 }
function get_colors($url)
{
    // get the image name
    $url = trim($url);
    // create a working image
    $im = imagecreatefromjpeg($url);
    $height = imagesy($im);
    $width = imagesx($im);
    // sample five points in the image, based on rule of thirds and center
    $topy = round($height / 3);
    $bottomy = round($height / 3 * 2);
    $leftx = round($width / 3);
    $rightx = round($width / 3 * 2);
    $centery = round($height / 2);
    $centerx = round($width / 2);
    // grab those colors
    $rgb[] = imagecolorat($im, $leftx, $topy);
    $rgb[] = imagecolorat($im, $rightx, $topy);
    $rgb[] = imagecolorat($im, $leftx, $bottomy);
    $rgb[] = imagecolorat($im, $rightx, $bottomy);
    $rgb[] = imagecolorat($im, $centerx, $centery);
    // process points
    for ($i = 0; $i <= count($rgb) - 1; $i++) {
        $r[$i] = $rgb[$i] >> 16 & 0xff;
        $g[$i] = $rgb[$i] >> 8 & 0xff;
        $b[$i] = $rgb[$i] & 0xff;
        //rgb
        list($colors[$i]['r'], $colors[$i]['g'], $colors[$i]['b']) = array($r[$i], $g[$i], $b[$i]);
        //hsv
        list($colors[$i]['h'], $colors[$i]['s'], $colors[$i]['v']) = hsv($r[$i], $g[$i], $b[$i]);
        //hex
        $colors[$i]['hex'] = rgbhex($r[$i], $g[$i], $b[$i]);
    }
    return $colors;
}
Example #3
0
 /**
  * @brief Render the chart in 3D
  * 
  * @return Canvas 
  */
 private function render3D()
 {
     $c = new Canvas($this->width, $this->height, rgb($this->getProperty('background', '#FFFFFF')));
     $radiusx = 180;
     $radiusy = 90;
     $cx = $c->getWidth() / 2;
     $cy = $c->getHeight() / 2;
     $explode = $this->getProperty('explode', 0);
     $palette = $this->getProperty('palette');
     list($label, $vals) = $this->dataset->getSeries(0);
     $labels = $this->dataset->getLabels();
     $sum = $vals->getSum();
     $ci = 0;
     $sa = 0;
     for ($n = 0; $n < $vals->getCount(); $n++) {
         list($val, $key) = $vals->getValue($n);
         $a = 360 / $sum * $val;
         // Get angle
         $ea = $sa + $a;
         $ch = rgb($palette[$ci]);
         $cs = hsv($ch);
         $cs->value = $cs->value - 30;
         if (arr::hasKey($labels, $n)) {
             $l = $labels[$n];
         } else {
             $l = $n;
         }
         $data[] = array('key' => $key, 'label' => $l, 'c1' => $ch, 'c2' => $cs, 'sa' => $sa, 'ea' => $ea);
         $sa = $ea;
         $ci++;
     }
     $offs = array();
     foreach ($data as $id => $slice) {
         $avg = ($slice['sa'] + $slice['ea']) / 2;
         $data[$id]['ox'] = cos(($avg - 90) % 360 * PI / 180) * $explode;
         $data[$id]['oy'] = sin(($avg - 90) % 360 * PI / 180) * $explode;
         $data[$id]['dx'] = cos(($avg - 180) % 360 * PI / 180);
         $data[$id]['dy'] = sin(($avg - 180) % 360 * PI / 180);
     }
     $f = new BitmapFont(3);
     $f->setTextEffect(BitmapFont::EFFECT_OUTLINE, rgb(255, 255, 255));
     $p = $c->getPainter();
     for ($yp = 20; $yp >= 0; $yp--) {
         foreach ($data as $slice) {
             $ox = $slice['ox'];
             $oy = $slice['oy'];
             $p->drawFilledArc($cx + $ox, $cy + $oy + $yp, $radiusx * 2, $radiusy * 2, $slice['sa'], $slice['ea'], $yp == 0 ? $slice['c1'] : $slice['c2']);
         }
     }
     for ($yp = 20; $yp >= 0; $yp--) {
         foreach ($data as $slice) {
             // TODO: Labels
             $m = $f->measure($slice['label']);
             $dx = $slice['dx'];
             $dy = $slice['dy'];
             $c->drawText($f, rgb(0, 0, 0), $cx + $dx * $radiusx / 1.5 - $m['width'] / 2, $cx + $dy * $radiusy / 1.5 - $m['height'] / 2, $slice['label']);
         }
     }
     $this->renderObjects($c);
     // Return the canvas
     return $c;
 }