Happy Valentines Day!

I have a special valentines day random heart generator. This script below will create 100 hearts in random shapes and sizes as a png. Just hit refresh to try it out.

random hearts png

It draws hearts the simple way, two circles and a triangle. As with most scripts the width, height size ect are all pretty easily editable. Enjoy.

PHP Heart Generator that Draws Hearts using GD.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
 
$imwidth = 500;
$imheight = 150;
$im = imagecreate($imwidth,$imheight);
$background_color = imagecolorallocate($im,255,255,255);
$hearts = 100;
 
function drawheart($im){
    global $imwidth,$imheight;
 
    $leftx = rand(0,$imwidth);
    $lefty = rand(0,$imheight);
    $circle_diameter = rand(3,$imwidth/12);
    $rightx = $leftx + $circle_diameter * .9;
    $righty = $lefty;
    $circle_radius = $circle_diameter/2;
 
    $points = array($leftx-$circle_radius,$lefty,$leftx+$circle_radius,$lefty+$circle_diameter+5,$rightx+$circle_radius,$righty,$leftx-$circle_radius,$lefty);
 
    $color = imagecolorallocate($im,255,0,0);
    imagefilledellipse($im,$leftx,$lefty,$circle_diameter,$circle_diameter,$color);
    imagefilledellipse($im,$rightx,$righty,$circle_diameter,$circle_diameter,$color);
    imagefilledpolygon($im,$points,4,$color);
    return $im;
}
;
for($i=0;$i<$hearts;$i++){
    $im = drawheart($im);
}
header('Content-type:image/png');
imagepng($im);
imagedestroy($im);
?>

Have a happy fun filled valentines day and enjoy your loved ones.

Bookmark and Share

Creating a distance between cities using the geolite data that I’ve used in the the mapping IP-2-Location. If you look at longitude and latitude points in decimal format instead of the degrees:min:seconds then you can pretty much figure out how you calculate distance. Its the same way you do when you measure the distance between two points on a sphere.

Surprisingly, php has no support that I know of to convert degrees to radians, so I first built that, then the rest is just self explanatory.

“The world is a book and those who do not travel read only one page.” - St. Augustine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
// no native function in php4/5
function toRadians($degrees)
{
    return $degrees * pi() / 180;
}
$lat1 = $_POST['lat1'];
$lat2 = $_POST['lat2'];
$lon1 = $_POST['lon1'];
$lon2 = $_POST['lon2'];
$R = 6371; // km radius of earth
$dLat = toRadians($lat2-$lat1);
$dLon = toRadians($lon2-$lon1);
$a = sin($dLat/2) * sin($dLat/2) +
        cos(toRadians($lat1)) * cos(toRadians($lat2)) *
        sin($dLon/2) * sin($dLon/2);
$ax = 1-$a;
$c = 2 * atan2(sqrt($a), sqrt($ax));
$d = $R * $c;
?>
<html>
	<body
<div >
	<?= 'Distance:'.$d ?
	</div>
<div>
<form action="" method="post">
		<fieldset><label>Lat1</label>
<input type="text" name="lat1" size=4></fieldset>
		<fieldset><label>Lat2</label>
<input type="text" name="lat2" size=4></fieldset>
		<fieldset><label>Lon1</label>
<input type="text" name="lon1" size=4></fieldset>
		<fieldset><label>Lon2</label>
<input type="text" name="lon2" size=4></fieldset>
<input type="submit">
		</form>
</div>
 
	</body>
</html>
Bookmark and Share

AJAX and PHP combined together in a simple chat message server.  This uses XMLHTTPRequest to call itself with the appropriate GET variables to trigger a read or write to a server-side file named chit-chat.dat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
<?php
$filename = 'chit-chat.dat';
 
if(isset($_GET['msg'])){
$msg = sanitize($_GET['msg']);
$conv = file_get_contents($filename);
file_put_contents($filename,$conv.$msg);
exit;
}
if(isset($_GET['conv'])){
$conv = file_get_contents($filename);
echo $conv;
exit;
}
function sanitize($var){
//sanitize here
return $var;
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<script type="text/javascript">
 
function createXMLHttpRequest() {
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (typeof ActiveXObject != "undefined") {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
throw new Error("XMLHttpRequest not supported");
}
}
 
function sendMsg(msg){
request = createXMLHttpRequest();
request.open("GET", "index.php?msg="+msg, true);
request.onreadystatechange = function() {
//
}
request.send(null);
}
function rcvMsg(){
request = createXMLHttpRequest();
request.open("GET", "index.php?conv=1", true);
request.onreadystatechange = function() {
if (request.readyState == 4 &#038;& request.status == 200) {
var node = document.createTextNode(request.responseText);
document.getElementById("output").appendChild(node);
}
}
request.send(null);
}
rcvMsg();</script>
<div id="output">
</div>
<input type="text" id="msg_form">
<input type="submit" value="send" onClick="sendMsg(document.getElementById('msg_form').value);rcvMsg();">
Bookmark and Share

« Previous PageNext Page »