Category Archives: PHP

Rounded corners using PHP and the GD library

Why GD library?

I needed to add rounded corners to some pictures and didn’t want to modify the source files in Photoshop (I think it’s a bad idea to modify original pictures if the effect can be added dynamically).

First, I thought CSS would do the trick. But after finding some tutorials on the web (some also use Javascript), I realized that to get it working in most web browsers, it doesn’t seem that easy, and it generally means that you have to create wrapping DIVs. Continue reading

Distance calculation from latitude and longitude in PHP

geographical distance calculationGeographical distance calculation is a common cause of headaches. Indeed, if the earth were flat, you could simply use Pythagoras’ theorem, which states that in a right triangle, the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides (you probably remember its formula a2 + b2 = c2 from high school). But of course, the Earth is not flat…

In the following distance calculation function, I use the haversine formula to calculate the distance between two points on the Earth’s surface, feeding it the latitude and longitude of both points. Continue reading

PHP point-in-polygon algorithm

The point-in-polygon algorithm allows you to programmatically check if a particular point is inside a polygon or outside of it. A common way to tackle the problem is to count how many times a line drawn from the point (in any direction) intersects with the polygon boundary. If the line and the polygon intersect an even number of times (or not at all), then the point is outside. If they intersect an odd number of times, the point is inside. It is true even for complex forms that have a lot of coordinates and thus create a very precise boundary.

Let’s see a sample image before we get to the code.

Continue reading