Exemplo n.º 1
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;
 }
Exemplo n.º 2
0
 public function perform(Zend_Image_Adapter_Gd $adapter, Zend_Image_Action_Rotate $rotate)
 {
     $handle = $adapter->getHandle();
     // By default GD turns 'the wrong way
     $angle = 360 - $rotate->getAngle();
     $color = $rotate->getBackgroundColor()->getRgb();
     $colorRes = imagecolorallocate($handle, $color['red'], $color['green'], $color['blue']);
     return imagerotate($handle, $angle, $colorRes);
 }
Exemplo n.º 3
0
 public function perform(Zend_Image_Adapter_Gd $adapter, Zend_Image_Action_Crop $crop)
 {
     $handle = $adapter->getHandle();
     $targetWidth = $crop->getWidth();
     $targetHeight = $crop->getHeight();
     $im = imagecreatetruecolor($targetWidth, $targetHeight);
     imagecopy($im, $adapter->getHandle(), 0, 0, $crop->getX(), $crop->getY(), $targetWidth, $targetHeight);
     return $im;
 }
Exemplo n.º 4
0
 /**
  * Draws an arc on the handle
  *
  * @param GD-object $handle The handle on which the ellipse is drawn
  * @param Zend_Image_Action_DrawEllipse $ellipseObject The object that with all info
  */
 public function perform(Zend_Image_Adapter_Gd $adapter, Zend_Image_Action_DrawArc $arcObject)
 {
     $color = $arcObject->getFillColor()->getRgb();
     $colorAlphaAlloc = imagecolorallocatealpha($adapter->getHandle(), $color['red'], $color['green'], $color['blue'], 127 - $arcObject->getFillAlpha() * 1.27);
     if (!$arcObject->isFilled()) {
         $style = IMG_ARC_NOFILL + IMG_ARC_EDGED;
     } else {
         $style = IMG_ARC_PIE;
     }
     $location = $arcObject->getLocation($adapter);
     imagefilledarc($adapter->getHandle(), $location->getX(), $location->getY(), $arcObject->getWidth(), $arcObject->getHeight(), $arcObject->getCutoutStart() - 90, $arcObject->getCutoutEnd() - 90, $colorAlphaAlloc, $style);
 }
Exemplo n.º 5
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);
        }
    }
Exemplo n.º 6
0
 public function perform(Zend_Image_Adapter_Gd $adapter, Zend_Image_Action_Resize $resize)
 {
     $handle = $adapter->getHandle();
     $newY = $resize->getYAmountCalculated();
     $newX = $resize->getXAmountCalculated();
     //if a dimension is 0, resize is proportionate to other dimension
     //if proportions are constrained and both dimensions are specified,
     //ImageMagick uses the smallest to constrain
     //
     //Note the fit parameter only works if the image is being made SMALLER
     //@TODO: implement constraints manually for images being made larger
     $fit = false;
     if ($resize->hasConstrainedProportions() && $newX > 0 && $newY > 0) {
         $fit = true;
     }
     //        $handle->resizeImage($newX, $newY, $resize->getFilter(), 1, $fit);
     $newImg = imagecreatetruecolor($newX, $newY);
     imagecopyresampled($newImg, $handle, 0, 0, 0, 0, $newX, $newY, $adapter->getWidth(), $adapter->getHeight());
     return $newImg;
 }
Exemplo n.º 7
0
 public function perform(Zend_Image_Adapter_Gd $adapter, Zend_Image_Action_Mirror $rotate)
 {
     $handle = $adapter->getHandle();
     $sizeX = $adapter->getWidth();
     $sizeY = $adapter->getHeight();
     $successFlop = true;
     if ($rotate->flop()) {
         $handleNew = imagecreatetruecolor($sizeX, $sizeY);
         $successFlop = imagecopyresampled($handleNew, $handle, 0, 0, $sizeX - 1, 0, $sizeX, $sizeY, 0 - $sizeX, $sizeY);
         $handle = $handleNew;
     }
     $successFlip = true;
     if ($rotate->flip()) {
         $handleNew = imagecreatetruecolor($sizeX, $sizeY);
         $successFlip = imagecopyresampled($handleNew, $handle, 0, 0, 0, $sizeY - 1, $sizeX, $sizeY, $sizeX, 0 - $sizeY);
         $handle = $handleNew;
     }
     if (!$successFlop || !$successFlip) {
         require_once 'Zend/Image/Exception';
         throw new Zend_Image_Exception('Was not able to mirror image as specified');
     }
     return $handle;
 }