I wrote a small PHP script last night to do image processing. I was surprised that it works. I needed to accept a file name, rotation angle, and color. The script opens the PNG file, enlarges it a few pixels to ensure that the rotation doesn’t cause edge pixels to get lost, filters it to change the color, and rotates it to the desired angle. All of this while maintaining the alpha blending and using a transparent color for the new pixels that are a result of the rotation.

The problem is that Apache running on the home system has PHP installed and working for this but 1&1, my web page host, doesn’t. They have PHP but it is either the wrong version or doesn’t have the graphics library installed. Either that or I have options or settings set wrong for the 1&1 site to work.

The image manipulation script is probably too slow to server up thousands of small marker images for Google Maps but it’s worth a try. Otherwise, all of the markers I expect to use in the Google maps project need to be created as separate PNG images.

Here is the image modification script:

< ?php
	// Arguments are:
	//	color = rrggbb_color_string
	//	file = png_file_name_string
	//	angle = degrees_to_rotate_number

	$image = $_GET['file'];
	$degrees = $_GET['angle'];
	$color = $_GET['color'];

	list( $red, $green, $blue ) = array( 127, 127, 127 );
	if( strlen( $color ) == 6 )
		list( $red, $green, $blue ) = array( $color[0].$color[1],
	                                         $color[4].$color[5] );

	$red = hexdec( $red );
	$green = hexdec( $green );
	$blue = hexdec( $blue );

	// Resize to ensure that the rotation works as expected. Otherwise, some
	// edges might get cut off. Then filter to the new color and rotate the
	// image.

	list( $width, $height ) = getimagesize( $image );
	$new_width = $width + 4;
	$new_height = $height + 4;

	$source = imagecreatefrompng( $image );
	imagealphablending( $source, true );
	imagesavealpha( $source, true );

	//imagefilter( $source, IMG_FILTER_GRAYSCALE ); Affects final color.
	imagefilter( $source, IMG_FILTER_COLORIZE, $red, $green, $blue, 0 );

	// Create a transparent color for the background
	$bkcolor = imagecolorallocatealpha( $source, 0, 0, 0, 127 );

	$newimage = imagecreatetruecolor( $new_width, $new_height );
	imagealphablending( $newimage, true );
	imagesavealpha( $newimage, true );
	imagefill( $newimage, 0, 0, $bkcolor );

	imagecopy( $newimage, $source, 2, 2, 0, 0, $width, $height );

	$rotate = imagerotate( $newimage, $degrees, $bkcolor, true );
	imagealphablending( $rotate, true );
	imagesavealpha( $rotate, true );

	header( 'Content-type: image/png' );
	imagepng( $rotate );