示例#1
0
    /**
     * Draws a polygon on the handle
     *
     * @param GD-object $handle The handle on which the polygon is drawn
     * @param Zend_Image_Action_DrawPolygon $polygonObject The object that with all info
     */
    public function perform(Zend_Image_Adapter_Gd $adapter, Zend_Image_Action_DrawPolygon $polygonObject) { // As of ZF2.0 / PHP5.3, this can be made static.
        $handle = $adapter->getHandle();
    
        $points = $this->_parsePoints($polygonObject->getPoints());
        
        if(($pattern = $polygonObject->getStrokeDashPattern())!== null) {
            $color = imagecolorallocate($handle, 0, 255, 0);
            $array = array();
            foreach($pattern as $amountOfPixels) {
                $array = array_merge($array, array_fill(0, $amountOfPixels, $color));
                $array = array_merge($array, array_fill(0, $polygonObject->getStrokeDashOffset(), IMG_COLOR_TRANSPARENT));
            }
            
            if(count($array) > 0) {
                imagesetstyle($handle, $array);
            }
        }
        
        
        
        if($polygonObject->isFilled()) {
            //@TODO: extract this to Zend_Image_Adapter_Gd_Action_ActionAbstract ?
            $color = $polygonObject->getFillColor()->getRgb();
            $colorRes = imagecolorallocatealpha($handle,
                                               $color['red'],
                                               $color['green'],
                                               $color['blue'],
                                               $polygonObject->getFillAlpha());
            
            imagefilledpolygon($handle, $points, count($points)/2, $colorRes);
        }
        
        $points = $polygonObject->getPoints();
        $start = current($points);;
        $line = new Zend_Image_Action_DrawLine();
        while(current($points)!==false) {
            $from = current($points);
            $to = next($points);
            if($to===false) {
                if(!$polygonObject->isClosed()) {
                    break;
                } else {
                    $to = $start;
                }
            }

            $line->from($from);
            $line->to($to);
            $line->perform($adapter);
        }
    }
示例#2
0
 /**
  * Draw a line on the image, returns the GD-handle
  *
  * @param  Zend_Image_Adapter_ImageMagick image resource    $handle Image to work on
  * @param  Zend_Image_Action_DrawLine   $lineObject The object containing all settings needed for drawing a line.
  * @return void
  */
 public function perform(Zend_Image_Adapter_ImageMagick $adapter, Zend_Image_Action_DrawLine $lineObject)
 {
     $handle = $adapter->getHandle();
     $draw = new ImagickDraw();
     $draw->setStrokeWidth($lineObject->getStrokeWidth());
     $color = $lineObject->getStrokeColor();
     $draw->setStrokeColor((string) $color);
     $draw->setStrokeOpacity($lineObject->getStrokeAlpha());
     $draw->line($lineObject->getPointStart()->getX(), $lineObject->getPointStart()->getY(), $lineObject->getPointEnd()->getX(), $lineObject->getPointEnd()->getY());
     $handle->drawImage($draw);
 }
示例#3
0
 /**
  * Draw a line on the image, returns the GD-handle
  *
  * @param  GD image resource    $handle Image to work on
  * @param  Zend_Image_Action_DrawLine   $lineObject The object containing all settings needed for drawing a line.
  * @return void
  */
 public function perform(Zend_Image_Adapter_Gd $adapter, Zend_Image_Action_DrawLine $lineObject)
 {
     $handle = $adapter->getHandle();
     $strokeColor = $lineObject->getStrokeColor();
     $color = $strokeColor->getRgb();
     $colorAlphaAlloc = imagecolorallocatealpha($handle, $color['red'], $color['green'], $color['blue'], $lineObject->getStrokeAlpha());
     if ($lineObject->getStrokeWidth() == 1) {
         // Simple line
         imageline($handle, $lineObject->getPointStart()->getX(), $lineObject->getPointStart()->getY(), $lineObject->getPointEnd()->getX(), $lineObject->getPointEnd()->getY(), $colorAlphaAlloc);
     } elseif ($lineObject->getPointStart()->getX() == $lineObject->getPointEnd()->getX() || $lineObject->getPointStart()->getY() == $lineObject->getPointEnd()->getY()) {
         // Simple thick line
         $x1 = round(min($lineObject->getPointStart()->getX(), $lineObject->getPointEnd()->getX()) - ($lineObject->getStrokeWidth() / 2 - 0.5));
         $y1 = round(min($lineObject->getPointStart()->getY(), $lineObject->getPointEnd()->getY()) - ($lineObject->getStrokeWidth() / 2 - 0.5));
         $x2 = round(max($lineObject->getPointStart()->getX(), $lineObject->getPointEnd()->getX()) + ($lineObject->getStrokeWidth() / 2 - 0.5));
         $y2 = round(max($lineObject->getPointStart()->getY(), $lineObject->getPointEnd()->getY()) + ($lineObject->getStrokeWidth() / 2 - 0.5));
         if ($lineObject->isFilled()) {
             imagefilledrectangle($handle, $x1, $y1, $x2, $y2, $colorAlphaAlloc);
         } else {
             imagerectangle($handle, $x1, $y1, $x2, $y2, $colorAlphaAlloc);
         }
     } else {
         // Not horizontal nor vertical thick line
         $polygonObject = new Zend_Image_Action_DrawPolygon();
         $slope = ($lineObject->getPointEnd()->getY() - $lineObject->getPointStart()->getY()) / ($lineObject->getPointEnd()->getX() - $lineObject->getPointStart()->getX());
         // y = ax + b
         $a = ($lineObject->getStrokeWidth() / 2 - 0.5) / sqrt(1 + pow($slope, 2));
         $points = array(new Zend_Image_Point(round($lineObject->getPointStart()->getX() - (1 + $slope) * $a), round($lineObject->getPointStart()->getY() + (1 - $slope) * $a)), new Zend_Image_Point(round($lineObject->getPointStart()->getX() - (1 - $slope) * $a), round($lineObject->getPointStart()->getY() - (1 + $slope) * $a)), new Zend_Image_Point(round($lineObject->getPointEnd()->getX() + (1 + $slope) * $a), round($lineObject->getPointEnd()->getY() - (1 - $slope) * $a)), new Zend_Image_Point(round($lineObject->getPointEnd()->getX() + (1 - $slope) * $a), round($lineObject->getPointEnd()->getY() + (1 + $slope) * $a)));
         //Draw polygon
         $polygonObject = new Zend_Image_Action_DrawPolygon(array('points' => $points, 'strokeColor' => $strokeColor));
         $handler = new Zend_Image_Adapter_Gd_Action_DrawPolygon();
         $handle = $handler->perform($adapter, $polygonObject);
     }
     return $handle;
 }