tags:

views:

89

answers:

2

I want to fetch images from other site and display on my site. I want to store that images in my side temporary only at time of showing on page , so that user think images are coming from my site.is it possible? How? Thanks in advance.

A: 

When the request for an image is made against your server, simply use fopen() to open the remote image, and fpassthru() to pass it through.

Ignacio Vazquez-Abrams
A: 

A simple caching system would do this. Given an HTML file like:

<body>
 <img src="imagesource.php?file=1" />
</body>

You could create your image data source (imagesource.php) like:

<?php
$files = array(
   'http://somesite.com/image_to_cache.gif',
   'http://somesite.com/image_to_cache2.gif'
);

if(!isset($_REQUEST['file']) || !isset($files[$_REQUEST['file']])) {
   header('HTTP/1.0 404 Not Found');
   exit;
}

// do we already have the cached file?
$requested_file = $files[$_REQUEST['file']];
$cache_dir = '/tmp/cached_images';
$cache_file = md5($requested_file);
$cache_path = $cache_dir . '/' . $cache_file;

header('Content-Type: image/gif');

if(file_exists($cache_path)) {
   readfile($cache_path);
   exit;
}

//need to create the cache.
$data = file_get_contents($requested_file);
file_put_contents($cache_path, $data);

echo $data;

This would cache the remote images locally if they haven't already been downloaded, and output them to the browser when requested.

Tyson