Tag Archives: php

Drawing mandalas with PHP for my Papa

A sample mandala I have generated

The earliest memories I have of computers is a contraption my dad rigged up that would draw mandalas on a screen. Later in my life he wrote software that would plot large mandalas on his huge plotters.

In my last project, I learned about image generation using PHP and realized that it might be possible to use PHP functions to draw mandalas. After a couple hours of relearning trig, I was able to create a script that outputs a .png image of a mandala based on the size and number of points that you give it.

Give it a try:



Source code:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
// Prints a mondala
$points = $_GET['points'];
$size = $_GET['size'];
// Test the size
if($size < 1 || $size > 1024){
	echo "Please use a better size";
// Test the number of points
}elseif($points < 1 || $points > 100){
	echo "Please use a more reasonable number or points";
}else{
	// Set the radius to half of the size of the image, so it fills the image
	$radius = $size/2;
	// Find the number of degrees between points (in radians)
	$degrees = deg2rad(360/$points);
	// The offset used to push the whole drawing into the first quadrant (because our math would 
	//		normally put it centered on 0,0
	$offset = $size/2;
 
	// Create the image
	$im = imagecreatetruecolor($size, $size);
	// Set the background color white
	$background = imagecolorallocate($im, 255, 255, 255);
	imagefill($im, 0, 0, $background);
	// Set the color of the lines black
	$lines = imagecolorallocate($im, 0, 0, 0);
 
 
	// Until we have made our way around the circle
	while($angle < deg2rad(360)){
		// Increase the angle to point to the next point
		$angle = $angle + $degrees;
 
		// Find the new points
		$x = $radius * cos($angle) + $offset;
		$y = $radius * sin($angle) + $offset;
		// Add those points to the array of points
		$pointsArr[] = array($x, $y);
	}
 
	// For each point
	while($from = array_pop($pointsArr)){
		// For each point that has not yet been plotted
		foreach($pointsArr as $to){
			// Draw a line
			imageline($im, $from[0], $from[1], $to[0], $to[1], $lines);
		}
	}
 
	// Output the image
	header ("Content-type: image/png");
	imagepng($im);
	// Clear the image form memory
	imagedestroy($im);
}
?>

Get alerts when the emails you send are opened

Inspired by spypig.com, a site that helps you track when your emails are opened, I decided to see if I could write a script with the same functionality. It tuned out to be a rather interesting project.

Here it is: http://www.classicalcode.com/t.php

It works by generating an image which you embed in your email. Then when your recipeint reads their email, their email client downloads that image. Downloading that image triggers a script on my server which then sends you an email.

In order to make the image look less suspicious I implemented a feature where the image is a string of text of the senders choosing. Example:

An Example of a tracker image from my email tracking project

Give it a try!

Only the first and last letters of a word need be in the right place

After my dad showed me an article in the Funny Times about how the only letters that really matter when we read are the first and last, I thought it would be fun to write a little script to scramble the inner letters of words.

Give it a try:

Below is the PHP script. It accepts a POST value named “text” and returns it scrambled:

1
2
3
4
<?=preg_replace_callback(
    "/(?<=[a-zA-Z])([a-zA-Z]+)(?=[a-zA-Z])/",
    create_function('$n', 'return str_shuffle($n[1]);')
    , $_POST['text'])?>
Page 2 of 212