【问题标题】:Count image views计算图像浏览量
【发布时间】:2011-11-15 15:09:54
【问题描述】:

我想要一个图像的代码来计算图像被查看的次数,而不管图像是在哪个网站上找到的。我想使用 img src 标签,并让 src 指向一个 php 页面,该页面计算该视图,然后返回要查看的图像。我在想这样的事情:

 <img src="www.mywebsiteurl.com/something.php" /> 

我将如何编写“something.php”以便它返回正确的图像文件?我知道如何记录页面浏览量,但如果在这种情况下我必须做一些不同的事情,请告诉我。

【问题讨论】:

    标签: php image views


    【解决方案1】:

    您的脚本需要:

    1. 记录命中。
    2. 设置输出的内容类型(header('Content-Type: image/jpeg');
    3. 输出图片(readfile( $path_to_image ));

    例如,请参阅readfile documentationOutput an Image in PHP

    【讨论】:

      【解决方案2】:

      从 php 加载图像/文件比正常加载文件慢。

      主要缺点:

      • 由于动态查询,大多数浏览器不支持动态查询,因此无法缓存图像。
      • 服务器和 apache 的开销。

      如果您使用数据库并希望在图像加载后增加列,那么人们可以一次又一次地加载您的图像。因此,您需要对每个查询使用条件。

      替代解决方案

      在要显示图像的页面的页脚中输入此代码

      ajax 调用

      $.get("inc_img_view.php?img_id="+<?php echo $img_id; ?>);
      

      【讨论】:

        【解决方案3】:

        这是您可以拥有的最简单的版本:

        <?php
        
        header('Content-type: image/jpeg');
        readfile('somepic.jpg');
        

        【讨论】:

          【解决方案4】:

          您可以使用类似于www.mywebsiteurl.com/image_renderer.php?url=url_to_image 的 URL 调用图像。

          image_rendered.php:

          <?php
          header("Content-Type: image/jpeg");
          $url = urldecode($_GET['url']);
          
          $headers = array(  
          "GET ".$url." HTTP/1.1",   
          "Content-Type: text/xml; charset=UTF-8",  
          "Accept: text/xml",
          "Cache-Control: no-cache",  
          "Pragma: no-cache"
          );
          
          $ch = curl_init($url);  
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
          curl_setopt($ch, CURLOPT_POST, false);  
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
          
          $r = curl_exec($ch);
          
          // you can connect to your database and increment the number of times this image was viewed
          
          echo $r;
          ?>
          

          希望这将为您指明正确的方向。

          【讨论】:

            【解决方案5】:

            是的,你的开端不错。 我会做这样的事情

            <img src="www.mywebsiteurl.com/image_counter.php?src=image.jpg" /> 
            

            在image_counter.php中,取$src = $_GET['src']为图片来源。

            然后 +1 数据库中该图像的匹配。

            那就做吧

            header('Content-type: image/jpeg');
            readfile($src);
            

            【讨论】:

              【解决方案6】:

              这样的事情应该可以工作:

              <?php
              
              $file = $_GET['img'];
              $path = "images/";
              
              // do an increment of views on the image here - probably in a database?
              
              $exifImageType = exif_imagetype($path.$file);
              if ($exifImageType !== false) {
                  $type = image_type_to_mime_type($exifImageType);
              }
              
              header('Content-Type: $type');
              echo file_get_contents($path.$file);
              

              注意:这会是这样的:

              <img src="getimage.php?img=image.jpg" />
              

              在增加视图后显示正确的图像。

              您始终可以添加一些 .htaccess 以使对 /images/anything 的所有调用都通过您的图像文件。这意味着您可以保持 URL 看起来好像它正在获取一个实际的图像文件(例如 /images/logo.jpg 将被重写以实际通过 getimage.php?img=logo.jpg)

              【讨论】:

                猜你喜欢
                • 2015-05-01
                • 1970-01-01
                • 1970-01-01
                • 2020-10-24
                • 2015-03-24
                • 1970-01-01
                • 2018-01-08
                • 2013-11-30
                • 1970-01-01
                相关资源
                最近更新 更多