Created
November 1, 2012 21:17
-
-
Save scardine/3996599 to your computer and use it in GitHub Desktop.
Generate a marker with a given background and foreground colour.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* .htaccess | |
RewriteRule markers?/([0-9a-fA-F]{6})/([0-9a-fA-F]{6})/([^/]+)/marker.png markers/index.php?bg=$1&fg=$2&icon=$3 | |
*/ | |
header('Content-type: image/png'); | |
// read parameters: icon file, foreground and background colors | |
$bgc = sscanf(empty($_GET['bg']) ? 'FFFFFF' : $_GET['bg'], '%2x%2x%2x'); | |
$fgc = sscanf(empty($_GET['fg']) ? '000000' : $_GET['fg'], '%2x%2x%2x'); | |
$icon = empty($_GET['icon']) ? 'base.png' : $_GET['icon'] . '.png'; | |
// read image information from template files | |
$shadow = imagecreatefrompng("../static/img/marker/shadow.png"); | |
$bg = imagecreatefrompng("../static/img/marker/bg.png"); | |
$fg = imagecreatefrompng("../static/img/marker/" . $icon); | |
$base = imagecreatefrompng("../static/img/marker/base.png"); | |
imagesavealpha($base, true); // for the "shadow" | |
// loop over every pixel | |
for($x=0; $x<imagesx($base); $x++) { | |
for($y=0; $y<imagesy($base); $y++) { | |
$color = imagecolorsforindex($bg, imagecolorat($bg, $x, $y)); | |
// templates are grayscale, any channel serves as alpha | |
$alpha = ($color['red'] >> 1) ^ 127; // 127=transparent, 0=opaque. | |
if($alpha != 127) { // if not 100% transparent | |
imagesetpixel($base, $x, $y, imagecolorallocatealpha($base, $bgc[0], $bgc[1], $bgc[2], $alpha)); | |
} | |
// repeat for foreground and shadow with foreground color | |
foreach(array($shadow, $fg) as $im) { | |
$color = imagecolorsforindex($im, imagecolorat($im, $x, $y)); | |
$alpha = ($color['red'] >> 1) ^ 127; | |
if($alpha != 127) { | |
imagesetpixel($base, $x, $y, imagecolorallocatealpha($base, $fgc[0], $fgc[1], $fgc[2], $alpha)); | |
} | |
} | |
} | |
} | |
// spit image | |
imagepng($base); | |
// destroy resources | |
foreach(array($shadow, $fg, $base, $bg) as $im) { | |
imagedestroy($im); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment