? I am storing banner ads in mysql table, or should ...">

How do I script a php file to display an image like <img src = "/ img.php? ImageID = 32" />?

I am storing banner ads in mysql table, or should I say just the filename, like "imagename.jpg" or "banner3.gif".

Obviously I want to track the loading of these banners, so I'm going to create a php file called img.php and call it like this

<img src="/img/img.php?imageID=3" />

      

And this will output the image with id 3 along with updating the hit table, etc.

I know how to update the hits table ... but what I'm trying to solve is the img.php file code so that it just fetches the filename and prints it to the screen so it works like a regular image.

As long as I have ...

<?php
header("Content-Type: image/jpeg");
// insert my db connect file
// update the hits table etc

$sql = 'SELECT * FROM ads WHERE id="' . $_GET['imageID'] .  '" ';
$res = mysql_query($sql);

$row = mysql_fetch_array($res);

$image = '/img/' . $row['file'];

imagejpeg($image);

// and this is where I'm at...

?>

      

I have some problems with the above code that I pulled from various places to get here. Images can be png / gif / jpg while the header content type seems to only have space for one type.

Is there a way for this one file to work fine for multiple types of images? I think I should first query the table, handle the file extension and then insert a header function based on that.

But what do I actually do when I have this right and I want the image to just appear?

thanks to your help here is the final working file

<?php

// make sure we're only getting a number for the query b4 doing stuff
if(is_numeric($_GET['imid'])) {

include('thedbfile.php');

$types['jpg'] = 'image/jpeg';
$types['png'] = 'image/png';
$types['gif'] = 'image/gif';



$sql = 'SELECT file FROM ads WHERE id="' . $_GET['imid'] . '" ';
$res = mysql_query($sql);

$row = mysql_fetch_array($res);

$image = 'img/banners/' . $row['file'];

$extension = end(explode('.', $row['file']));


header('Content-Type: ' . $types[$extension]);

echo file_get_contents($image);


}

?>

      

+2


source to share


5 answers


You may want to store the content of the image directly in your database.

As you said, you need to parse the content type from the filename. You can also add a field to your database that contains the extension.

$extension = end(explode('.', $row['file']));

      

Then you need to create an array that contains the content type of the header:

$types['jpg'] = 'image/jpeg';
$types['png'] = 'image/png';

      

then send a header (you want to check if the type array contains an extension key and an error if it doesn't work)



header('Content-Type: ' . $types[$extension]);

      

Then load the image with

echo file_get_contents($image);

      

This should do it. Note that you really need to check if the ImageID parameter is an integer for security reasons:

if(!ctype_digit($_GET['ImageID'])) // error

      

+2


source


echo file_get_contents($image);

      

But I doubt that your images are actually in the /img/

(top level of the filesystem hierarchy). And yes, tweak Content-type

after you know what type of your image sounds good.



PS you extended your code a bit and I have no idea what it does imagejpeg($image)

; -)

+2


source


you can try header('Location: <URL-TO-IMAGE>');

after you are done with the image.

you can detect the extension from the filename and then return the appropriate MIME type.

function mime2ext($mime){
    $m = explode('/',$mime);
    $mime = $m[count($m)-1];
    switch($mime){
        case 'jpg': return 'jpg'; break;
        case 'jpeg': return 'jpg'; break;
        case 'pjpeg': return 'jpg'; break;
        case 'png': return 'png'; break;
        case 'gif': return 'gif'; break;
    }
    return '';
}

function ext2mime($fname){
    $ext = end(explode('.',$fname));
    switch($ext){
        case 'jpg': return 'image/jpeg'; break;
        case 'jpeg': return 'image/jpeg'; break;
        case 'png': return 'image/png'; break;
        case 'gif': return 'image/gif'; break;
    }
    return '';
}

      

regarding plural mime type, there is no plural mime type. Only the last Content-Type will be sent.

cm

<?php
header('Content-Type: image/jpeg');
header('Content-Type: image/png');
header('Content-Type: image/gif');
?>

      

Content type is image / gif.

+2


source


Something like this will ensure that you are delivering the image correctly and make sure it is not cached so that a page reload will trigger a new fetch ...

//tell client abou tcontent type
header("Content-Type:image/jpeg");

//prevent caching
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
header ("Pragma: no-cache");                          // HTTP/1.0

//how big is it?
header("Content-Length:".filesize($image));

//send image data
readfile($image);

      

+2


source


In the end, you just need to rotate the image data in the body of your message along with the correct listener. My code looks like this:

header("Content-type: $mime");
echo $this->mBinaryJunk;

      

You need to set the $ mime type correctly based on the actual image type. You can do this by saving it when the original finds the file or finds it from the filename.

+1


source







All Articles