Add points 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().

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

Источник

php — Add point in the database and the rest of the points in order depending on the point added

I have the following problem:

In a mysql table there are geographical points as follows:

enter image description here

I add a point after point 1 it will become point 2 but the former point 2 must become point 3 and so on .

enter image description here

mysqli_query($con, "INSERT INTO `editare_coordonate` (`id_pdf`,`ord`,`x`,`y`) VALUE('$id_pdf','$ord','','')"); $i_ord = 1; $select_ordonare = mysqli_query($con, "SELECT * FROM `editare_coordonate` WHERE `id_pdf` = '$id_pdf' ORDER BY `ord` ASC"); while($row_ordonare = mysqli_fetch_array($select_ordonare))< mysqli_query($con, "UPDATE `editare_coordonate` SET `ord` = '$i_ord' WHERE `id_pdf` = '$id_pdf' AND `Id` = '".$row_ordonare['Id']."'"); //echo "UPDATE `editare_coordonate` SET `ord` = '$i_ord' WHERE `id_pdf` = '$id_pdf' AND `Id` = '".$row_ordonare['Id']."'
"; $i_ord++; >

I’ve been struggling with this for 2 days and I can’t shake my head 🙁

Answer

Solution:

#tell mysqli to throw exceptions on error mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); #start a transaction $con->begin_transaction(); try < #update the existing ord values $sql = "UPDATE `editare_coordonate` SET ord = ord+1 WHERE id_pdf = ? AND ord >= ?"; $upd = $con->prepare($sql); $upd->bind_param('ii', $id_pdf, $ord); $upd->execute(); # Add new point $sql = "INSERT INTO `editare_coordonate` (`id_pdf`,`ord`,`x`,`y`) VALUE(. )"; $ins = $con->prepare($sql); $ins->bind_param('iiss', $id_pdf, $ord, '', ''); # . Would assume that the x and y points should be added into this query . # so that line should probably be #$ins->bind_param('iiii', $id_pdf, $ord, $x, $y); $ins->execute(); #if you get here, both updates were succesful so # commit both changes together $con->commit(); > catch (Exception $e) < #something went wrong rollback any updates that may have worked before the error $con->rollback(); > 

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

MySQL

DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL. It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Источник

Читайте также:  Check for null value php
Оцените статью