Menu

Round corner rectangles extension

2003-07-15
2012-12-04
  • Shaun Crampton

    Shaun Crampton - 2003-07-15

    Very simple rounded corner rectangle function, draws the four straight lines then the curved corners - recommend setting the endcap on the lines to square.

    <?
    include_once('scripts/class.ezpdf.php');

    class Cezrpdf extends Cezpdf {
        function Cezrpdf($paper='a4',$orientation='portrait') {
            $this->Cezpdf($paper, $orientation);
        }

        function ezrRoundRectangle($x1,$y1,$width,$height,$dia) {
            $this->line($x1+$dia, $y1, $x1+$width-$dia, $y1);    # Bottom Line
            $this->line($x1+$dia, $y1+$height, $x1+$width-$dia, $y1+$height);    # Bottom Line
            $this->line($x1, $y1+$dia, $x1, $y1+$height-$dia);    # left Line
            $this->line($x1+$width, $y1+$dia, $x1+$width, $y1+$height-$dia);    # Right line

            $this->curve($x1+$dia, $y1, $x1, $y1, $x1,$y1, $x1, $y1+$dia);
            $this->curve($x1, $y1+$height-$dia, $x1, $y1+$height, $x1,$y1+$height, $x1+$dia, $y1+$height);
            $this->curve($x1+$width-$dia, $y1, $x1+$width, $y1, $x1+$width,$y1, $x1+$width, $y1+$dia);
            $this->curve($x1-$dia+$width, $y1+$height, $x1+$width, $y1+$height, $x1+$width,$y1+$height, $x1+$width, $y1-$dia+$height);
        }
    }
    ?>

     
    • Tam Khuat

      Tam Khuat - 2003-08-17

      Pls provide some guideline on how to use this file
      to create round corner rectangle.

      Pls consider I am as a dummies.I am new to PHP.
      where  to put the aboce files and how to use it
      Please give example if possible.
      Thank you.

       
    • Tam Khuat

      Tam Khuat - 2003-08-28

      I created a file callled 'class.cezrpdf.php'
      In this i included the code you given.
      I wrote the following code ..set the line cap to square or round....the rectangle still does not have round corner.Please show me where was it wrong.Thanks

      <? php
      include 'class.cezrpdf.php';
      $pdf = new Cezrpdf();

      $pdf->setStrokeColor(0,0,0);
      $pdf->setLineStyle(0.5,'square');

      $y=800;
      $size=7;

      $pdf->ezrRoundRectangle(87,$y+3,195,53);

      $pdf->stream();

      ?>

       
    • Scott King

      Scott King - 2004-10-31

      Here is a better rounded rectangle function that works with any line style. I've implemented this as an extension to the cpdf class, but it could be part of the cezpdf class.

      The function above draws each segment individually. This function creates the rounded rectangle as one path before stroking or filling it. Also, the function above uses the corners of the rectangle as the control points for the bezier. This function instead moves the control points half of the radius away from the corner,  causing the corner radius to be more exact.

      /**
      * draws a rounded rectangle
      * similar to rectangle, but rounds the corners of the rectangle using the given radius
      * the last parameter, if set to 1, will fill the path rather than stroke it
      */
      function roundedRectangle($x1,$y1,$width,$height,$rad,$f=0) {
          $x2 = $x1+$width;
          $y2 = $y1+$height;
          $rad2 = $rad/2;
          $string = sprintf("%.3f %.3f m ", $x1+$rad, $y1);
          $string.= sprintf("%.3f %.3f l ", $x2-$rad, $y1);
          $string.= sprintf("%.3f %.3f %.3f %.3f %.3f %.3f c ", $x2-$rad2, $y1, $x2, $y1+$rad2, $x2, $y1+$rad);
          $string.= sprintf("%.3f %.3f l ", $x2, $y2-$rad);
          $string.= sprintf("%.3f %.3f %.3f %.3f %.3f %.3f c ", $x2, $y2-$rad2, $x2-$rad2, $y2, $x2-$rad, $y2);
          $string.= sprintf("%.3f %.3f l ", $x1+$rad, $y2);
          $string.= sprintf("%.3f %.3f %.3f %.3f %.3f %.3f c ", $x1+$rad2, $y2, $x1, $y2-$rad2, $x1, $y2-$rad);
          $string.= sprintf("%.3f %.3f l ", $x1, $y1+$rad);
          $string.= sprintf("%.3f %.3f %.3f %.3f %.3f %.3f c ", $x1, $y1+$rad2, $x1+$rad2, $y1, $x1+$rad, $y1);
          $string.= ($f==1 ? 'f' : 'S');
          $this->addContent("\n".$string);
      }

       
    • rkt

      rkt - 2005-09-14

      If you are using Scott King's function, you must use a positive a number for height (regular rectangle() permits negative height). If you use negative nunber with roundedRectangle(), you will get a strange shape.

       

Log in to post a comment.

MongoDB Logo MongoDB