Jump to content

Masking the source of images?


virtuexru

Recommended Posts

is there a particular reason you cannot store these images locally ?

 

We have a data feed from another website that we pay for. Images are a part of this feed (they feed us their updates links into a MySQL database). I don't want people seeing where the images are being fed from though, that is the problem.

Hi

 

Never tried it from a remote site, but read the image from the other location into php and then write out from php complete with headers.

 

header("Content-Type: image/gif");
readfile("http://www.domain2.com");

 

Might work (think it depends on a setting in the php ini file to allow remote reads like this, maybe allow_url_fopen ).

 

All the best

 

Keith

If a server's going to remotely request it more than once, it might as well just save it.  You're wasting bandwidth otherwise.

 

I would agree that is best practice, and to cache it locally. But I presume he doesn't want a link to a real local copy either.

 

All the best

 

Keith

If a server's going to remotely request it more than once, it might as well just save it.  You're wasting bandwidth otherwise.

 

I would agree that is best practice, and to cache it locally. But I presume he doesn't want a link to a real local copy either.

 

All the best

 

Keith

 

How would I go about saving a local copy automatically?

Hi

 

Something like this (not tested as hacked from a script I have, so may be a typo or 2):-

 

$CacheFileName = '/SomeCacheLocation/SomeImage.jpg';
$RemoteFileAddress = 'http://www.remotesite.com/SomeImage.jpg';
$RemoteFileName = 'SomeImage.jpg';
if (file_exists( $CacheFileName ))
{
header('Content-Type: image/jpg');
readfile($CacheFileName );
} 
else 
{
$img = imagecreatefromstring(file_get_contents( $RemoteFileAddress ));
if ($img !== false) 
{
	$thumb = imagecreatetruecolor($width, $height);
	if ($thumb !== false) 
	{
		imagecopyresampled($thumb, $img, 0, 0, 0, 0, $width, $height);
		imagejpeg($thumb, $CacheFileName, 100);
		header("Content-disposition: attachment; filename=\"$RemoteFileName\"");
		header('Content-Type: image/jpg');
		readfile($CacheFileName );
		imagedestroy($img);
		imagedestroy($thumb);
	}
}
}

 

Basically, check if the image is cached. If so just output that. If not then create an image in memory from the remote location, create another local image and specify the width and height you want (so you can create a smaller / larger copy), copy the first image to the resized local copy, write it out as a jpeg, then put out the headers and do a readfile to read the cached image and output it.

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.