script.php 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. // Define paths and filenames for the counter file and generated image output
  3. $counterFile = "counter.txt";
  4. $imageFile = "counter.png";
  5. // Background image URL for the counter display
  6. $backgroundImageUrl = "https://computer.glamour.ovh/hit-counter/bg.png"; // Replace this with your actual background image URL
  7. // Text settings to be displayed on the image
  8. $customText = "computer_glamour"; // Main headline text
  9. $secondaryText = "amount of hits:"; // Secondary descriptive text
  10. // Coordinates for positioning the text and number on the image
  11. $textPositionX = 5; // X-coordinate for the main text
  12. $textPositionY = 5; // Y-coordinate for the main text
  13. $secondaryTextPositionX = 5; // X-coordinate for the secondary text
  14. $secondaryTextPositionY = 20; // Y-coordinate for the secondary text
  15. $numberPositionX = 5; // X-coordinate for the counter number
  16. $numberPositionY = 40; // Y-coordinate for the counter number
  17. // RGB color definitions for text, secondary text, number, and optional frame
  18. $textColorRGB = ['red' => 255, 'green' => 255, 'blue' => 255]; // Color for the main text
  19. $secondaryTextColorRGB = ['red' => 255, 'green' => 255, 'blue' => 255]; // Color for the secondary text
  20. $numberColorRGB = ['red' => 255, 'green' => 255, 'blue' => 255]; // Color for the counter number
  21. $frameColorRGB = ['red' => 255, 'green' => 0, 'blue' => 255]; // Color for the frame
  22. $drawFrame = true; // Boolean to toggle drawing a frame around the image
  23. // Ensure the counter file exists, and initialize if not
  24. if (!file_exists($counterFile)) {
  25. file_put_contents($counterFile, "0"); // Create the file with an initial value of 0 if it doesn't exist
  26. }
  27. // Open the file for reading and writing (c+ creates the file if it does not exist)
  28. $fp = fopen($counterFile, "c+");
  29. if (flock($fp, LOCK_EX)) { // Lock to synchronize file access
  30. // Read and increment the counter
  31. $number = (int)fread($fp, filesize($counterFile));
  32. $number++; // Increment the counter by 1
  33. // Prepare the file for writing the updated counter
  34. ftruncate($fp, 0); // Clear file content
  35. rewind($fp); // Reset the file pointer to the start of the file
  36. fwrite($fp, (string)$number); // Write the new counter value to the file
  37. // Release the lock on the file
  38. flock($fp, LOCK_UN);
  39. }
  40. fclose($fp); // Close the file
  41. // Load the background image and determine its type (JPEG or PNG)
  42. list($width, $height, $type) = getimagesize($backgroundImageUrl); // Get image dimensions and type
  43. switch ($type) {
  44. case IMAGETYPE_JPEG:
  45. $backgroundImage = imagecreatefromjpeg($backgroundImageUrl); // Load JPEG image
  46. break;
  47. case IMAGETYPE_PNG:
  48. $backgroundImage = imagecreatefrompng($backgroundImageUrl); // Load PNG image
  49. break;
  50. default:
  51. die("Unsupported image format: " . $backgroundImageUrl); // Exit script if image format is not supported
  52. }
  53. // Allocate colors for the text and frame on the image
  54. $frameColor = imagecolorallocate($backgroundImage, $frameColorRGB['red'], $frameColorRGB['green'], $frameColorRGB['blue']);
  55. $textColor = imagecolorallocate($backgroundImage, $textColorRGB['red'], $textColorRGB['green'], $textColorRGB['blue']);
  56. $secondaryTextColor = imagecolorallocate($backgroundImage, $secondaryTextColorRGB['red'], $secondaryTextColorRGB['green'], $secondaryTextColorRGB['blue']);
  57. $numberColor = imagecolorallocate($backgroundImage, $numberColorRGB['red'], $numberColorRGB['green'], $numberColorRGB['blue']);
  58. // Optionally draw a rectangular frame around the entire image
  59. if ($drawFrame) {
  60. imagerectangle($backgroundImage, 0, 0, $width - 1, $height - 1, $frameColor); // Draw the frame
  61. }
  62. // Add the main text, secondary text, and counter number to the image
  63. imagestring($backgroundImage, 7, $textPositionX, $textPositionY, $customText, $textColor); // Draw main text
  64. if (!empty($secondaryText)) {
  65. imagestring($backgroundImage, 5, $secondaryTextPositionX, $secondaryTextPositionY, $secondaryText, $secondaryTextColor); // Draw secondary text if provided
  66. }
  67. imagestring($backgroundImage, 7, $numberPositionX, $numberPositionY, (string)$number, $numberColor); // Draw counter number
  68. // Output the final image as a PNG file
  69. imagepng($backgroundImage, $imageFile); // Save the image to a file
  70. imagedestroy($backgroundImage); // Free up memory used by the image
  71. // Set headers for the browser to interpret the output as an image
  72. header('Content-Type: image/png'); // MIME type for PNG image
  73. header('Content-Disposition: filename="' . $imageFile . '"'); // Suggested filename for download
  74. readfile($imageFile); // Output the image file
  75. // For more information, refer to the Git repository:
  76. // https://git.cyberwa.re/revengeday/hit-counter/src/branch/main
  77. ?>