【问题标题】:how to make simple PHP visitor counter for my website如何为我的网站制作简单的 PHP 访客计数器
【发布时间】:2019-12-05 19:49:12
【问题描述】:

首先,对于任何具有 php 知识的人来说,这应该很容易。我正在尝试计算访问过的用途并将数字显示为图像,图像名称为“0.png”-“9.png”,它根据计数选择它,并且应该将其添加到 canvas.png这是一个 296x76 的图像,用作 png 大小为 56x76 的背景。计数器有效,但图像不显示

这里的错误列表后跟奇怪的文字

libpng warning: iCCP: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG
libpng warning: iCCP: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG
libpng warning: iCCP: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG
libpng warning: iCCP: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG
libpng warning: iCCP: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG

//html

<div>
<p> <center> Number of visitors:</center> </p>
<center><img alt="Visitor counter" src="counter.php" /></center>
</div>

//counter.php

<?php
session_start();
$counter_name = "counter.txt";

// Check if a text file exists.
//If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
    $f = fopen($counter_name, "w");
    fwrite($f,"0");
    fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);

// Has visitor been counted in this session?
// If not, increase counter value by one
if(!isset($_SESSION['hasVisited'])){
    $_SESSION['hasVisited']="yes";
    $counterVal++;
    $f = fopen($counter_name, "w");
    fwrite($f, $counterVal);
    fclose($f);
}

$counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT);
$chars = preg_split('//', $counterVal);
$im = imagecreatefrompng("canvas.png");

$src1 = imagecreatefrompng ("digits/$chars[1].png");
$src2 = imagecreatefrompng ("digits/$chars[2].png");
$src3 = imagecreatefrompng ("digits/$chars[3].png");
$src4 = imagecreatefrompng ("digits/$chars[4].png");
$src5 = imagecreatefrompng ("digits/$chars[5].png");

imagecopymerge($im, $src1, 0, 0, 0, 0, 56, 76, 100);
imagecopymerge($im, $src2, 60, 0, 0, 0, 56, 76, 100);
imagecopymerge($im, $src3, 120, 0, 0, 0, 56, 76, 100);
imagecopymerge($im, $src4, 180, 0, 0, 0, 56, 76, 100);
imagecopymerge($im, $src5, 240, 0, 0, 0, 56, 76, 100);

// Output and free from memory
header('Content-Type: image/png');
echo imagepng($im);
imagedestroy($im);
?>

它应该给出计数的图像并将其显示在我的 index.html 上,但我看到的只是一个“?”图片应该在哪里

【问题讨论】:

    标签: php html png


    【解决方案1】:

    您在这里遇到了一些严重的效率问题,但让我们从基础开始。

    首先,这一行: echo imagepng($im);

    取出回声。如果您不将文件名传递给 imagepng(),则该函数将输出图像数据。但是,您的回声显示的是该函数的返回值,它是一个布尔值。因此,回声实际上会添加破坏其余图像数据的数据。

    接下来,您的灰度警告是由于 canvas.png 是灰度图像。您需要打开它并将其重新保存为真彩色图像,以便在复制其他图像时,它们的颜色存在于 canvas.png 调色板中。

    第三,为计数器数据使用文本文件是个坏主意。在任何时候,如果您有两个同时访问者,您最终会得到不正确的数据。数据库将防止这种情况发生。

    第四,为每个数字使用单独的图像文件效率低下。我认为您使用的是程式化的数字,因此您不能只写文本(除非您有原始字体)。有了这个假设,您应该将所有数字并排放在一张图像中,然后根据需要复制这些数字。

    或者,如果您想使用单独的数字图像,请使用 PHP 简单地为单个 img 标记并排生成 HTML。这比使用 GD 动态生成新图像要高效得多。

    第五,你有一个 $counterVal,它是一个字符串。您可以像访问数组一样访问 $counterVal 中的各个字符:

    $counterVal[0] 是第一个字符/数字等...你根本不需要 preg_split()。

    最后,我始终建议人们不要使用访问计数器。没有任何好处。它永远不准确,如果数字很低,那么您只会向世界其他地方展示该页面不受欢迎。如果这个数字很高,那么其他访问者无论如何都不会真正关心,而您只是在数千或数百万次额外的 PHP 执行上花费了大量的 Web 服务器资源来生成这些数据。

    如果您对页面的流量感到好奇,最准确的机制是分析您的网络服务器日志。它会告诉您每页的点击次数以及更多。

    【讨论】:

      【解决方案2】:

      echo imagepng 不需要,因为默认情况下 imagepng 函数会将输出发送到浏览器。请参阅imagepng 的文档。

      据我了解,您需要 $counterVal 来保存 5 位数字。 str_pad 函数不会生成具有固定字符数的字符串。它只是在输入字符串的左侧或右侧添加给定数量的字符。请参阅str_pad 的文档。为确保 $counterVal 有 5 位数字,可以使用以下代码:

      $len          = strlen($counterVal);
      $zero_count   = (5 - $len);
      $zero_padding = str_repeat("0", $zero_count);
      $counterVal   = $zero_padding . $counterVal;
      

      您发布的错误消息表明 canvas.png 图像是灰度图像,但数字图像是 RGB 格式。

      在灰度图像中,每个像素的值介于 0 到 255 之间。在 RGB 图像中,每个像素都有一个由 (r, g, b) 给出的三元组值,其中 r、g 和 b 的值分别在 0 到 255 之间.

      您可以尝试使用imagecopymergegray,它将 RGB 像素转换为灰度,然后再从源复制到目标。如果这不起作用,那么您可以使用图像编辑器将画布图像和数字图像转换为相同的格式。例如,两者都可以转换为灰度或 RGB。

      正如@jhilgeman 的回答中提到的,使用 GD 为每次访问生成新的访客计数器图像效率不高。简单地输出数字的图像标签会好得多。

      【讨论】:

        【解决方案3】:

        让它变得简单,我希望这个答案对你有所帮助,回显具有观看者数量的 txt 文件并为其添加背景图像

        <div>
                <p> <center> Number of visitors:</center> </p>
                <center>  <div style="    position: relative;
                width: 24px;
               
                background-image: url(https://image.freepik.com/free-vector/elegant-white-background-with-shiny-lines_1017-17580.jpg);
                padding: 50px"><?php echo file_get_contents("counter.txt"); ?></div>
                </center>
                </div>

        【讨论】:

          猜你喜欢
          • 2012-06-23
          • 2017-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多