Created
March 30, 2014 00:54
-
-
Save mfyz/9865575 to your computer and use it in GitHub Desktop.
Simple Image Counter
This file contains hidden or 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 | |
// sample: <img src="counter.php?counter=total_visits"> | |
function hex2rgb($color){ | |
$color = str_replace('#', '', $color); | |
if (strlen($color) != 6){ return array(0,0,0); } | |
$rgb = array(); | |
for ($x=0;$x<3;$x++){ | |
$rgb[$x] = hexdec(substr($color,(2*$x),2)); | |
} | |
return $rgb; | |
} | |
// get params | |
$ref = isset($_GET['ref']) ? $_GET['ref'] : NULL; | |
$counter = isset($_GET['counter']) ? $_GET['counter'] : NULL; | |
$bg = isset($_GET['bg']) ? $_GET['bg'] : NULL; | |
// create file | |
if (isset($_GET['create'])) { | |
touch('counters/'. $counter .'.txt'); | |
} | |
// content | |
if( $counter ){ | |
$file = 'counters/'. $counter .'.txt'; | |
if( file_exists($file) ){ | |
$counter = file_get_contents($file); | |
$counter += 1; | |
// writing | |
file_put_contents($file, $counter); | |
// result | |
$text = $counter; | |
}else{ | |
$text = 'N/A'; | |
} | |
}else{ | |
$text = 'N/A'; | |
} | |
// bg color | |
if( $bg ){ | |
$bg = hex2rgb($bg); | |
}else{ | |
$bg = explode(',', '255,255,255'); | |
} | |
// params | |
$width = 100; | |
$height = 20; | |
$font = 'DaxMedium.ttf'; | |
$fontSize = "10"; | |
// creating image | |
$image = imagecreatetruecolor($width, $height); | |
$bgcolor = imagecolorallocate($image, $bg[0], $bg[1], $bg[2]); | |
$black = imagecolorallocate($image, 0, 0, 0); | |
// background color | |
imagefilledrectangle($image, 0, 0, $width, $height, $bgcolor); | |
// writing text | |
ImageTTFText($image, $fontSize, 0, 0, 15, $black, $font, $text); | |
// display image | |
header("Content-Type: image/PNG"); | |
ImagePng ($image); | |
imagedestroy($image); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment