Connecting Tech Pros Worldwide Help | Site Map

How to register image downloads in php

Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#1: Aug 23 '09
Hi,


I am giving cleints an image to use as a kind of "trust seal" and I want
to record the number of times it is diplayed, is that possible with
php, of is this a javascript project ?

This is what I mean:

I will give a client this code to place on their website:

Expand|Select|Wrap|Line Numbers
  1. <!-- //SF-Code---somewstuff here--// --> 
  2. <a href="http://www.my-website.com/check.php?key=$sup_id\" 
  3.  onclick=\"NewWindow(this.href,'','800','600','yes','default'); return false\" 
  4.  onfocus=\"this.blur()\" >
  5.  <img src=\"http://www.my-website.com/images/logo2.gif\" 
  6.  alt=\"Membership Click to Verify - Before you Buy\" 
  7.  border=\"0\" >
  8. </a> 
Now I will be recording the number of times the image is clicked
on, but I also want to record the number of times that the
image is downloaded from my website.

How would I do this ?

Thanks
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: Aug 23 '09

re: How to register image downloads in php


It is possible, yes, although there may be an easier way to do it via your server (be it Apache, IIS, etc).

Anyway, the way you would do it is:
  1. Give your client a landing page URL like so (you can adapt it to your needs): http://yoursite.com/path/to/dir/index.php?img=logo.jpg;
  2. On that page, using GD, load the gif using imagecreatefromgif() and display it - look at the examples on php.net to see how you achieve this;
  3. Then, on that page, when it is loaded, update a counter (in database, flat-file, etc).
  4. Hey presto.

Give it a shot, and then if you need further assistance, just say the word!

Mark.
Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#3: Aug 24 '09

re: How to register image downloads in php


Thanks for your reply,

OK - I have changed the <img> tag so that it now runs a script.

like this:

Expand|Select|Wrap|Line Numbers
  1. <!-- //SF-Code---somewstuff here--// --> 
  2. <a href="http://www.my-website.com/check.php?key=$sup_id\" 
  3.  onclick=\"NewWindow(this.href,'','800','600','yes','default'); return false\" 
  4.  onfocus=\"this.blur()\" >
  5.  <img src=\"http://www.my-website.com/image_load.php?id=$sup_id\" 
  6.  alt=\"Membership Click to Verify - Before you Buy\" 
  7.  border=\"0\" >
  8. </a>  

and the scrip updates the product database:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. /*
  3. *  image_load.php
  4. *
  5. * Called by the sales page
  6. *
  7. */        
  8. $prod_id=$_GET["id"];
  9.  
  10. $sql = "SELECT image_count FROM products WHERE prod_id = '$prod_id' ";
  11. $result = mysql_query($sql)    or die("could not execute FIND CLIENT query"). mysql_error();  
  12.  
  13. $num = mysql_num_rows($result);
  14.  
  15. if ($num == 0 ) { //  if the  client does not exist
  16.    $message1 = "That user id <br>
  17.   is not registered on the system.<br>
  18.   "; 
  19.  
  20. require_once("check_err.php");
  21.   exit(); 
  22. }  // endif    
  23.  
  24. $sql = "UPDATE products SET 
  25.          image_count   = image_count+1
  26.          WHERE prod_id = '$prod_id' ";
  27.  
  28. mysql_query($sql) or die("could not execute PROFILE update query". mysql_error());
  29.  
  30. HERE DO THE 
  31. imagecreatefromgif(img/logo2.gif);
  32.  
  33. AND DISPLAY IT.
  34.  
  35. how ???
  36. ?>    
  37.  
My question is though, why do the
imagecreatefromgif function ?

Why can I not just echo the image like this ?
Expand|Select|Wrap|Line Numbers
  1. echo "<img src=\"http://www.support-focus.com/img/logo2.gif>";

Also, if I have to do the GD imagecreatefromgif() function ,

how do I display the image aferwards ? is it with the
<img > tag - brings me back to the first question :confused:
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#4: Aug 24 '09

re: How to register image downloads in php


Ignore what I said - you can just read the contents of the file using file_get_contents(), then use header() to set the correct content-type (mime-type), and then echo the image contents.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $img = file_get_contents('/path/to/img.gif');
  4.  
  5. header('Content-type: image/gif');
  6.  
  7. echo $img;
  8.  
Mark.
Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#5: Aug 25 '09

re: How to register image downloads in php


Thanks Mark.

Another way I found is:

Expand|Select|Wrap|Line Numbers
  1. header('Content-Type: image/gif');
  2. @readfile('http://www.my_site/img/logo2.gif'); 
That seems to be working ok

Thanks for your help.
Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#6: Aug 25 '09

re: How to register image downloads in php


BTW my image does not look as clear as my origional.

The natural size of the image that I made with Fireworks
is 300 (width) x 120 pixels.

But I want to display a smaller version of about
200 px wide.

The clarity is not so good.

Is there a way to display image at a smaller size and maintain quality of image ?

If I use GD and resample it, will the clarity be batter ?

Or should I just re-create my image in Fireworks ?

In Fireworks, the original is a png file, maybe Fireworks
will let me export it as a smaller version ?

What do think is the best way - (I dont want to load up my server with
GD work if I don't have to )

Thanks.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#7: Aug 25 '09

re: How to register image downloads in php


Nah, just make a new image in Fireworks; pointless using GD.

If the filetype is originally a PNG, display it as a PNG.

Mark.
Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#8: Aug 25 '09

re: How to register image downloads in php


The sizes of the files types vary a lot

.png is 35k whereas .gif is only 3k bytes

so I am trying to compare the quality of the two
images together on the same page.

I am using this:

Expand|Select|Wrap|Line Numbers
  1. // Define the correct path to "logo2.gif" ...
  2. header('Content-Type: image/gif');
  3. @readfile('http://www.my-site.com/images/seal_v1_175.png');
  4. @readfile('http://www.my-site.com/images/seal_v1_175.gif'); 
But I only get I file displaying !

How should I write this function so that I get both ?

Thanks
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#9: Aug 25 '09

re: How to register image downloads in php


Quote:

Originally Posted by jeddiki View Post

The sizes of the files types vary a lot

.png is 35k whereas .gif is only 3k bytes

so I am trying to compare the quality of the two
images together on the same page.

I am using this:

Expand|Select|Wrap|Line Numbers
  1. // Define the correct path to "logo2.gif" ...
  2. header('Content-Type: image/gif');
  3. @readfile('http://www.my-site.com/images/seal_v1_175.png');
  4. @readfile('http://www.my-site.com/images/seal_v1_175.gif'); 
But I only get I file displaying !

How should I write this function so that I get both ?

Thanks

You can't.

However, you could, via a URL parameter decide which to load.

htxp://yoursite.com/image.php?img=1
htxp://yoursite.com/image.php?img=2

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // Load the image option from URL, set default to 1
  4. $img = (isset($_GET['img']) ? $_GET['img'] : 1;
  5.  
  6. switch(img) {
  7.     case 1:
  8.         // Load gif
  9.         header('Content-type: image/gif');
  10.         $img_data = readfile('/path/to/gif.gif');
  11.         break;
  12.     case 2:
  13.         // Load png
  14.         header('Content-type: image/png');
  15.         $img_data = readfile('/path/to/png.png');
  16.         break;
  17.  
  18.     default:
  19.         break;
  20. }
  21.  
  22. echo $img_data;
  23.  
And then your img src:
Expand|Select|Wrap|Line Numbers
  1. <img src="htxp://yoursite.com/image.php?img=1" /><br />
  2. <img src="htxp://yoursite.com/image.php?img=2" />
  3.  
Mark.

P.S. Don't bother using the @ symbol - it suppresses any errors and is expensive.
Reply