Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-12-28 01:22:08

kuopassa
Plugin Author
From: Porvoo, Finland
Registered: 2008-12-03
Posts: 241
Website

Image view counter?

Is there a plugin or some script which counts how many times an image has been viewed (loaded)?

I’m building a website with image gallery and would like to sort images by popularity. Basically what I’m looking for is dzd_counter_view but just for images. :-)

Offline

#2 2014-12-28 10:15:34

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Image view counter?

You’d have to either load the image through TXP (instead of loading it as a static image) or let the static image request also trigger a request to TXP purely for counting. Not impossible, but I don’t think there’s a plugin for that yet.

Sorting by image view count is not the same as sorting by popularity. There are at least two problems with that:
  1. a 1 year old image with 10 views would appear to be more popular than a 1 day old image with 5 views. It’s hard for a new image to become popular, unless you also take the image age into account when calculating how popular it is.
  2. people tend to click the first few images more than the ones at the bottom, so the popular images get even more hits because they are shown first.

The best way to find out which image is popular is not showing popularity to the visitors and showing the images in random order.

Offline

#3 2014-12-28 13:32:17

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,656
GitHub Twitter

Re: Image view counter?

Maybe the rsx_request_count plugin could do the trick with some changes because it counts the article views into the textpattern table instead the txp_image table:

function rsx_request_count($atts,$thing) {
global $is_article_list,$thisarticle;
if (is_array($atts)) extract($atts);
// are we viewing a single article
if ($is_article_list) return "";
// yes, update the request count for the article and return the value
$result = safe_update('textpattern','request_count = request_count + 1','id = '.$thisarticle['thisid']);
$value = safe_field('request_count','textpattern','id = '.$thisarticle['thisid']);
return isset($noshow) ? "" : ( $result ? $value : -1 );
}

Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#4 2014-12-28 15:48:59

kuopassa
Plugin Author
From: Porvoo, Finland
Registered: 2008-12-03
Posts: 241
Website

Re: Image view counter?

Thanks ruud and Pat64 for your thoughts, they’re appreciated. :-) And thanks for the code idea, Pat64. I’ll check that a bit later.

Offline

#5 2014-12-28 17:19:58

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Image view counter?

Add a column ‘downloads’ to the txp_image table with default value 0.
Then create a plugin that adds a handler for /img/123 urls, where 123, is the image id (code not tested and assumes that all your images are JPG’s):

register_callback('kuo_url_handler', 'pretext');

function kuo_url_handler() {
  $subpath = preg_quote(preg_replace("/http:\/\/.*(\/.*)/Ui","$1",hu),"/");
  $req = preg_replace("/^$subpath/i","/",serverSet('REQUEST_URI'));
  extract(chopUrl($req));

  # handle images
  if (!empty($u1) and 'img' === $u1 and !empty($u2) and is_int($u2))
  { 
    safe_update('txp_image','downloads = downloads + 1','id = '.doSlash($u2));
    txp_die('go get your image', '303', 'http://your.domain.tld/images/'.$u2.'.jpg');
  }
}

You can then sort based on the column ‘downloads’ when displaying images, but you must ensure that the images are called via the /img/123 url instead of directly via /images/123.jpg. Basically you’re creating a wrapper function around the image link which takes care of the counting.

Offline

Board footer

Powered by FluxBB