Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving css images with PHP from outside public folder

Tags:

css

php

image

I have a css file that links to images.

.test{
  background: url(http://site.com/dynamic/test.jpg) no-repeat;
}

These images however are dynamic. They change by the minute so I have to generate them and serve them with php. This means I don't save them to the public folder. I just serve them directly from the php script.

The problem is when I visit the php script (http://site.com/dynamic/test.jpg), it generates the image correctly and I can see it in the browser, but when the same url is used in the css file as I need, the css can't seem to access the images.

I think it's because the image isn't saved in the public folder and is delivered by php which means that image has to be directly called to be generated, and calling the css file doesn't actually hit the php script (image url).

Does anyone have ideas to solve this problem?

like image 395
bunk Avatar asked Nov 26 '22 12:11

bunk


1 Answers

Try to return Content-type using php header function

header("Content-type: application/force-download"); 
header('Content-Type: application/octet-stream'); 
header('Content-Type: image/jpeg'); // or png...
like image 195
ngduc Avatar answered Nov 29 '22 02:11

ngduc