mirror of
https://github.com/mosbth/cimage.git
synced 2025-08-04 07:07:32 +02:00
Naming cache-file using md5 for remote images, fix #86
This commit is contained in:
@@ -537,7 +537,7 @@ class CImage
|
|||||||
$src = $remote->download($src);
|
$src = $remote->download($src);
|
||||||
|
|
||||||
$this->log("Remote HTTP status: " . $remote->getStatus());
|
$this->log("Remote HTTP status: " . $remote->getStatus());
|
||||||
$this->log("Remote item has local cached file: $src");
|
$this->log("Remote item is in local cache: $src");
|
||||||
$this->log("Remote details on cache:" . print_r($remote->getDetails(), true));
|
$this->log("Remote details on cache:" . print_r($remote->getDetails(), true));
|
||||||
|
|
||||||
return $src;
|
return $src;
|
||||||
|
@@ -48,7 +48,7 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base name of cache file for downloaded item.
|
* Base name of cache file for downloaded item and name of image.
|
||||||
*/
|
*/
|
||||||
private $fileName;
|
private $fileName;
|
||||||
|
|
||||||
@@ -61,13 +61,6 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filename for image-file.
|
|
||||||
*/
|
|
||||||
private $fileImage;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache details loaded from file.
|
* Cache details loaded from file.
|
||||||
*/
|
*/
|
||||||
@@ -75,16 +68,6 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get status of last HTTP request.
|
* Get status of last HTTP request.
|
||||||
*
|
*
|
||||||
@@ -157,34 +140,13 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Translate a content type to a file extension.
|
|
||||||
*
|
|
||||||
* @param string $type a valid content type.
|
|
||||||
*
|
|
||||||
* @return string as file extension or false if no match.
|
|
||||||
*/
|
|
||||||
function contentTypeToFileExtension($type) {
|
|
||||||
$extension = array(
|
|
||||||
'image/jpeg' => 'jpg',
|
|
||||||
'image/png' => 'png',
|
|
||||||
'image/gif' => 'gif',
|
|
||||||
);
|
|
||||||
|
|
||||||
return isset($extension[$type])
|
|
||||||
? $extension[$type]
|
|
||||||
: false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set header fields.
|
* Set header fields.
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
function setHeaderFields() {
|
function setHeaderFields() {
|
||||||
$this->http->setHeader("User-Agent", "CImage/0.6 (PHP/". phpversion() . " cURL)");
|
$this->http->setHeader("User-Agent", "CImage/0.7.0 (PHP/". phpversion() . " cURL)");
|
||||||
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
||||||
|
|
||||||
if ($this->useCache) {
|
if ($this->useCache) {
|
||||||
@@ -209,30 +171,24 @@ class CRemoteImage
|
|||||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||||
$lastModified = $this->http->getLastModified();
|
$lastModified = $this->http->getLastModified();
|
||||||
$type = $this->http->getContentType();
|
$type = $this->http->getContentType();
|
||||||
$extension = $this->contentTypeToFileExtension($type);
|
|
||||||
|
|
||||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||||
$this->cache['Max-Age'] = $maxAge;
|
$this->cache['Max-Age'] = $maxAge;
|
||||||
$this->cache['Content-Type'] = $type;
|
$this->cache['Content-Type'] = $type;
|
||||||
$this->cache['File-Extension'] = $extension;
|
$this->cache['Url'] = $this->url;
|
||||||
|
|
||||||
if ($lastModified) {
|
if ($lastModified) {
|
||||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($extension) {
|
// Save only if body is a valid image
|
||||||
|
$body = $this->http->getBody();
|
||||||
|
$img = imagecreatefromstring($body);
|
||||||
|
|
||||||
$this->fileImage = $this->fileName . "." . $extension;
|
if ($img !== false) {
|
||||||
|
file_put_contents($this->fileName, $body);
|
||||||
// Save only if body is a valid image
|
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||||
$body = $this->http->getBody();
|
return $this->fileName;
|
||||||
$img = imagecreatefromstring($body);
|
|
||||||
|
|
||||||
if ($img !== false) {
|
|
||||||
file_put_contents($this->fileImage, $body);
|
|
||||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
|
||||||
return $this->fileImage;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -251,15 +207,15 @@ class CRemoteImage
|
|||||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||||
$lastModified = $this->http->getLastModified();
|
$lastModified = $this->http->getLastModified();
|
||||||
|
|
||||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||||
$this->cache['Max-Age'] = $maxAge;
|
$this->cache['Max-Age'] = $maxAge;
|
||||||
|
|
||||||
if ($lastModified) {
|
if ($lastModified) {
|
||||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||||
return $this->fileImage;
|
return $this->fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -313,7 +269,7 @@ class CRemoteImage
|
|||||||
*/
|
*/
|
||||||
public function loadCacheDetails()
|
public function loadCacheDetails()
|
||||||
{
|
{
|
||||||
$cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url);
|
$cacheFile = md5($this->url);
|
||||||
$this->fileName = $this->saveFolder . $cacheFile;
|
$this->fileName = $this->saveFolder . $cacheFile;
|
||||||
$this->fileJson = $this->fileName . ".json";
|
$this->fileJson = $this->fileName . ".json";
|
||||||
if (is_readable($this->fileJson)) {
|
if (is_readable($this->fileJson)) {
|
||||||
@@ -330,15 +286,15 @@ class CRemoteImage
|
|||||||
*/
|
*/
|
||||||
public function getCachedSource()
|
public function getCachedSource()
|
||||||
{
|
{
|
||||||
$this->fileImage = $this->fileName . "." . $this->cache['File-Extension'];
|
$imageExists = is_readable($this->fileName);
|
||||||
$imageExists = is_readable($this->fileImage);
|
|
||||||
|
|
||||||
// Is cache valid?
|
// Is cache valid?
|
||||||
$date = strtotime($this->cache['Date']);
|
$date = strtotime($this->cache['Date']);
|
||||||
$maxAge = $this->cache['Max-Age'];
|
$maxAge = $this->cache['Max-Age'];
|
||||||
$now = time();
|
$now = time();
|
||||||
|
|
||||||
if ($imageExists && $date + $maxAge > $now) {
|
if ($imageExists && $date + $maxAge > $now) {
|
||||||
return $this->fileImage;
|
return $this->fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare for a 304 if available
|
// Prepare for a 304 if available
|
||||||
|
@@ -5,6 +5,7 @@ Revision history
|
|||||||
v0.7.0.x (latest)
|
v0.7.0.x (latest)
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
|
|
||||||
|
* Naming cache-file using md5 for remote images, fix #86.
|
||||||
* Loading images without depending on filename extension, fix #85.
|
* Loading images without depending on filename extension, fix #85.
|
||||||
* Adding unittest with phpunit #84, fix #13
|
* Adding unittest with phpunit #84, fix #13
|
||||||
* Adding support for whitelist of remote hostnames, #84
|
* Adding support for whitelist of remote hostnames, #84
|
||||||
|
@@ -327,7 +327,7 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base name of cache file for downloaded item.
|
* Base name of cache file for downloaded item and name of image.
|
||||||
*/
|
*/
|
||||||
private $fileName;
|
private $fileName;
|
||||||
|
|
||||||
@@ -340,13 +340,6 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filename for image-file.
|
|
||||||
*/
|
|
||||||
private $fileImage;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache details loaded from file.
|
* Cache details loaded from file.
|
||||||
*/
|
*/
|
||||||
@@ -354,16 +347,6 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get status of last HTTP request.
|
* Get status of last HTTP request.
|
||||||
*
|
*
|
||||||
@@ -436,34 +419,13 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Translate a content type to a file extension.
|
|
||||||
*
|
|
||||||
* @param string $type a valid content type.
|
|
||||||
*
|
|
||||||
* @return string as file extension or false if no match.
|
|
||||||
*/
|
|
||||||
function contentTypeToFileExtension($type) {
|
|
||||||
$extension = array(
|
|
||||||
'image/jpeg' => 'jpg',
|
|
||||||
'image/png' => 'png',
|
|
||||||
'image/gif' => 'gif',
|
|
||||||
);
|
|
||||||
|
|
||||||
return isset($extension[$type])
|
|
||||||
? $extension[$type]
|
|
||||||
: false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set header fields.
|
* Set header fields.
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
function setHeaderFields() {
|
function setHeaderFields() {
|
||||||
$this->http->setHeader("User-Agent", "CImage/0.6 (PHP/". phpversion() . " cURL)");
|
$this->http->setHeader("User-Agent", "CImage/0.7.0 (PHP/". phpversion() . " cURL)");
|
||||||
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
||||||
|
|
||||||
if ($this->useCache) {
|
if ($this->useCache) {
|
||||||
@@ -488,30 +450,24 @@ class CRemoteImage
|
|||||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||||
$lastModified = $this->http->getLastModified();
|
$lastModified = $this->http->getLastModified();
|
||||||
$type = $this->http->getContentType();
|
$type = $this->http->getContentType();
|
||||||
$extension = $this->contentTypeToFileExtension($type);
|
|
||||||
|
|
||||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||||
$this->cache['Max-Age'] = $maxAge;
|
$this->cache['Max-Age'] = $maxAge;
|
||||||
$this->cache['Content-Type'] = $type;
|
$this->cache['Content-Type'] = $type;
|
||||||
$this->cache['File-Extension'] = $extension;
|
$this->cache['Url'] = $this->url;
|
||||||
|
|
||||||
if ($lastModified) {
|
if ($lastModified) {
|
||||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($extension) {
|
// Save only if body is a valid image
|
||||||
|
$body = $this->http->getBody();
|
||||||
|
$img = imagecreatefromstring($body);
|
||||||
|
|
||||||
$this->fileImage = $this->fileName . "." . $extension;
|
if ($img !== false) {
|
||||||
|
file_put_contents($this->fileName, $body);
|
||||||
// Save only if body is a valid image
|
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||||
$body = $this->http->getBody();
|
return $this->fileName;
|
||||||
$img = imagecreatefromstring($body);
|
|
||||||
|
|
||||||
if ($img !== false) {
|
|
||||||
file_put_contents($this->fileImage, $body);
|
|
||||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
|
||||||
return $this->fileImage;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -530,15 +486,15 @@ class CRemoteImage
|
|||||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||||
$lastModified = $this->http->getLastModified();
|
$lastModified = $this->http->getLastModified();
|
||||||
|
|
||||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||||
$this->cache['Max-Age'] = $maxAge;
|
$this->cache['Max-Age'] = $maxAge;
|
||||||
|
|
||||||
if ($lastModified) {
|
if ($lastModified) {
|
||||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||||
return $this->fileImage;
|
return $this->fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -592,7 +548,7 @@ class CRemoteImage
|
|||||||
*/
|
*/
|
||||||
public function loadCacheDetails()
|
public function loadCacheDetails()
|
||||||
{
|
{
|
||||||
$cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url);
|
$cacheFile = md5($this->url);
|
||||||
$this->fileName = $this->saveFolder . $cacheFile;
|
$this->fileName = $this->saveFolder . $cacheFile;
|
||||||
$this->fileJson = $this->fileName . ".json";
|
$this->fileJson = $this->fileName . ".json";
|
||||||
if (is_readable($this->fileJson)) {
|
if (is_readable($this->fileJson)) {
|
||||||
@@ -609,15 +565,15 @@ class CRemoteImage
|
|||||||
*/
|
*/
|
||||||
public function getCachedSource()
|
public function getCachedSource()
|
||||||
{
|
{
|
||||||
$this->fileImage = $this->fileName . "." . $this->cache['File-Extension'];
|
$imageExists = is_readable($this->fileName);
|
||||||
$imageExists = is_readable($this->fileImage);
|
|
||||||
|
|
||||||
// Is cache valid?
|
// Is cache valid?
|
||||||
$date = strtotime($this->cache['Date']);
|
$date = strtotime($this->cache['Date']);
|
||||||
$maxAge = $this->cache['Max-Age'];
|
$maxAge = $this->cache['Max-Age'];
|
||||||
$now = time();
|
$now = time();
|
||||||
|
|
||||||
if ($imageExists && $date + $maxAge > $now) {
|
if ($imageExists && $date + $maxAge > $now) {
|
||||||
return $this->fileImage;
|
return $this->fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare for a 304 if available
|
// Prepare for a 304 if available
|
||||||
@@ -1169,7 +1125,7 @@ class CImage
|
|||||||
$src = $remote->download($src);
|
$src = $remote->download($src);
|
||||||
|
|
||||||
$this->log("Remote HTTP status: " . $remote->getStatus());
|
$this->log("Remote HTTP status: " . $remote->getStatus());
|
||||||
$this->log("Remote item has local cached file: $src");
|
$this->log("Remote item is in local cache: $src");
|
||||||
$this->log("Remote details on cache:" . print_r($remote->getDetails(), true));
|
$this->log("Remote details on cache:" . print_r($remote->getDetails(), true));
|
||||||
|
|
||||||
return $src;
|
return $src;
|
||||||
|
@@ -327,7 +327,7 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base name of cache file for downloaded item.
|
* Base name of cache file for downloaded item and name of image.
|
||||||
*/
|
*/
|
||||||
private $fileName;
|
private $fileName;
|
||||||
|
|
||||||
@@ -340,13 +340,6 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filename for image-file.
|
|
||||||
*/
|
|
||||||
private $fileImage;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache details loaded from file.
|
* Cache details loaded from file.
|
||||||
*/
|
*/
|
||||||
@@ -354,16 +347,6 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get status of last HTTP request.
|
* Get status of last HTTP request.
|
||||||
*
|
*
|
||||||
@@ -436,34 +419,13 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Translate a content type to a file extension.
|
|
||||||
*
|
|
||||||
* @param string $type a valid content type.
|
|
||||||
*
|
|
||||||
* @return string as file extension or false if no match.
|
|
||||||
*/
|
|
||||||
function contentTypeToFileExtension($type) {
|
|
||||||
$extension = array(
|
|
||||||
'image/jpeg' => 'jpg',
|
|
||||||
'image/png' => 'png',
|
|
||||||
'image/gif' => 'gif',
|
|
||||||
);
|
|
||||||
|
|
||||||
return isset($extension[$type])
|
|
||||||
? $extension[$type]
|
|
||||||
: false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set header fields.
|
* Set header fields.
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
function setHeaderFields() {
|
function setHeaderFields() {
|
||||||
$this->http->setHeader("User-Agent", "CImage/0.6 (PHP/". phpversion() . " cURL)");
|
$this->http->setHeader("User-Agent", "CImage/0.7.0 (PHP/". phpversion() . " cURL)");
|
||||||
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
||||||
|
|
||||||
if ($this->useCache) {
|
if ($this->useCache) {
|
||||||
@@ -488,30 +450,24 @@ class CRemoteImage
|
|||||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||||
$lastModified = $this->http->getLastModified();
|
$lastModified = $this->http->getLastModified();
|
||||||
$type = $this->http->getContentType();
|
$type = $this->http->getContentType();
|
||||||
$extension = $this->contentTypeToFileExtension($type);
|
|
||||||
|
|
||||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||||
$this->cache['Max-Age'] = $maxAge;
|
$this->cache['Max-Age'] = $maxAge;
|
||||||
$this->cache['Content-Type'] = $type;
|
$this->cache['Content-Type'] = $type;
|
||||||
$this->cache['File-Extension'] = $extension;
|
$this->cache['Url'] = $this->url;
|
||||||
|
|
||||||
if ($lastModified) {
|
if ($lastModified) {
|
||||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($extension) {
|
// Save only if body is a valid image
|
||||||
|
$body = $this->http->getBody();
|
||||||
|
$img = imagecreatefromstring($body);
|
||||||
|
|
||||||
$this->fileImage = $this->fileName . "." . $extension;
|
if ($img !== false) {
|
||||||
|
file_put_contents($this->fileName, $body);
|
||||||
// Save only if body is a valid image
|
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||||
$body = $this->http->getBody();
|
return $this->fileName;
|
||||||
$img = imagecreatefromstring($body);
|
|
||||||
|
|
||||||
if ($img !== false) {
|
|
||||||
file_put_contents($this->fileImage, $body);
|
|
||||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
|
||||||
return $this->fileImage;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -530,15 +486,15 @@ class CRemoteImage
|
|||||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||||
$lastModified = $this->http->getLastModified();
|
$lastModified = $this->http->getLastModified();
|
||||||
|
|
||||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||||
$this->cache['Max-Age'] = $maxAge;
|
$this->cache['Max-Age'] = $maxAge;
|
||||||
|
|
||||||
if ($lastModified) {
|
if ($lastModified) {
|
||||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||||
return $this->fileImage;
|
return $this->fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -592,7 +548,7 @@ class CRemoteImage
|
|||||||
*/
|
*/
|
||||||
public function loadCacheDetails()
|
public function loadCacheDetails()
|
||||||
{
|
{
|
||||||
$cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url);
|
$cacheFile = md5($this->url);
|
||||||
$this->fileName = $this->saveFolder . $cacheFile;
|
$this->fileName = $this->saveFolder . $cacheFile;
|
||||||
$this->fileJson = $this->fileName . ".json";
|
$this->fileJson = $this->fileName . ".json";
|
||||||
if (is_readable($this->fileJson)) {
|
if (is_readable($this->fileJson)) {
|
||||||
@@ -609,15 +565,15 @@ class CRemoteImage
|
|||||||
*/
|
*/
|
||||||
public function getCachedSource()
|
public function getCachedSource()
|
||||||
{
|
{
|
||||||
$this->fileImage = $this->fileName . "." . $this->cache['File-Extension'];
|
$imageExists = is_readable($this->fileName);
|
||||||
$imageExists = is_readable($this->fileImage);
|
|
||||||
|
|
||||||
// Is cache valid?
|
// Is cache valid?
|
||||||
$date = strtotime($this->cache['Date']);
|
$date = strtotime($this->cache['Date']);
|
||||||
$maxAge = $this->cache['Max-Age'];
|
$maxAge = $this->cache['Max-Age'];
|
||||||
$now = time();
|
$now = time();
|
||||||
|
|
||||||
if ($imageExists && $date + $maxAge > $now) {
|
if ($imageExists && $date + $maxAge > $now) {
|
||||||
return $this->fileImage;
|
return $this->fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare for a 304 if available
|
// Prepare for a 304 if available
|
||||||
@@ -1169,7 +1125,7 @@ class CImage
|
|||||||
$src = $remote->download($src);
|
$src = $remote->download($src);
|
||||||
|
|
||||||
$this->log("Remote HTTP status: " . $remote->getStatus());
|
$this->log("Remote HTTP status: " . $remote->getStatus());
|
||||||
$this->log("Remote item has local cached file: $src");
|
$this->log("Remote item is in local cache: $src");
|
||||||
$this->log("Remote details on cache:" . print_r($remote->getDetails(), true));
|
$this->log("Remote details on cache:" . print_r($remote->getDetails(), true));
|
||||||
|
|
||||||
return $src;
|
return $src;
|
||||||
|
@@ -327,7 +327,7 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base name of cache file for downloaded item.
|
* Base name of cache file for downloaded item and name of image.
|
||||||
*/
|
*/
|
||||||
private $fileName;
|
private $fileName;
|
||||||
|
|
||||||
@@ -340,13 +340,6 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filename for image-file.
|
|
||||||
*/
|
|
||||||
private $fileImage;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache details loaded from file.
|
* Cache details loaded from file.
|
||||||
*/
|
*/
|
||||||
@@ -354,16 +347,6 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get status of last HTTP request.
|
* Get status of last HTTP request.
|
||||||
*
|
*
|
||||||
@@ -436,34 +419,13 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Translate a content type to a file extension.
|
|
||||||
*
|
|
||||||
* @param string $type a valid content type.
|
|
||||||
*
|
|
||||||
* @return string as file extension or false if no match.
|
|
||||||
*/
|
|
||||||
function contentTypeToFileExtension($type) {
|
|
||||||
$extension = array(
|
|
||||||
'image/jpeg' => 'jpg',
|
|
||||||
'image/png' => 'png',
|
|
||||||
'image/gif' => 'gif',
|
|
||||||
);
|
|
||||||
|
|
||||||
return isset($extension[$type])
|
|
||||||
? $extension[$type]
|
|
||||||
: false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set header fields.
|
* Set header fields.
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
function setHeaderFields() {
|
function setHeaderFields() {
|
||||||
$this->http->setHeader("User-Agent", "CImage/0.6 (PHP/". phpversion() . " cURL)");
|
$this->http->setHeader("User-Agent", "CImage/0.7.0 (PHP/". phpversion() . " cURL)");
|
||||||
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
||||||
|
|
||||||
if ($this->useCache) {
|
if ($this->useCache) {
|
||||||
@@ -488,30 +450,24 @@ class CRemoteImage
|
|||||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||||
$lastModified = $this->http->getLastModified();
|
$lastModified = $this->http->getLastModified();
|
||||||
$type = $this->http->getContentType();
|
$type = $this->http->getContentType();
|
||||||
$extension = $this->contentTypeToFileExtension($type);
|
|
||||||
|
|
||||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||||
$this->cache['Max-Age'] = $maxAge;
|
$this->cache['Max-Age'] = $maxAge;
|
||||||
$this->cache['Content-Type'] = $type;
|
$this->cache['Content-Type'] = $type;
|
||||||
$this->cache['File-Extension'] = $extension;
|
$this->cache['Url'] = $this->url;
|
||||||
|
|
||||||
if ($lastModified) {
|
if ($lastModified) {
|
||||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($extension) {
|
// Save only if body is a valid image
|
||||||
|
$body = $this->http->getBody();
|
||||||
|
$img = imagecreatefromstring($body);
|
||||||
|
|
||||||
$this->fileImage = $this->fileName . "." . $extension;
|
if ($img !== false) {
|
||||||
|
file_put_contents($this->fileName, $body);
|
||||||
// Save only if body is a valid image
|
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||||
$body = $this->http->getBody();
|
return $this->fileName;
|
||||||
$img = imagecreatefromstring($body);
|
|
||||||
|
|
||||||
if ($img !== false) {
|
|
||||||
file_put_contents($this->fileImage, $body);
|
|
||||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
|
||||||
return $this->fileImage;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -530,15 +486,15 @@ class CRemoteImage
|
|||||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||||
$lastModified = $this->http->getLastModified();
|
$lastModified = $this->http->getLastModified();
|
||||||
|
|
||||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||||
$this->cache['Max-Age'] = $maxAge;
|
$this->cache['Max-Age'] = $maxAge;
|
||||||
|
|
||||||
if ($lastModified) {
|
if ($lastModified) {
|
||||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||||
return $this->fileImage;
|
return $this->fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -592,7 +548,7 @@ class CRemoteImage
|
|||||||
*/
|
*/
|
||||||
public function loadCacheDetails()
|
public function loadCacheDetails()
|
||||||
{
|
{
|
||||||
$cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url);
|
$cacheFile = md5($this->url);
|
||||||
$this->fileName = $this->saveFolder . $cacheFile;
|
$this->fileName = $this->saveFolder . $cacheFile;
|
||||||
$this->fileJson = $this->fileName . ".json";
|
$this->fileJson = $this->fileName . ".json";
|
||||||
if (is_readable($this->fileJson)) {
|
if (is_readable($this->fileJson)) {
|
||||||
@@ -609,15 +565,15 @@ class CRemoteImage
|
|||||||
*/
|
*/
|
||||||
public function getCachedSource()
|
public function getCachedSource()
|
||||||
{
|
{
|
||||||
$this->fileImage = $this->fileName . "." . $this->cache['File-Extension'];
|
$imageExists = is_readable($this->fileName);
|
||||||
$imageExists = is_readable($this->fileImage);
|
|
||||||
|
|
||||||
// Is cache valid?
|
// Is cache valid?
|
||||||
$date = strtotime($this->cache['Date']);
|
$date = strtotime($this->cache['Date']);
|
||||||
$maxAge = $this->cache['Max-Age'];
|
$maxAge = $this->cache['Max-Age'];
|
||||||
$now = time();
|
$now = time();
|
||||||
|
|
||||||
if ($imageExists && $date + $maxAge > $now) {
|
if ($imageExists && $date + $maxAge > $now) {
|
||||||
return $this->fileImage;
|
return $this->fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare for a 304 if available
|
// Prepare for a 304 if available
|
||||||
@@ -1169,7 +1125,7 @@ class CImage
|
|||||||
$src = $remote->download($src);
|
$src = $remote->download($src);
|
||||||
|
|
||||||
$this->log("Remote HTTP status: " . $remote->getStatus());
|
$this->log("Remote HTTP status: " . $remote->getStatus());
|
||||||
$this->log("Remote item has local cached file: $src");
|
$this->log("Remote item is in local cache: $src");
|
||||||
$this->log("Remote details on cache:" . print_r($remote->getDetails(), true));
|
$this->log("Remote details on cache:" . print_r($remote->getDetails(), true));
|
||||||
|
|
||||||
return $src;
|
return $src;
|
||||||
|
Reference in New Issue
Block a user