Count image views

前端 未结 6 1878
余生分开走
余生分开走 2021-01-25 08:14

I would like to have code for an image that counts the number of times the image is viewed, regardless of what site the image is found on. I want to use the img src tag, and hav

相关标签:
6条回答
  • 2021-01-25 08:44

    Yes you're on a good start. I'd do something like this

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

    In image_counter.php, take $src = $_GET['src'] for image source.

    Then +1 the hit for that image in the database.

    Then do

    header('Content-type: image/jpeg');
    readfile($src);
    
    0 讨论(0)
  • 2021-01-25 08:46

    Your script needs to:

    1. Record the hit.
    2. Set the content type of the output ( header('Content-Type: image/jpeg'); )
    3. Output the image ( readfile( $path_to_image ) );

    For examples, please see the readfile documentation and Output an Image in PHP.

    0 讨论(0)
  • 2021-01-25 08:55

    You can call an image using a URL similar to: www.mywebsiteurl.com/image_renderer.php?url=url_to_image.

    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;
    ?>
    

    Hope this will point you into the right direction.

    0 讨论(0)
  • 2021-01-25 08:57

    This is the most barebones version you can have:

    <?php
    
    header('Content-type: image/jpeg');
    readfile('somepic.jpg');
    
    0 讨论(0)
  • 2021-01-25 09:01

    Something like this should work:

    <?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);
    

    Note: this would make something like:

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

    show the correct image after incrementing the views.

    You could always add some .htaccess to make all calls to /images/anything go through your image file. It'd mean you could keep the URL looking as though it's getting an actual image file (e.g. /images/logo.jpg would be re-written to actually go through getimage.php?img=logo.jpg)

    0 讨论(0)
  • 2021-01-25 09:02

    Loading images / files from php is slow than normal loading files.

    Main cons:

    • Images can't be cached due to dynamic query most browser dont support on dynamic queries.
    • Overhead on sever and apache both.

    If you use database and want to increment column after image loads than a person can load your image again and again. So you need to use conditions on every query.

    Alternative Solution

    Enter this code in footer of page on which you want to show images

    A ajax call

    $.get("inc_img_view.php?img_id="+<?php echo $img_id; ?>);
    
    0 讨论(0)
提交回复
热议问题