Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to cache image generated by php

Tags:

php

caching

image

i made a file to print out the image file with W and H i define by get method

but my problem is to cache this pictures

i add this headers to the file

@header("Cache-Control: private, max-age=10800, pre-check=10800");
@header("Pragma: private");
@header("Expires: " . date(DATE_RFC822,filemtime($full_path)));

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
       &&
  (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($full_path))) {
  // send the last mod time of the file back
  header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($full_path)).' GMT',true, 304);
  exit;
}else
{
     @header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($full_path)) . ' GMT');
     @header('Content-Type: image/jpeg');
     @imagejpeg($image);
}

but my problem is some pictures are cached ok , but others are not , and sometime pictures inside album not appear until if i disable the cache header

is my headers are correct ? , and about the cache do i have to use - or + to set the time cache how its work ?

like image 334
Noob Avatar asked Dec 29 '11 17:12

Noob


People also ask

How do you cache an image?

Image caching essentially means downloading an image to the local storage in the app's cache directory (or any other directory that is accessible to the app) and loading it from local storage next time the image loads.

Can PHP files be cached?

The Windows Cache Extension for PHP includes a file cache that is used to store the content of the PHP script files in shared memory, which reduces the amount of file system operations performed by PHP engine. Resolve File Path Cache - PHP scripts very often include or operate with files by using relative file paths.

How can I get cache in PHP?

PHP | CachingIterator getCache() Function The CachingIterator::getCache() function is an inbuilt function in PHP which is used to retrieve the contents of the cache. Parameters: This function does not accept any parameters. Return Value: This function returns an array containing the cache items.

Are images stored in cache?

Cached data are files, scripts, images, and other multimedia stored on your device after opening an app or visiting a website for the first time. This data is then used to quickly gather information about the app or website every time revisited, reducing load time.


1 Answers

because they use Htaccess with mod_expires?

Example #1:

# enable expirations
ExpiresActive On
# expire GIF images after a month in the client's cache
ExpiresByType image/gif A2592000
# HTML documents are good for a week from the
# time they were changed
ExpiresByType text/html M604800

Read This Documentation

Example #2:

ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
like image 190
Jorge Olaf Avatar answered Oct 03 '22 16:10

Jorge Olaf