how get PHP script to work as img source?

SilverNodashi

Expert Member
Joined
Oct 12, 2007
Messages
3,337
I want to track some usage stats on email, and thought of putting the image that I use in a tracking script, and then add that to the email.

But, I can't get it to work. The code I have is as follows:

HTML:
<img src="http://www.mydomain.com/newsletter/newsletter.php?campaign=14Dec09&uid=45" />

and from what I have read on the internet, this should work, but as soon as I put it into an HTML file, it doesn't show the image. I get a broke image sign, yet accessing http://www.mydomain.com/newsletter/newsletter.php?campaign=14Dec09&uid=45 directly in the browser shows the image fine.


the PHP script itself has the following code:

HTML:
<HTML>
<TITLE>Season Greetings from SoftDux</TITLE>
</HTML>
<body>
<center>
<img src="http://www.mydomain.com/newsletter/dec_2009.jpg">
</center>
<?php
// PHP code goes here

Does anyone know how todo something like this?
 

ColinR

Expert Member
Joined
Aug 24, 2006
Messages
3,753
PHP:
// ** Creates an image from a png file. If you want to use gif or jpg images, just use the coresponding functions: imagecreatefromjpeg and imagecreatefromgif.
$image=imagecreatefrompng("images/baseimage.png");

// ** Output image to browser
header('Content-type: image/jpeg');
imagejpeg($image);

// ** Destroys the image
imagedestroy($image);

You'll need html like this:

Code:
<img src='mydynamicimagescript.php'>

But most email apps prevent image downloading for this very reason, is there not a better approach?
 

SilverNodashi

Expert Member
Joined
Oct 12, 2007
Messages
3,337
Awesome! This is exactly what I needed :)

I want to email a lot of users from a database, but want to track who gets the email. I know that using an image isn't 100% accurate, but I want to track who sees, or clicks on the provided link to download the graphic - i.e. get a conversion rate.
 

rorz0r

Executive Member
Joined
Feb 10, 2006
Messages
7,968
Basically you are kinda doing it wrong.
When you call the php script from an img tag it must return only the content type header and the actual image. What your php script is currently doing is returning an html page that calls the image, so you're now trying to call and image from your email and then that "image" is actually a page which calls another image so it's not working and you're getting a broken image.

Basically take out everything above <?php in your script and include the code carudden posted as your actual script. You can then add to the script and update databases and such for the tracking.
 

sn3rd

Expert Member
Joined
Jan 18, 2008
Messages
4,305
To do what you want, the PHP script you use as the img src should provide a header (as carudden showed) and then dump out the binary data of the image.

The PHP script you provided won't work because it doesn't send a header. You also need to send the header as the FIRST part; i.e. you can't have your HTML tags first.

The reason it works when you access it in the browser is because you've embedded the image there, not showing the image directly. You can't embed an HTML document within an image.

Alternative to carudden's example above (dumpimg.php):
Code:
<?php
//assume data is stored in the variable $imgdata
header('Content-type: image/jpeg');
echo $imgdata;
?>
Then you can embed it in HTML using
Code:
<img src="dumpimg.php" />
 

SilverNodashi

Expert Member
Joined
Oct 12, 2007
Messages
3,337
Basically you are kinda doing it wrong.
When you call the php script from an img tag it must return only the content type header and the actual image. What your php script is currently doing is returning an html page that calls the image, so you're now trying to call and image from your email and then that "image" is actually a page which calls another image so it's not working and you're getting a broken image.

Basically take out everything above <?php in your script and include the code carudden posted as your actual script. You can then add to the script and update databases and such for the tracking.

Thank you, that's what carudden said in his post as well :) And it's working fine now.
 
Top