Hướng dẫn what is graphics php?

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.

Nội dung chính

  • Drawing Lines
  • Example: imageline()
  • Output: ImageLine()
  • Drawing Rectangles
  • Output: Draw Rectangle
  • Drawing Polygons
  • Example - imagepolygon()
  • Drawing Arcs
  • Example - ImageArc()
  • Drawing Ellipses
  • Example - ImageEllipse()
  • Drawing Patterned Lines
  • Example - ImageSetStyle()
  • Related Articles
  • What are graphics functions?
  • What is graphics PHP?
  • What is the function used to draw filled in Figure in PHP?
  • What are the features of image in PHP?

Nội dung chính

  • Drawing Lines
  • Example: imageline()
  • Output: ImageLine()
  • Drawing Rectangles
  • Output: Draw Rectangle
  • Drawing Polygons
  • Example - imagepolygon()
  • Drawing Arcs
  • Example - ImageArc()
  • Drawing Ellipses
  • Example - ImageEllipse()
  • Drawing Patterned Lines
  • Example - ImageSetStyle()
  • Related Articles
  • What are graphics functions?
  • What is graphics PHP?
  • What is the function used to draw filled in Figure in PHP?
  • What are the features of image in PHP?

Nội dung chính

  • Drawing Lines
  • Example: imageline()
  • Output: ImageLine()
  • Drawing Rectangles
  • Output: Draw Rectangle
  • Drawing Polygons
  • Example - imagepolygon()
  • Drawing Arcs
  • Example - ImageArc()
  • Drawing Ellipses
  • Example - ImageEllipse()
  • Drawing Patterned Lines
  • Example - ImageSetStyle()
  • Related Articles
  • What are graphics functions?
  • What is graphics PHP?
  • What is the function used to draw filled in Figure in PHP?
  • What are the features of image in PHP?

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.

Drawing 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 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 imagecolorallocate().

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., transparent color, which is filled by imagefill() function.

Output: ImageLine()

Hướng dẫn what is graphics php?

Drawing Rectangles

PHP provides 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 allocate a color for the rectangle using imagecolorallocatealpha() function.

Output: Draw Rectangle

Drawing Polygons

PHP provides ImagePolygon() function to draw polygon.

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

Parameters

$image - An image resource, this is returned by 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

Drawing Arcs

PHP provides ImageArc() function for drawing arcs.

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

Parameters

$image - An image resource, this is returned by 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 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

Drawing Ellipses

PHP provides ImageEllipse() function to draw an ellipse.

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

Parameters

$image - An image resource, this is returned by 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 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);
?>

Drawing Patterned Lines

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

ImageSetStyle($image, $style)

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);
?>

Output

How to add google reCAPTCHA v2 in registration form using PHP
Complete HTML Form Validation in PHP
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP
PHP code to generate Captcha and add in contact form with Validation
How to store Emoji character in MySQL using PHP
PHP File Upload MIME Type Validation with Error Handler
File Upload Validation in PHP
Simple File Upload Script in PHP
jquery file upload progress bar
Star rating in PHP
JavaScript display PDF in the browser using Ajax call
jQuery loop over JSON result after AJAX Success

◀ Previous Article
PHP secure password with password_hash() and verify with password_verify()

What are graphics functions?

Graphic functions is an exercise concerning the graphical recognition of functions of one real variable. The server will give you the graph of a function , whose expression will be hidden.

What is graphics PHP?

Graphics are one of the most compelling aspects of PHP. Equipped with a powerful GD graphics library, it can dynamically manipulate images. It can create GIFs, JPEGs and PNG files. Use of GD graphics library provides PHP programming with another major advantage.

What is the function used to draw filled in Figure in PHP?

PHP | imagefilledrectangle() Function The imagefilledrectangle() function is an inbuilt function in PHP which is used to create a filled rectangle. This function creates a rectangle filled with a given color in the image. The top left corner of the image is (0, 0).

What are the features of image in PHP?

GD and Image Functions ¶.

gd_info — Retrieve information about the currently installed GD library..

getimagesize — Get the size of an image..

getimagesizefromstring — Get the size of an image from a string..

image_type_to_extension — Get file extension for image type..