You are here:  » Slightly Different Take on Image Caching


Slightly Different Take on Image Caching

Submitted by Keeop on Tue, 2016-03-15 10:05 in

Hi David,

Looking for a solution to a little thing I'm working on which is a bit of a twist on the image caching function.

Basically, I need to access the physical path to an image which needs to be downloaded locally based on the image in the product feed. But, there's no rendering of the image as such, just a download and value based on the physical location it's downloaded to. I thought the easiest way would be to mess about with the imagecache.php code but of course this is designed to work by parsing the the product feed image through the script via an 'IMG SRC' URL value. What I need is more of a function, I guess, where I can pass the product feed image to it and it then spits out the physical location it's downloaded the file to - if successful of course. Guessing it would be easier for a whole new bit of code?

Cheers.
Keeop

Submitted by support on Tue, 2016-03-15 14:19

Hello Keeop,

A basic fetch and return file system path is straight forward - have a go with something like

function fetch_image($image_url)
{
  $dir = "/home/username/images/";
  $filename = $dir.md5($image_url).".img";
  if (!file_exists($filename))
  {
    @copy($image_url,$filename);
  }
  return (file_exists($filename)?$filename:FALSE);
}

- the directory specified in $dir must be writeable by PHP (e.g. mode 777)

- the @ prefix in-front of the copy() function will suppress any error messages if the transfer fails

- function will return absolute file system path to local image file e.g.

/home/username/images/{md5 hash of image_url}.img

(or FALSE if transfer failed)

The use of copy() requires that allow URL fopen() is enabled however if you need to use CURL instead, in place of:

    @copy($image_url,$filename);

Use:

  $fp = fopen($filename,"w");
  $ch = curl_init($image_url);
  if (!ini_get('open_basedir')) curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
  curl_setopt($ch,CURLOPT_HEADER,0);
  curl_setopt($ch,CURLOPT_FILE,$fp);
  $success = curl_exec($ch);
  fclose($fp);
  if (!$success) unlink($filename);

You might want to consider using CURL anyway, since PHP does not follow HTTP 302 (Moved) redirection headers, whereas CURL will as long as an open_basedir restriction is not in effect.

Hope this points you in the right direction!

Cheers,
David.
--
PriceTapestry.com

Submitted by Keeop on Tue, 2016-03-15 15:12

Hi David,

Thanks for the quick response as always!

However, I can't get either method to work. The 'fopen' one returns no value and the 'curl' one returns a load of non standard characters on screen! I have deffo got fopen enabled. Doesn't seem to be pulling the files down to the directory which is strange as it's the same folder as I use to do the standard image caching so all the permissions should be fine.

Cheers.
Keeop

Submitted by Keeop on Tue, 2016-03-15 15:15

Ah, seems to be the path. This is Windows, so is a little different. I have tried "D:\\wwwroot\sitename\imagecache\" - doesn't work. Also tried "D:/wwwroot/sitename/imagecache/" - doesn't work. Just tried a relative in there at "../imagecache/" and it is fine but the relative will be different depending on where it's called from. Think I need to look up that sort of stuff but then it looks like it works a treat!

Thanks.
Keeop

Submitted by support on Tue, 2016-03-15 15:18

Hi Keeop,

Go to /admin/ > Support Info of the installation that you are testing this on and you should see the "Install Path" displayed - that should indicate exactly what to use in-front of imagecache/ on your Windows server...

Cheers,
David.
--
PriceTapestry.com