How to register image downloads in php | Familiar Sight | | Join Date: Jan 2009
Posts: 165
| |
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: -
<!-- //SF-Code---somewstuff here--// -->
-
<a href="http://www.my-website.com/check.php?key=$sup_id\"
-
onclick=\"NewWindow(this.href,'','800','600','yes','default'); return false\"
-
onfocus=\"this.blur()\" >
-
<img src=\"http://www.my-website.com/images/logo2.gif\"
-
alt=\"Membership Click to Verify - Before you Buy\"
-
border=\"0\" >
-
</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
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | 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: - 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;
- 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;
- Then, on that page, when it is loaded, update a counter (in database, flat-file, etc).
- 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
| | | 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: - <!-- //SF-Code---somewstuff here--// -->
-
<a href="http://www.my-website.com/check.php?key=$sup_id\"
-
onclick=\"NewWindow(this.href,'','800','600','yes','default'); return false\"
-
onfocus=\"this.blur()\" >
-
<img src=\"http://www.my-website.com/image_load.php?id=$sup_id\"
-
alt=\"Membership Click to Verify - Before you Buy\"
-
border=\"0\" >
-
</a>
and the scrip updates the product database: - <?php
-
/*
-
* image_load.php
-
*
-
* Called by the sales page
-
*
-
*/
-
$prod_id=$_GET["id"];
-
-
$sql = "SELECT image_count FROM products WHERE prod_id = '$prod_id' ";
-
$result = mysql_query($sql) or die("could not execute FIND CLIENT query"). mysql_error();
-
-
$num = mysql_num_rows($result);
-
-
if ($num == 0 ) { // if the client does not exist
-
$message1 = "That user id <br>
-
is not registered on the system.<br>
-
";
-
-
require_once("check_err.php");
-
exit();
-
} // endif
-
-
$sql = "UPDATE products SET
-
image_count = image_count+1
-
WHERE prod_id = '$prod_id' ";
-
-
mysql_query($sql) or die("could not execute PROFILE update query". mysql_error());
-
-
HERE DO THE
-
imagecreatefromgif(img/logo2.gif);
-
-
AND DISPLAY IT.
-
-
how ???
-
?>
-
My question is though, why do the
imagecreatefromgif function ?
Why can I not just echo the image like this ? -
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:
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | 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. -
<?php
-
-
$img = file_get_contents('/path/to/img.gif');
-
-
header('Content-type: image/gif');
-
-
echo $img;
-
Mark.
| | Familiar Sight | | Join Date: Jan 2009
Posts: 165
| | | re: How to register image downloads in php
Thanks Mark.
Another way I found is: - header('Content-Type: image/gif');
-
@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
| | | 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.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | 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
| | | 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: - // Define the correct path to "logo2.gif" ...
-
header('Content-Type: image/gif');
-
@readfile('http://www.my-site.com/images/seal_v1_175.png');
-
@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
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | re: How to register image downloads in php Quote:
Originally Posted by jeddiki 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: - // Define the correct path to "logo2.gif" ...
-
header('Content-Type: image/gif');
-
@readfile('http://www.my-site.com/images/seal_v1_175.png');
-
@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 -
<?php
-
-
// Load the image option from URL, set default to 1
-
$img = (isset($_GET['img']) ? $_GET['img'] : 1;
-
-
switch(img) {
-
case 1:
-
// Load gif
-
header('Content-type: image/gif');
-
$img_data = readfile('/path/to/gif.gif');
-
break;
-
case 2:
-
// Load png
-
header('Content-type: image/png');
-
$img_data = readfile('/path/to/png.png');
-
break;
-
-
default:
-
break;
-
}
-
-
echo $img_data;
-
And then your img src: -
<img src="htxp://yoursite.com/image.php?img=1" /><br />
-
<img src="htxp://yoursite.com/image.php?img=2" />
-
Mark.
P.S. Don't bother using the @ symbol - it suppresses any errors and is expensive.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|