mirror of
https://github.com/mosbth/cimage.git
synced 2025-07-31 21:40:12 +02:00
adding CCache to improve cache handling of dummy and srgb
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -11,3 +11,6 @@ vendor
|
|||||||
|
|
||||||
# Build and test
|
# Build and test
|
||||||
build/
|
build/
|
||||||
|
|
||||||
|
# Mac OS
|
||||||
|
.DS_Store
|
||||||
|
107
CCache.php
Normal file
107
CCache.php
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Deal with the cache directory and cached items.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class CCache
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Path to the cache directory.
|
||||||
|
*/
|
||||||
|
private $path;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the path to the cache dir which must exist.
|
||||||
|
*
|
||||||
|
* @param string path to the cache dir.
|
||||||
|
*
|
||||||
|
* @throws Exception when $path is not a directory.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setDir($path)
|
||||||
|
{
|
||||||
|
if (!is_dir($path)) {
|
||||||
|
throw new Exception("Cachedir is not a directory.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->path = $path;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the path to the cache subdir and try to create it if its not there.
|
||||||
|
*
|
||||||
|
* @param string $subdir name of subdir
|
||||||
|
* @param array $create default is to try to create the subdir
|
||||||
|
*
|
||||||
|
* @return string | boolean as real path to the subdir or
|
||||||
|
* false if it does not exists
|
||||||
|
*/
|
||||||
|
public function getPathToSubdir($subdir, $create = true)
|
||||||
|
{
|
||||||
|
$path = realpath($this->path . "/" . $subdir);
|
||||||
|
|
||||||
|
if (is_dir($path)) {
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($create && is_writable($this->path)) {
|
||||||
|
$path = $this->path . "/" . $subdir;
|
||||||
|
|
||||||
|
if (mkdir($path)) {
|
||||||
|
return realpath($path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get status of the cache subdir.
|
||||||
|
*
|
||||||
|
* @param string $subdir name of subdir
|
||||||
|
*
|
||||||
|
* @return string with status
|
||||||
|
*/
|
||||||
|
public function getStatusOfSubdir($subdir)
|
||||||
|
{
|
||||||
|
$path = realpath($this->path . "/" . $subdir);
|
||||||
|
|
||||||
|
$exists = is_dir($path);
|
||||||
|
$res = $exists ? "exists" : "does not exist";
|
||||||
|
|
||||||
|
if ($exists) {
|
||||||
|
$res .= is_writable($path) ? ", writable" : ", not writable";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the cache subdir.
|
||||||
|
*
|
||||||
|
* @param string $subdir name of subdir
|
||||||
|
*
|
||||||
|
* @return null | boolean true if success else false, null if no operation
|
||||||
|
*/
|
||||||
|
public function removeSubdir($subdir)
|
||||||
|
{
|
||||||
|
$path = realpath($this->path . "/" . $subdir);
|
||||||
|
|
||||||
|
if (is_dir($path)) {
|
||||||
|
return rmdir($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@@ -5,6 +5,12 @@ Revision history
|
|||||||
[](https://scrutinizer-ci.com/g/mosbth/cimage/build-status/master)
|
[](https://scrutinizer-ci.com/g/mosbth/cimage/build-status/master)
|
||||||
|
|
||||||
|
|
||||||
|
v0.7.8* (2015-12-06)
|
||||||
|
-------------------------------------
|
||||||
|
|
||||||
|
* Adding CCache to improve cache handling of dummy and srgb, #130.
|
||||||
|
|
||||||
|
|
||||||
v0.7.8 (2015-12-06)
|
v0.7.8 (2015-12-06)
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
|
|
||||||
|
0
cache/dummy/README.md
vendored
0
cache/dummy/README.md
vendored
@@ -27,6 +27,7 @@
|
|||||||
"CRemoteImage.php",
|
"CRemoteImage.php",
|
||||||
"CWhitelist.php",
|
"CWhitelist.php",
|
||||||
"CAsciiArt.php"
|
"CAsciiArt.php"
|
||||||
|
"CCache.php"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
68
test/CCacheTest.php
Normal file
68
test/CCacheTest.php
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* A testclass
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class CCacheTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Test
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSetCacheDir()
|
||||||
|
{
|
||||||
|
$cache = new CCache();
|
||||||
|
$cache->setDir(CACHE_PATH);
|
||||||
|
|
||||||
|
$exp = "exists, writable";
|
||||||
|
$res = $cache->getStatusOfSubdir("");
|
||||||
|
$this->assertEquals($exp, $res, "Status of cache dir missmatch.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test
|
||||||
|
*
|
||||||
|
* @expectedException Exception
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSetWrongCacheDir()
|
||||||
|
{
|
||||||
|
$cache = new CCache();
|
||||||
|
$cache->setDir(CACHE_PATH . "/NO_EXISTS");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testCreateSubdir()
|
||||||
|
{
|
||||||
|
$cache = new CCache();
|
||||||
|
$cache->setDir(CACHE_PATH);
|
||||||
|
|
||||||
|
$subdir = "__test__";
|
||||||
|
$cache->removeSubdir($subdir);
|
||||||
|
|
||||||
|
$exp = "does not exist";
|
||||||
|
$res = $cache->getStatusOfSubdir($subdir, false);
|
||||||
|
$this->assertEquals($exp, $res, "Subdir should not be created.");
|
||||||
|
|
||||||
|
$res = $cache->getPathToSubdir($subdir);
|
||||||
|
$exp = realpath(CACHE_PATH . "/$subdir");
|
||||||
|
$this->assertEquals($exp, $res, "Subdir path missmatch.");
|
||||||
|
|
||||||
|
$exp = "exists, writable";
|
||||||
|
$res = $cache->getStatusOfSubdir($subdir);
|
||||||
|
$this->assertEquals($exp, $res, "Subdir should exist.");
|
||||||
|
|
||||||
|
$res = $cache->removeSubdir($subdir);
|
||||||
|
$this->assertTrue($res, "Remove subdir.");
|
||||||
|
}
|
||||||
|
}
|
@@ -5,6 +5,57 @@
|
|||||||
*/
|
*/
|
||||||
class CImageDummyTest extends \PHPUnit_Framework_TestCase
|
class CImageDummyTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
const DUMMY = "__dummy__";
|
||||||
|
private $cachepath;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup environment
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$cache = new CCache();
|
||||||
|
$cache->setDir(CACHE_PATH);
|
||||||
|
$this->cachepath = $cache->getPathToSubdir(self::DUMMY);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up cache dir content.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function removeFilesInCacheDir()
|
||||||
|
{
|
||||||
|
$files = glob($this->cachepath . "/*");
|
||||||
|
foreach ($files as $file) {
|
||||||
|
if (is_file($file)) {
|
||||||
|
unlink($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Teardown environment
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function tearDown()
|
||||||
|
{
|
||||||
|
$cache = new CCache();
|
||||||
|
$cache->setDir(CACHE_PATH);
|
||||||
|
$this->removeFilesInCacheDir();
|
||||||
|
$cache->removeSubdir(self::DUMMY);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test
|
* Test
|
||||||
*
|
*
|
||||||
@@ -14,15 +65,15 @@ class CImageDummyTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$img = new CImage();
|
$img = new CImage();
|
||||||
|
|
||||||
$img->setSaveFolder(CACHE_PATH . "/dummy");
|
$img->setSaveFolder($this->cachepath);
|
||||||
$img->setSource('dummy', CACHE_PATH . "/dummy");
|
$img->setSource(self::DUMMY, $this->cachepath);
|
||||||
$img->createDummyImage();
|
$img->createDummyImage();
|
||||||
$img->generateFilename(null, false);
|
$img->generateFilename(null, false);
|
||||||
$img->save(null, null, false);
|
$img->save(null, null, false);
|
||||||
|
|
||||||
$filename = $img->getTarget();
|
$filename = $img->getTarget();
|
||||||
|
|
||||||
$this->assertEquals(basename($filename), "dummy_100_100", "Filename not as expected on dummy image.");
|
$this->assertEquals(basename($filename), self::DUMMY . "_100_100", "Filename not as expected on dummy image.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -36,14 +87,14 @@ class CImageDummyTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$img = new CImage();
|
$img = new CImage();
|
||||||
|
|
||||||
$img->setSaveFolder(CACHE_PATH . "/dummy");
|
$img->setSaveFolder($this->cachepath);
|
||||||
$img->setSource('dummy', CACHE_PATH . "/dummy");
|
$img->setSource(self::DUMMY, $this->cachepath);
|
||||||
$img->createDummyImage(200, 400);
|
$img->createDummyImage(200, 400);
|
||||||
$img->generateFilename(null, false);
|
$img->generateFilename(null, false);
|
||||||
$img->save(null, null, false);
|
$img->save(null, null, false);
|
||||||
|
|
||||||
$filename = $img->getTarget();
|
$filename = $img->getTarget();
|
||||||
|
|
||||||
$this->assertEquals(basename($filename), "dummy_200_400", "Filename not as expected on dummy image.");
|
$this->assertEquals(basename($filename), self::DUMMY . "_200_400", "Filename not as expected on dummy image.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -8,7 +8,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$version = "v0.7.8 (2015-12-06)";
|
$version = "v0.7.8* (2015-12-06)";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -943,11 +943,13 @@ verbose("alias = $alias");
|
|||||||
* Get the cachepath from config.
|
* Get the cachepath from config.
|
||||||
*/
|
*/
|
||||||
$cachePath = getConfig('cache_path', __DIR__ . '/../cache/');
|
$cachePath = getConfig('cache_path', __DIR__ . '/../cache/');
|
||||||
|
$cache = new CCache();
|
||||||
|
$cache->setDir($cachePath);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the cachepath from config.
|
* Add cache control HTTP header.
|
||||||
*/
|
*/
|
||||||
$cacheControl = getConfig('cache_control', null);
|
$cacheControl = getConfig('cache_control', null);
|
||||||
|
|
||||||
@@ -961,11 +963,8 @@ if ($cacheControl) {
|
|||||||
/**
|
/**
|
||||||
* Prepare a dummy image and use it as source image.
|
* Prepare a dummy image and use it as source image.
|
||||||
*/
|
*/
|
||||||
$dummyDir = getConfig('dummy_dir', $cachePath. "/" . $dummyFilename);
|
|
||||||
|
|
||||||
if ($dummyImage === true) {
|
if ($dummyImage === true) {
|
||||||
is_writable($dummyDir)
|
$dummyDir = $cache->getPathToSubdir("dummy");
|
||||||
or verbose("dummy dir not writable = $dummyDir");
|
|
||||||
|
|
||||||
$img->setSaveFolder($dummyDir)
|
$img->setSaveFolder($dummyDir)
|
||||||
->setSource($dummyFilename, $dummyDir)
|
->setSource($dummyFilename, $dummyDir)
|
||||||
@@ -993,24 +992,16 @@ if ($dummyImage === true) {
|
|||||||
/**
|
/**
|
||||||
* Prepare a sRGB version of the image and use it as source image.
|
* Prepare a sRGB version of the image and use it as source image.
|
||||||
*/
|
*/
|
||||||
$srgbDirName = "/srgb";
|
|
||||||
$srgbDir = realpath(getConfig('srgb_dir', $cachePath . $srgbDirName));
|
|
||||||
$srgbDefault = getConfig('srgb_default', false);
|
$srgbDefault = getConfig('srgb_default', false);
|
||||||
$srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc');
|
$srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc');
|
||||||
$srgb = getDefined('srgb', true, null);
|
$srgb = getDefined('srgb', true, null);
|
||||||
|
|
||||||
if ($srgb || $srgbDefault) {
|
if ($srgb || $srgbDefault) {
|
||||||
|
|
||||||
if (!is_writable($srgbDir)) {
|
|
||||||
if (is_writable($cachePath)) {
|
|
||||||
mkdir($srgbDir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$filename = $img->convert2sRGBColorSpace(
|
$filename = $img->convert2sRGBColorSpace(
|
||||||
$srcImage,
|
$srcImage,
|
||||||
$imagePath,
|
$imagePath,
|
||||||
$srgbDir,
|
$cache->getPathToSubdir("srgb"),
|
||||||
$srgbColorProfile,
|
$srgbColorProfile,
|
||||||
$useCache
|
$useCache
|
||||||
);
|
);
|
||||||
@@ -1034,9 +1025,16 @@ if ($status) {
|
|||||||
$text .= "PHP version = " . PHP_VERSION . "\n";
|
$text .= "PHP version = " . PHP_VERSION . "\n";
|
||||||
$text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n";
|
$text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n";
|
||||||
$text .= "Allow remote images = $allowRemote\n";
|
$text .= "Allow remote images = $allowRemote\n";
|
||||||
$text .= "Cache writable = " . is_writable($cachePath) . "\n";
|
|
||||||
$text .= "Cache dummy writable = " . is_writable($dummyDir) . "\n";
|
$res = $cache->getStatusOfSubdir("");
|
||||||
$text .= "Cache srgb writable = " . is_writable($srgbDir) . "\n";
|
$text .= "Cache $res\n";
|
||||||
|
|
||||||
|
$res = $cache->getStatusOfSubdir("dummy");
|
||||||
|
$text .= "Cache dummy $res\n";
|
||||||
|
|
||||||
|
$res = $cache->getStatusOfSubdir("srgb");
|
||||||
|
$text .= "Cache srgb $res\n";
|
||||||
|
|
||||||
$text .= "Alias path writable = " . is_writable($aliasPath) . "\n";
|
$text .= "Alias path writable = " . is_writable($aliasPath) . "\n";
|
||||||
|
|
||||||
$no = extension_loaded('exif') ? null : 'NOT';
|
$no = extension_loaded('exif') ? null : 'NOT';
|
||||||
|
@@ -14,7 +14,7 @@ return array(
|
|||||||
* mode: 'production'
|
* mode: 'production'
|
||||||
*/
|
*/
|
||||||
//'mode' => 'production', // 'development', 'strict'
|
//'mode' => 'production', // 'development', 'strict'
|
||||||
//'mode' => 'development', // 'development', 'strict'
|
'mode' => 'development', // 'development', 'strict'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -124,17 +124,15 @@ return array(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert the image to srgb before processing. Saves the converted
|
* Convert the image to srgb before processing. Saves the converted
|
||||||
* image in the sub cache dir. This option is default false but can
|
* image in a cache subdir 'srgb'. This option is default false but can
|
||||||
* be changed to default true to do this conversion for all images.
|
* be changed to default true to do this conversion for all images.
|
||||||
* This option requires PHP extension imagick and will silently fail
|
* This option requires PHP extension imagick and will silently fail
|
||||||
* if that is not installed.
|
* if that is not installed.
|
||||||
*
|
*
|
||||||
* Default value:
|
* Default value:
|
||||||
* srgb_dir: $cachePath/srgb
|
|
||||||
* srgb_default: false
|
* srgb_default: false
|
||||||
* srgb_colorprofile: __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc'
|
* srgb_colorprofile: __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc'
|
||||||
*/
|
*/
|
||||||
//'srgb_dir' => __DIR__ . '/../cache/srgb/',
|
|
||||||
//'srgb_default' => false,
|
//'srgb_default' => false,
|
||||||
//'srgb_colorprofile' => __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc',
|
//'srgb_colorprofile' => __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc',
|
||||||
|
|
||||||
@@ -170,23 +168,19 @@ return array(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The name representing a dummy image which is automatically created
|
* The name representing a dummy image which is automatically created
|
||||||
* and stored at the defined path. The dummy image can then be used
|
* and stored as a image in the dir CACHE_PATH/dummy. The dummy image
|
||||||
* inplace of an original image as a placeholder.
|
* can then be used as a placeholder image.
|
||||||
* The dummy_dir must be writable and it defaults to a subdir of the
|
* The dir CACHE_PATH/dummy is automatically created when needed.
|
||||||
* cache directory.
|
* Write protect the CACHE_PATH/dummy to prevent creation of new
|
||||||
* Write protect the dummy_dir to prevent creation of new dummy images,
|
* dummy images, but continue to use the existing ones.
|
||||||
* but continue to use the existing ones.
|
|
||||||
*
|
*
|
||||||
* Default value:
|
* Default value:
|
||||||
* dummy_enabled: true as default, disable dummy feature by setting
|
* dummy_enabled: true as default, disable dummy feature by setting
|
||||||
* to false.
|
* to false.
|
||||||
* dummy_filename: 'dummy' use this as ?src=dummy to create a dummy image.
|
* dummy_filename: 'dummy' use this as ?src=dummy to create a dummy image.
|
||||||
* dummy_dir: Defaults to subdirectory of 'cache_path',
|
|
||||||
* named the same as 'dummy_filename'
|
|
||||||
*/
|
*/
|
||||||
//'dummy_enabled' => true,
|
//'dummy_enabled' => true,
|
||||||
//'dummy_filename' => 'dummy',
|
//'dummy_filename' => 'dummy',
|
||||||
//'dummy_dir' => 'some writable directory',
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user