user1968070
user1968070

Reputation:

Replace images by serving through PHP script

I'm trying to serve all my images through a PHP script. Ultimately the goal is to replace the images dynamically if certain conditions are met, but for now I'm just trying to get the script to return a specific image file. I've got a .htaccess which is sending all image requests to this script but at the moment the image is not showing up on the page.

Full contents of PHP script:

$img = '/imgs/img1.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($img));
readfile($img);

I want to get URL of the image request that was redirected to this script. Depending on conditions I want to either serve the image immediately or use the image to generate a new one with GD and return that.

Upvotes: 0

Views: 192

Answers (1)

Ares
Ares

Reputation: 5903

You are giving a path to the location in the hard drive not the docroot. Therefore, unless the file in located in your hard drive in /imgs/ it wont work. Try:

$img = $_SERVER['DOCUMENT_ROOT'] . '/imgs/img1.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($img));
readfile($img);

Upvotes: 1

Related Questions