Draw lines in php

PHP Graphics: Drawing Line, Rectangle, Polygon, Arc, Ellipse, Patterned Line

PHP provides many functions to draw lines, rectangles, polygons, arcs, ellipses, and much more. The GD library is utilized for dynamic picture creation. In PHP, we can easily use ​the GD library to make GIF, PNG, or JPG pictures quickly from our code. So there is no need to write HTML and CSS code. We can easily handle this using the PHP programming language.

If you are unsure that you have the GD library, you can run phpinfo() to check that GD support is enabled. On the off chance that you don’t have it, you can download it for free.

PHP Draw Lines

PHP provides the imageline() function to draw a line between the two given points.

imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) : bool

Parameters

$image— An image resource, this is returned by the imagecreatetruecolor() function.
$x1— x coordinate for the first point.
$y1— y coordinate for the first point.
$x2— x coordinate for the second point.
$y2— y coordinate for the second point.
$color— color identified by the imagecolorallocate().

Читайте также:  Implement and extend java class

It returns a boolean value, TRUE on success and FALSE on failure.

Example: imageline()

?php $image = ImageCreateTrueColor(270, 150); imagesavealpha($image, true); $trans_colour = imagecolorallocatealpha($image, 0, 0, 0, 127); imagefill($image, 0, 0, $trans_colour); $x1 = $y1 = 0 ; $x2 = 270; $y2 = 150; $color = #4a235a; ImageLine($image, $x1, $y1, $x2, $y2, $color); header('Content-type: image/png'); ImagePNG($image); ImageDestroy($image); ?>

In the above example, the ImageCreateTrueColor() function creates a new true color image. imagesavealpha() function sets a flag to retain full alpha channel information. imagecolorallocatealpha() function allocates a color for an image, i.e., a transparent color, which is filled by the imagefill() function.

Output: ImageLine()

PHP Graphics

PHP Draw Rectangles

PHP provides the imagefilledrectangle() function to draw a filled rectangle.

imagefilledrectangle($image, $x1, $y1, $x2, $y2, $color)

Parameters

$image— An image resource. This is returned by the imagecreatetruecolor() function.
$x1, $y1— x and y coordinates for point 1.
$x2, $y2— x and y coordinates for point 2.
$color— color identified by imagecolorallocate().

Example

?php $x = 180; $y = 110; $image=imagecreatetruecolor($x, $y); //set red color $red = imagecolorallocatealpha($image, 255, 0, 0, 75); imagefilledrectangle($image, 0, 0, $x, $y, $red); header('Content-type: image/png'); ImagePNG($image); ImageDestroy($image); ?>

In the above example, we allocated a color for the rectangle using the imagecolorallocatealpha() function.

Output: Draw Rectangle

PHP Graphics draw rectangle

PHP Draw Polygons

PHP provides the ImagePolygon() function to draw polygon.

imagepolygon($image, $points, $numpoints, $color)

Parameters

$image— An image resource, this is returned by the imagecreatetruecolor() function.
$points— An array containing polygon vertices.
$numpoints— Total number of points.
$color— color identified by imagecolorallocate()

Example- imagepolygon()

?php $x = 250; $y = 210; $image=imagecreatetruecolor($x, $y); $white= imagecolorallocatealpha($image, 255, 255, 255, 75); // Draw the polygon imagepolygon($image, array( 10, 10, 50, 140, 100, 200, 220, 180 ), 4, $white); header('Content-type: image/png'); ImagePNG($image); ImageDestroy($image); ?>

Output

PHP Graphics draw polygons

PHP Draw Arcs

PHP provides the ImageArc() function for drawing arcs.

ImageArc($image, $x, $y, $width, $height, $start, $end, $color);

Parameters

$image — An image resource, this is returned by the imagecreatetruecolor() function.
$x, $y— To set the x and y coordinates of the center.
$width, $height— To set the width and height of an arc.
$start— To set the arc start angle in degree.
$end— To set the arc end angle in degree.
$color— Color identified by the imagecolorallocate().

Example- ImageArc()

?php $x = 200; $y = 200; $image=imagecreatetruecolor($x, $y); $white= imagecolorallocatealpha($image, 255, 255, 255, 75); // Draw an arc imagearc($image, 100, 100, 150, 150, 25, 155, $white); header('Content-type: image/png'); ImagePNG($image); ImageDestroy($image); ?>

Output

PHP Graphics draw polygons

PHP Draw Ellipses

PHP provides the ImageEllipse() function to draw an ellipse.

ImageEllipse($image, $x, $y, $width, $height, $color);

Parameters

$image — An image resource, this is returned by the imagecreatetruecolor() function.
$x, $y — To set x and y coordinates of the center.
$width — To set the ellipse width.
$height — To set the ellipse height.
$color — Color identified by the imagecolorallocate().

Example- ImageEllipse()

?php $x = 150; $y = 200; $image=imagecreatetruecolor($x, $y); $yellow = imagecolorallocatealpha($image, 255, 255, 0, 75); // Draw an ellipse imageellipse($image, 70, 100, 100, 150, $yellow); header('Content-type: image/png'); ImagePNG($image); ImageDestroy($image); ?>

PHP Graphics ellipse polygons

PHP Draw Patterned Lines

In PHP, we can draw patterned lines with the help of the ImageSetStyle() and the ImageFilledRectangle() functions. The ImageSetStyle() function sets the style for line drawing.

Parameters

$image— An image resource, this is returned by imagecreatetruecolor() function.
$style— This is an array of pixel colors. To pass this, we have used IMG_COLOR_STYLE in ImageFilledRectangle() function.

Example- ImageSetStyle()

?php $x = 50; $y = 50; $black = 0x000000; $white = 0xFFFFFF; $image=imagecreatetruecolor($x, $y); $style = array($white, $white, $white, $white, $white, $black, $black, $black, $black, $black); ImageSetStyle($image, $style); ImageFilledRectangle($image, 0, 0, 60, 60, IMG_COLOR_STYLED); header('Content-type: image/png'); ImagePNG($image); ImageDestroy($image); ?>

Источник

ImagickDraw::line

This function is currently not documented; only its argument list is available.

Draws a line on the image using the current stroke color, stroke opacity, and stroke width.

Parameters

Return Values

Examples

Example #1 ImagickDraw::line() example

function line ( $strokeColor , $fillColor , $backgroundColor )

$draw -> setStrokeColor ( $strokeColor );
$draw -> setFillColor ( $fillColor );

$draw -> setStrokeWidth ( 2 );
$draw -> setFontSize ( 72 );

$draw -> line ( 125 , 70 , 100 , 50 );
$draw -> line ( 350 , 170 , 100 , 150 );

$imagick = new \ Imagick ();
$imagick -> newImage ( 500 , 500 , $backgroundColor );
$imagick -> setImageFormat ( «png» );
$imagick -> drawImage ( $draw );

header ( «Content-Type: image/png» );
echo $imagick -> getImageBlob ();
>

User Contributed Notes 1 note

// a twist on the above radar screen.
// This makes random colored spokes from a center point

$image = new Imagick ();
$image -> newImage ( $width , $height , new ImagickPixel ( ‘wheat’ ) );
$draw = new ImagickDraw ();
//$draw->setStrokeColor( new ImagickPixel( ‘black’ ) );

$rx = $width / 2 ;
$ry = $height / 2 ;
$total = 2 * M_PI ;
$part = $total / 16 ;
while( $total > 0 )
$ex = $rx + $rx * sin ( $total );
$ey = $ry + $ry * cos ( $total );
$draw -> line ( $rx , $ry , $ex , $ey );
$total -= $part ;

// We need three HEX numbers to create an RGB color code like ‘#FF33DD’.

$draw -> setStrokeColor ( get_random_color () );
>
$image -> drawImage ( $draw );
$image -> setImageFormat ( «png» );
header ( «Content-Type: image/png» );
echo $image ;
exit;

function get_random_color () // Thanks to Greg R. for this nice little function.
for ( $i = 0 ; $i < 6 ; $i ++)
$c .= dechex ( rand ( 0 , 15 ));
>
return «# $c » ;
>
?>

  • ImagickDraw
    • affine
    • annotation
    • arc
    • bezier
    • circle
    • clear
    • clone
    • color
    • comment
    • composite
    • _​_​construct
    • destroy
    • ellipse
    • getClipPath
    • getClipRule
    • getClipUnits
    • getFillColor
    • getFillOpacity
    • getFillRule
    • getFont
    • getFontFamily
    • getFontSize
    • getFontStretch
    • getFontStyle
    • getFontWeight
    • getGravity
    • getStrokeAntialias
    • getStrokeColor
    • getStrokeDashArray
    • getStrokeDashOffset
    • getStrokeLineCap
    • getStrokeLineJoin
    • getStrokeMiterLimit
    • getStrokeOpacity
    • getStrokeWidth
    • getTextAlignment
    • getTextAntialias
    • getTextDecoration
    • getTextEncoding
    • getTextInterlineSpacing
    • getTextInterwordSpacing
    • getTextKerning
    • getTextUnderColor
    • getVectorGraphics
    • line
    • matte
    • pathClose
    • pathCurveToAbsolute
    • pathCurveToQuadraticBezierAbsolute
    • pathCurveToQuadraticBezierRelative
    • pathCurveToQuadraticBezierSmoothAbsolute
    • pathCurveToQuadraticBezierSmoothRelative
    • pathCurveToRelative
    • pathCurveToSmoothAbsolute
    • pathCurveToSmoothRelative
    • pathEllipticArcAbsolute
    • pathEllipticArcRelative
    • pathFinish
    • pathLineToAbsolute
    • pathLineToHorizontalAbsolute
    • pathLineToHorizontalRelative
    • pathLineToRelative
    • pathLineToVerticalAbsolute
    • pathLineToVerticalRelative
    • pathMoveToAbsolute
    • pathMoveToRelative
    • pathStart
    • point
    • polygon
    • polyline
    • pop
    • popClipPath
    • popDefs
    • popPattern
    • push
    • pushClipPath
    • pushDefs
    • pushPattern
    • rectangle
    • render
    • resetVectorGraphics
    • rotate
    • roundRectangle
    • scale
    • setClipPath
    • setClipRule
    • setClipUnits
    • setFillAlpha
    • setFillColor
    • setFillOpacity
    • setFillPatternURL
    • setFillRule
    • setFont
    • setFontFamily
    • setFontSize
    • setFontStretch
    • setFontStyle
    • setFontWeight
    • setGravity
    • setResolution
    • setStrokeAlpha
    • setStrokeAntialias
    • setStrokeColor
    • setStrokeDashArray
    • setStrokeDashOffset
    • setStrokeLineCap
    • setStrokeLineJoin
    • setStrokeMiterLimit
    • setStrokeOpacity
    • setStrokePatternURL
    • setStrokeWidth
    • setTextAlignment
    • setTextAntialias
    • setTextDecoration
    • setTextEncoding
    • setTextInterlineSpacing
    • setTextInterwordSpacing
    • setTextKerning
    • setTextUnderColor
    • setVectorGraphics
    • setViewbox
    • skewX
    • skewY
    • translate

    Источник

    Drawing Lines in PHP

    To Draw Lines using Aspose.Imaging Java for PHP, simply invoke DrawingLines module. Here you can see example code.

     # Create an instance of BmpOptions and set its various properties $create_options = new BmpOptions(); $create_options->setBitsPerPixel(32); \# Define the source property for the instance of BmpOptions $ary=array(); $create_options->setSource(new StreamSource(new ByteArrayInputStream($ary))); \# Create an instance of Image $image=new Image(); $image = $image->create($create_options,100,100); \# Create an instance of Color $color = new Color(); \# Create an instance of Pen $pen = new Pen(); \# Create and initialize an instance of Graphics class $graphic = new Graphics($image); \# Clear the image surface with Yellow color $graphic->clear($color->getYellow()); \# Draw a dotted line by specifying the Pen object having blue color and co-ordinate Points $graphic->drawLine(new Pen($color->getBlue()), 9, 9, 90, 90); $graphic->drawLine(new Pen($color->getBlue()), 9, 90, 90, 9); $solid_brush = new SolidBrush(); $point = new Point(); \# Draw a continuous line by specifying the Pen object having Solid Brush with red color and two point structures $graphic->drawLine(new Pen(new SolidBrush($color->getRed())), new Point(9, 9), new Point(9, 90)); \# Draw a continuous line by specifying the Pen object having Solid Brush with aqua color and two point structures $graphic->drawLine(new Pen(new SolidBrush($color->getAqua())), new Point(9, 90), new Point(90, 90)); \# Draw a continuous line by specifying the Pen object having Solid Brush with black color and two point structures $graphic->drawLine(new Pen(new SolidBrush($color->getBlack())), new Point(90,90), new Point(90,9)); \# Draw a continuous line by specifying the Pen object having Solid Brush with white color and two point structures $graphic->drawLine(new Pen(new SolidBrush($color->getWhite())), new Point(90,9), new Point(9,9)); \# Save all changes. $image->save($dataDir."DrawLinesExample.bmp"); print "Lines have been drawn in image successfully!".PHP_EOL; 

    Download Running Code

    Download Drawing Lines (Aspose.Imaging) from any of the below mentioned social coding sites:

    Источник

Оцените статью