mirror of
https://github.com/mosbth/cimage.git
synced 2025-08-06 16:16:39 +02:00
update docs/api with phpdoc
This commit is contained in:
@@ -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,14 @@ 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)");
|
||||
public function setHeaderFields()
|
||||
{
|
||||
$this->http->setHeader("User-Agent", "CImage/0.7.2 (PHP/". phpversion() . " cURL)");
|
||||
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
||||
|
||||
if ($this->useCache) {
|
||||
@@ -202,37 +165,31 @@ class CRemoteImage
|
||||
*
|
||||
* @return string as path to saved file or false if not saved.
|
||||
*/
|
||||
function save() {
|
||||
|
||||
public function save()
|
||||
{
|
||||
$this->cache = array();
|
||||
$date = $this->http->getDate(time());
|
||||
$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) {
|
||||
// Save only if body is a valid image
|
||||
$body = $this->http->getBody();
|
||||
$img = imagecreatefromstring($body);
|
||||
|
||||
$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->fileJson, json_encode($this->cache));
|
||||
return $this->fileImage;
|
||||
}
|
||||
if ($img !== false) {
|
||||
file_put_contents($this->fileName, $body);
|
||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -245,21 +202,21 @@ class CRemoteImage
|
||||
*
|
||||
* @return string as path to cached file.
|
||||
*/
|
||||
function updateCacheDetails() {
|
||||
|
||||
public function updateCacheDetails()
|
||||
{
|
||||
$date = $this->http->getDate(time());
|
||||
$maxAge = $this->http->getMaxAge($this->defaultMaxAge);
|
||||
$lastModified = $this->http->getLastModified();
|
||||
|
||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||
$this->cache['Max-Age'] = $maxAge;
|
||||
$this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date);
|
||||
$this->cache['Max-Age'] = $maxAge;
|
||||
|
||||
if ($lastModified) {
|
||||
$this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified);
|
||||
}
|
||||
|
||||
file_put_contents($this->fileJson, json_encode($this->cache));
|
||||
return $this->fileImage;
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
|
||||
@@ -269,10 +226,12 @@ class CRemoteImage
|
||||
*
|
||||
* @param string $url a remote url.
|
||||
*
|
||||
* @throws Exception when status code does not match 200 or 304.
|
||||
*
|
||||
* @return string as path to downloaded file or false if failed.
|
||||
*/
|
||||
function download($url) {
|
||||
|
||||
public function download($url)
|
||||
{
|
||||
$this->http = new CHttpGet();
|
||||
$this->url = $url;
|
||||
|
||||
@@ -296,12 +255,12 @@ class CRemoteImage
|
||||
if ($this->status === 200) {
|
||||
$this->isCacheWritable();
|
||||
return $this->save();
|
||||
} else if ($this->status === 304) {
|
||||
} elseif ($this->status === 304) {
|
||||
$this->isCacheWritable();
|
||||
return $this->updateCacheDetails();
|
||||
}
|
||||
|
||||
return false;
|
||||
throw new Exception("Unknown statuscode when downloading remote image: " . $this->status);
|
||||
}
|
||||
|
||||
|
||||
@@ -313,7 +272,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 +289,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();
|
||||
$now = time();
|
||||
|
||||
if ($imageExists && $date + $maxAge > $now) {
|
||||
return $this->fileImage;
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
// Prepare for a 304 if available
|
||||
|
Reference in New Issue
Block a user