forked from revengeday/hit-counter
78 lines
No EOL
4.2 KiB
PHP
78 lines
No EOL
4.2 KiB
PHP
<?php
|
|
// Define paths and file names for the counter file and the generated image output
|
|
$counterFile = "example.txt";
|
|
$imageFile = "example.png";
|
|
|
|
// Background image URL for the counter display image
|
|
$backgroundImageUrl = "https://datakra.sh/assets/example_counter.jpg"; // Replace this with your actual background image URL
|
|
|
|
// Text settings to be displayed on the image
|
|
$customText = "Hit Counter!"; // Main headline text
|
|
$secondaryText = "Super cyber, super cool!"; // Secondary descriptive text
|
|
|
|
// Coordinates for positioning the text and number on the image
|
|
$textPositionX = 5; // X-coordinate for the main text
|
|
$textPositionY = 5; // Y-coordinate for the main text
|
|
$secondaryTextPositionX = 5; // X-coordinate for the secondary text
|
|
$secondaryTextPositionY = 15; // Y-coordinate for the secondary text
|
|
$numberPositionX = 5; // X-coordinate for the counter number
|
|
$numberPositionY = 25; // Y-coordinate for the counter number
|
|
|
|
// RGB color definitions for text, secondary text, number, and optional frame
|
|
$textColorRGB = ['red' => 253, 'green' => 252, 'blue' => 1]; // Color for the main text
|
|
$secondaryTextColorRGB = ['red' => 0, 'green' => 255, 'blue' => 0]; // Color for the secondary text
|
|
$numberColorRGB = ['red' => 255, 'green' => 255, 'blue' => 255]; // Color for the counter number
|
|
$frameColorRGB = ['red' => 255, 'green' => 238, 'blue' => 0]; // Color for the frame
|
|
$drawFrame = true; // Boolean to toggle drawing a frame around the image
|
|
|
|
// Increment the visit counter stored in the counter file
|
|
if (!file_exists($counterFile)) {
|
|
file_put_contents($counterFile, "0"); // If the file does not exist, create it with an initial value of 0
|
|
}
|
|
$number = (int)file_get_contents($counterFile); // Read the current count from the file
|
|
$number++; // Increment the counter by 1
|
|
file_put_contents($counterFile, (string)$number); // Write the new count back to the file
|
|
|
|
// Load the background image and determine its type (JPEG or PNG)
|
|
list($width, $height, $type) = getimagesize($backgroundImageUrl); // Get image dimensions and type
|
|
switch ($type) {
|
|
case IMAGETYPE_JPEG:
|
|
$backgroundImage = imagecreatefromjpeg($backgroundImageUrl); // Load JPEG image
|
|
break;
|
|
case IMAGETYPE_PNG:
|
|
$backgroundImage = imagecreatefrompng($backgroundImageUrl); // Load PNG image
|
|
break;
|
|
default:
|
|
die("Unsupported image format: " . $backgroundImageUrl); // Terminate script if image format is not supported
|
|
}
|
|
|
|
// Allocate colors for the text and frame on the image
|
|
$frameColor = imagecolorallocate($backgroundImage, $frameColorRGB['red'], $frameColorRGB['green'], $frameColorRGB['blue']);
|
|
$textColor = imagecolorallocate($backgroundImage, $textColorRGB['red'], $textColorRGB['green'], $textColorRGB['blue']);
|
|
$secondaryTextColor = imagecolorallocate($backgroundImage, $secondaryTextColorRGB['red'], $secondaryTextColorRGB['green'], $secondaryTextColorRGB['blue']);
|
|
$numberColor = imagecolorallocate($backgroundImage, $numberColorRGB['red'], $numberColorRGB['green'], $numberColorRGB['blue']);
|
|
|
|
// Optionally draw a rectangular frame around the entire image
|
|
if ($drawFrame) {
|
|
imagerectangle($backgroundImage, 0, 0, $width - 1, $height - 1, $frameColor); // Draw the frame
|
|
}
|
|
|
|
// Add the main text, secondary text, and counter number to the image
|
|
imagestring($backgroundImage, 3, $textPositionX, $textPositionY, $customText, $textColor); // Draw main text
|
|
if (!empty($secondaryText)) {
|
|
imagestring($backgroundImage, 3, $secondaryTextPositionX, $secondaryTextPositionY, $secondaryText, $secondaryTextColor); // Draw secondary text if provided
|
|
}
|
|
imagestring($backgroundImage, 3, $numberPositionX, $numberPositionY, (string)$number, $numberColor); // Draw counter number
|
|
|
|
// Output the final image as a PNG file
|
|
imagepng($backgroundImage, $imageFile); // Save the image to a file
|
|
imagedestroy($backgroundImage); // Free up memory used by the image
|
|
|
|
// Set headers for browser to interpret the output as an image
|
|
header('Content-Type: image/png'); // MIME type for PNG image
|
|
header('Content-Disposition: filename="' . $imageFile . '"'); // Suggested filename for download
|
|
readfile($imageFile); // Output the image file
|
|
|
|
// For more information, refer to the Git repository:
|
|
// https://git.cyberwa.re/revengeday/hit-counter/src/branch/main
|
|
?>
|