mirror of
https://github.com/mosbth/cimage.git
synced 2025-08-05 07:37:37 +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);
|
||||
|
||||
$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));
|
||||
|
||||
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;
|
||||
|
||||
@@ -61,13 +61,6 @@ class CRemoteImage
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Filename for image-file.
|
||||
*/
|
||||
private $fileImage;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Cache details loaded from file.
|
||||
*/
|
||||
@@ -75,16 +68,6 @@ class CRemoteImage
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
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");
|
||||
|
||||
if ($this->useCache) {
|
||||
@@ -209,30 +171,24 @@ class CRemoteImage
|
||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||
$lastModified = $this->http->getLastModified();
|
||||
$type = $this->http->getContentType();
|
||||
$extension = $this->contentTypeToFileExtension($type);
|
||||
|
||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||
$this->cache['Max-Age'] = $maxAge;
|
||||
$this->cache['Content-Type'] = $type;
|
||||
$this->cache['File-Extension'] = $extension;
|
||||
$this->cache['Url'] = $this->url;
|
||||
|
||||
if ($lastModified) {
|
||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||
}
|
||||
|
||||
if ($extension) {
|
||||
|
||||
$this->fileImage = $this->fileName . "." . $extension;
|
||||
|
||||
// Save only if body is a valid image
|
||||
$body = $this->http->getBody();
|
||||
$img = imagecreatefromstring($body);
|
||||
|
||||
if ($img !== false) {
|
||||
file_put_contents($this->fileImage, $body);
|
||||
file_put_contents($this->fileName, $body);
|
||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||
return $this->fileImage;
|
||||
}
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -259,7 +215,7 @@ class CRemoteImage
|
||||
}
|
||||
|
||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||
return $this->fileImage;
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
|
||||
@@ -313,7 +269,7 @@ class CRemoteImage
|
||||
*/
|
||||
public function loadCacheDetails()
|
||||
{
|
||||
$cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url);
|
||||
$cacheFile = md5($this->url);
|
||||
$this->fileName = $this->saveFolder . $cacheFile;
|
||||
$this->fileJson = $this->fileName . ".json";
|
||||
if (is_readable($this->fileJson)) {
|
||||
@@ -330,15 +286,15 @@ class CRemoteImage
|
||||
*/
|
||||
public function getCachedSource()
|
||||
{
|
||||
$this->fileImage = $this->fileName . "." . $this->cache['File-Extension'];
|
||||
$imageExists = is_readable($this->fileImage);
|
||||
$imageExists = is_readable($this->fileName);
|
||||
|
||||
// Is cache valid?
|
||||
$date = strtotime($this->cache['Date']);
|
||||
$maxAge = $this->cache['Max-Age'];
|
||||
$now = time();
|
||||
|
||||
if ($imageExists && $date + $maxAge > $now) {
|
||||
return $this->fileImage;
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
// Prepare for a 304 if available
|
||||
|
@@ -5,6 +5,7 @@ Revision history
|
||||
v0.7.0.x (latest)
|
||||
-------------------------------------
|
||||
|
||||
* Naming cache-file using md5 for remote images, fix #86.
|
||||
* Loading images without depending on filename extension, fix #85.
|
||||
* Adding unittest with phpunit #84, fix #13
|
||||
* 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;
|
||||
|
||||
@@ -340,13 +340,6 @@ class CRemoteImage
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Filename for image-file.
|
||||
*/
|
||||
private $fileImage;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Cache details loaded from file.
|
||||
*/
|
||||
@@ -354,16 +347,6 @@ class CRemoteImage
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
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");
|
||||
|
||||
if ($this->useCache) {
|
||||
@@ -488,30 +450,24 @@ class CRemoteImage
|
||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||
$lastModified = $this->http->getLastModified();
|
||||
$type = $this->http->getContentType();
|
||||
$extension = $this->contentTypeToFileExtension($type);
|
||||
|
||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||
$this->cache['Max-Age'] = $maxAge;
|
||||
$this->cache['Content-Type'] = $type;
|
||||
$this->cache['File-Extension'] = $extension;
|
||||
$this->cache['Url'] = $this->url;
|
||||
|
||||
if ($lastModified) {
|
||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||
}
|
||||
|
||||
if ($extension) {
|
||||
|
||||
$this->fileImage = $this->fileName . "." . $extension;
|
||||
|
||||
// Save only if body is a valid image
|
||||
$body = $this->http->getBody();
|
||||
$img = imagecreatefromstring($body);
|
||||
|
||||
if ($img !== false) {
|
||||
file_put_contents($this->fileImage, $body);
|
||||
file_put_contents($this->fileName, $body);
|
||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||
return $this->fileImage;
|
||||
}
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -538,7 +494,7 @@ class CRemoteImage
|
||||
}
|
||||
|
||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||
return $this->fileImage;
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
|
||||
@@ -592,7 +548,7 @@ class CRemoteImage
|
||||
*/
|
||||
public function loadCacheDetails()
|
||||
{
|
||||
$cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url);
|
||||
$cacheFile = md5($this->url);
|
||||
$this->fileName = $this->saveFolder . $cacheFile;
|
||||
$this->fileJson = $this->fileName . ".json";
|
||||
if (is_readable($this->fileJson)) {
|
||||
@@ -609,15 +565,15 @@ class CRemoteImage
|
||||
*/
|
||||
public function getCachedSource()
|
||||
{
|
||||
$this->fileImage = $this->fileName . "." . $this->cache['File-Extension'];
|
||||
$imageExists = is_readable($this->fileImage);
|
||||
$imageExists = is_readable($this->fileName);
|
||||
|
||||
// Is cache valid?
|
||||
$date = strtotime($this->cache['Date']);
|
||||
$maxAge = $this->cache['Max-Age'];
|
||||
$now = time();
|
||||
|
||||
if ($imageExists && $date + $maxAge > $now) {
|
||||
return $this->fileImage;
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
// Prepare for a 304 if available
|
||||
@@ -1169,7 +1125,7 @@ class CImage
|
||||
$src = $remote->download($src);
|
||||
|
||||
$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));
|
||||
|
||||
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;
|
||||
|
||||
@@ -340,13 +340,6 @@ class CRemoteImage
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Filename for image-file.
|
||||
*/
|
||||
private $fileImage;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Cache details loaded from file.
|
||||
*/
|
||||
@@ -354,16 +347,6 @@ class CRemoteImage
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
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");
|
||||
|
||||
if ($this->useCache) {
|
||||
@@ -488,30 +450,24 @@ class CRemoteImage
|
||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||
$lastModified = $this->http->getLastModified();
|
||||
$type = $this->http->getContentType();
|
||||
$extension = $this->contentTypeToFileExtension($type);
|
||||
|
||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||
$this->cache['Max-Age'] = $maxAge;
|
||||
$this->cache['Content-Type'] = $type;
|
||||
$this->cache['File-Extension'] = $extension;
|
||||
$this->cache['Url'] = $this->url;
|
||||
|
||||
if ($lastModified) {
|
||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||
}
|
||||
|
||||
if ($extension) {
|
||||
|
||||
$this->fileImage = $this->fileName . "." . $extension;
|
||||
|
||||
// Save only if body is a valid image
|
||||
$body = $this->http->getBody();
|
||||
$img = imagecreatefromstring($body);
|
||||
|
||||
if ($img !== false) {
|
||||
file_put_contents($this->fileImage, $body);
|
||||
file_put_contents($this->fileName, $body);
|
||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||
return $this->fileImage;
|
||||
}
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -538,7 +494,7 @@ class CRemoteImage
|
||||
}
|
||||
|
||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||
return $this->fileImage;
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
|
||||
@@ -592,7 +548,7 @@ class CRemoteImage
|
||||
*/
|
||||
public function loadCacheDetails()
|
||||
{
|
||||
$cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url);
|
||||
$cacheFile = md5($this->url);
|
||||
$this->fileName = $this->saveFolder . $cacheFile;
|
||||
$this->fileJson = $this->fileName . ".json";
|
||||
if (is_readable($this->fileJson)) {
|
||||
@@ -609,15 +565,15 @@ class CRemoteImage
|
||||
*/
|
||||
public function getCachedSource()
|
||||
{
|
||||
$this->fileImage = $this->fileName . "." . $this->cache['File-Extension'];
|
||||
$imageExists = is_readable($this->fileImage);
|
||||
$imageExists = is_readable($this->fileName);
|
||||
|
||||
// Is cache valid?
|
||||
$date = strtotime($this->cache['Date']);
|
||||
$maxAge = $this->cache['Max-Age'];
|
||||
$now = time();
|
||||
|
||||
if ($imageExists && $date + $maxAge > $now) {
|
||||
return $this->fileImage;
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
// Prepare for a 304 if available
|
||||
@@ -1169,7 +1125,7 @@ class CImage
|
||||
$src = $remote->download($src);
|
||||
|
||||
$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));
|
||||
|
||||
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;
|
||||
|
||||
@@ -340,13 +340,6 @@ class CRemoteImage
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Filename for image-file.
|
||||
*/
|
||||
private $fileImage;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Cache details loaded from file.
|
||||
*/
|
||||
@@ -354,16 +347,6 @@ class CRemoteImage
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
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");
|
||||
|
||||
if ($this->useCache) {
|
||||
@@ -488,30 +450,24 @@ class CRemoteImage
|
||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||
$lastModified = $this->http->getLastModified();
|
||||
$type = $this->http->getContentType();
|
||||
$extension = $this->contentTypeToFileExtension($type);
|
||||
|
||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||
$this->cache['Max-Age'] = $maxAge;
|
||||
$this->cache['Content-Type'] = $type;
|
||||
$this->cache['File-Extension'] = $extension;
|
||||
$this->cache['Url'] = $this->url;
|
||||
|
||||
if ($lastModified) {
|
||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||
}
|
||||
|
||||
if ($extension) {
|
||||
|
||||
$this->fileImage = $this->fileName . "." . $extension;
|
||||
|
||||
// Save only if body is a valid image
|
||||
$body = $this->http->getBody();
|
||||
$img = imagecreatefromstring($body);
|
||||
|
||||
if ($img !== false) {
|
||||
file_put_contents($this->fileImage, $body);
|
||||
file_put_contents($this->fileName, $body);
|
||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||
return $this->fileImage;
|
||||
}
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -538,7 +494,7 @@ class CRemoteImage
|
||||
}
|
||||
|
||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||
return $this->fileImage;
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
|
||||
@@ -592,7 +548,7 @@ class CRemoteImage
|
||||
*/
|
||||
public function loadCacheDetails()
|
||||
{
|
||||
$cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url);
|
||||
$cacheFile = md5($this->url);
|
||||
$this->fileName = $this->saveFolder . $cacheFile;
|
||||
$this->fileJson = $this->fileName . ".json";
|
||||
if (is_readable($this->fileJson)) {
|
||||
@@ -609,15 +565,15 @@ class CRemoteImage
|
||||
*/
|
||||
public function getCachedSource()
|
||||
{
|
||||
$this->fileImage = $this->fileName . "." . $this->cache['File-Extension'];
|
||||
$imageExists = is_readable($this->fileImage);
|
||||
$imageExists = is_readable($this->fileName);
|
||||
|
||||
// Is cache valid?
|
||||
$date = strtotime($this->cache['Date']);
|
||||
$maxAge = $this->cache['Max-Age'];
|
||||
$now = time();
|
||||
|
||||
if ($imageExists && $date + $maxAge > $now) {
|
||||
return $this->fileImage;
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
// Prepare for a 304 if available
|
||||
@@ -1169,7 +1125,7 @@ class CImage
|
||||
$src = $remote->download($src);
|
||||
|
||||
$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));
|
||||
|
||||
return $src;
|
||||
|
Reference in New Issue
Block a user