Skip to content
Advertisement

Hit Counter PHP, TXT, Image and Imagick

Some days Ago I started to create a SaaS Hit counter System, I founded some problems but solved, in the last answer about this I just talked about the problem so no codes have been posted with the solution…

Well, I have 95% created but I need to run the PHP file which contain the script inside the image, I tried to insert some rules inside Htaccess file but got no lucky, Below is the code for who wants to help and use 😉

$number = trim(file_get_contents('visitas.txt'));//for Example: 92

$file = 'visitas.txt'; 
$views = file_get_contents ($file); 
$fdata = intval($views)+1; 
file_put_contents($file, $fdata);

$sources = str_split($number);
array_walk($sources,function(&$item) {$item .= '.png';});



$stack = new Imagick();
foreach( $sources as $source ) {
    $stack->addImage(new Imagick($source));
}

$montage = $stack->montageImage(new ImagickDraw(), '10x1', '40x33', 0, '0');
$montage->writeImage('output.gif');

?>

<img src="output.gif" />

So each refresh in the page which contains this script, one view summ the counter, everything perfect right? nop.. because I need to embed the output.gif in another domains and need to execute the script each time the .gif get reloaded

So I tried to put this code in my Htaccess but just did an error 500 =/

RewriteEngine On
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^output.gif$ output.php[R=301,L]

Heres a print of the Counter working:

counter

Someone knows how can I run my .gif as php?

Advertisement

Answer

For everyone who wants to use, I did this:

  • Add this rules to my root htaccess
RewriteEngine On
RewriteRule ^/output.gif$ /output.php [L]
  • Add this codes to output.php
$montage->setImageFormat('gif');
header("Content-Type: image/gif");
echo $montage->getImageBlob();

And removed:

$montage->writeImage('output.gif');
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement