Exemple #1
0
        $polygon = array_merge($polygon, $quad);
        for ($i = 2; $i < $n - 5; $i += 2) {
            //ctxx.bezierCurveTo(cp[2*i-2],cp[2*i-1],cp[2*i],cp[2*i+1],pts[i+2],pts[i+3]);
            $p1 = array($pts[$i], $pts[$i + 1]);
            $p2 = array($cp[2 * $i - 2], $cp[2 * $i - 1]);
            $p3 = array($cp[2 * $i], $cp[2 * $i + 1]);
            $p4 = array($pts[$i + 2], $pts[$i + 3]);
            $bez = Bezier_convert($p1, $p2, $p3, $p4, $t);
            $polygon = array_merge($polygon, $bez);
        }
        //ctxx.quadraticCurveTo(cp[2*n-10],cp[2*n-9],pts[n-2],pts[n-1]); // last arc
        $quad = quadBezier($polygon[count($polygon) - 2], $polygon[count($polygon) - 1], $cp[2 * $n - 10], $cp[2 * $n - 9], $pts[$n - 2], $pts[$n - 1]);
        $polygon = array_merge($polygon, $quad);
    }
    return $polygon;
}
$p = drawSpline(array(50, 250, 550, 250, 550, 750));
//print_r($p); exit;
$img = imagecreatetruecolor(80, 80);
$img2 = imagecreatetruecolor(800, 800);
$bg = ImageColorAllocate($img2, 255, 255, 255);
$fg = ImageColorAllocate($img2, 0, 0, 0);
imagefill($img2, 0, 0, $bg);
imageantialias($img2, true);
ImageSetThickness($img2, 5);
ImagePolygon($img2, $p, count($p) / 2, $fg);
imagearc($img2, 400, 400, 200, 200, 0, 350, $fg);
for ($i = 0; $i < 0; $i++) {
    imagefilter($img2, IMG_FILTER_GAUSSIAN_BLUR);
}
ImagePng($img2);
 public function mask($mask = array("face", "neck", "left_ear", "right_ear"), $rgba = array(255, 255, 255, 1), $blur = 0, $custom = null)
 {
     ini_set('memory_limit', '1024M');
     $default_masks = array("oval" => array(array(145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 184, 183, 145)), "face" => array(array(111, 186, 110, 185, 109, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 112, 187, 113, 188, 114, 133, 132, 131, 130, 129, 128, 127, 126, 125, 111)), "left_ear" => array(array(111, 119, 118, 117, 116, 115, 109), array(109, 185, 110, 186, 111)), "right_ear" => array(array(112, 120, 121, 122, 123, 124, 114), array(114, 188, 113, 187, 112)), "neck" => array(array(132, 157), array(157, 184, 183, 145), array(145, 126), array(126, 127, 128, 129, 130, 131, 132)), "left_eye" => array(array(18, 19, 20, 21, 22), array(22, 30, 29, 28, 18)), "right_eye" => array(array(23, 24, 25, 26, 27), array(27, 33, 32, 31, 23)), "left_brow" => array(array(71, 72, 73, 74, 75, 76), array(76, 84, 83, 71)), "right_brow" => array(array(77, 78, 79, 80, 81, 82), array(82, 86, 85, 77)), "mouth" => array(array(87, 88, 89, 90, 91, 92, 93), array(93, 108, 107, 106, 105, 104, 87)), "teeth" => array(array(87, 94, 95, 96, 97, 98, 93), array(93, 103, 102, 101, 100, 99, 87)), "nose" => array(array(50, 61, 62, 63, 64, 180, 55, 181, 69, 68, 67, 66, 56, 50)));
     if (!$this->_img || !$this->_tem) {
         return false;
     }
     $masks = array();
     foreach ($mask as $sm) {
         if ($sm == 'custom') {
             $masks = $custom;
         } else {
             $masks[] = $default_masks[$sm];
         }
     }
     //print_r($masks);
     $blur = $blur < 0 ? 0 : $blur > 30 ? 30 : $blur;
     $this->resize(2.0);
     // load original image
     $img = $this->_img;
     $original_image = $img->getImage();
     $tem = $this->_tem;
     $pointNumber = $tem->getPointNumber();
     $temPoints = $tem->getPoints();
     $lineVectors = $tem->getLines();
     $original_width = $img->getWidth();
     $original_height = $img->getHeight();
     // create mask image and allocate bg and transparent colours
     $maskimg = imagecreate($original_width, $original_height);
     $bgcolor = imagecolorallocate($maskimg, $rgba[0], $rgba[1], $rgba[2]);
     $r = $rgba[0] == 255 ? 254 : $rgba[0] + 1;
     $g = $rgba[1] == 255 ? 254 : $rgba[1] + 1;
     $b = $rgba[2] == 255 ? 254 : $rgba[2] + 1;
     $trans = imagecolorallocate($maskimg, $r, $g, $b);
     // make transform colour very close to bgcolor
     imagecolortransparent($maskimg, $trans);
     imageantialias($maskimg, true);
     // construct sets of Bezier curves
     $polygons = array();
     foreach ($masks as $mask) {
         $polygon = array();
         foreach ($mask as $m) {
             $subpolygon = array();
             $mask_pts = array();
             foreach ($m as $pt) {
                 $mask_pts[] = $temPoints[$pt][0];
                 $mask_pts[] = $temPoints[$pt][1];
             }
             if (count($m) == 2) {
                 // just a straight line, no need for curves
                 $subpolygon = $mask_pts;
             } else {
                 $subpolygon = drawSpline($mask_pts);
             }
             $polygon = array_merge($polygon, $subpolygon);
         }
         imagefilledpolygon($maskimg, $polygon, count($polygon) / 2, $trans);
         $polygons[] = $polygon;
     }
     imagefill($maskimg, 0, 0, $bgcolor);
     // fill with mask color
     imageantialias($maskimg, false);
     $gaussian = array(array(1.0, 2.0, 1.0), array(2.0, 4.0, 2.0), array(1.0, 2.0, 1.0));
     for ($blurlevel = 0; $blurlevel <= $blur; $blurlevel++) {
         imagesetthickness($maskimg, $blurlevel);
         // construct sets of Bezier curves
         foreach ($polygons as $polygon) {
             imagepolygon($maskimg, $polygon, count($polygon) / 2, $trans);
         }
         //imagefilter($maskimg, IMG_FILTER_GAUSSIAN_BLUR);
         //imageconvolution($maskimg, $gaussian, 16, 0);
         $blurtrans = $blur == $blurlevel || $blur == 0 ? 100 : 100 / $blur;
         imagecopymerge($original_image, $maskimg, 0, 0, 0, 0, $original_width, $original_height, $blurtrans);
     }
     imagedestroy($maskimg);
     $this->resize(0.5);
     // set transparent if alpha == 0
     if ($rgba[3] == 0) {
         imagecolortransparent($original_image, $bgcolor);
     }
     //$img->setImage($original_image);
     $img->setDescription(array("mask" => array("mask" => $mask, "color" => $rgba, "blur" => $blur)));
     return $this;
 }