1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-01-16 18:48:14 +01:00

adding CCache to improve cache handling of dummy and srgb

This commit is contained in:
Mikael Roos 2015-12-06 19:53:34 +01:00
parent 29743b75ec
commit 376a40e538
9 changed files with 265 additions and 37 deletions

3
.gitignore vendored
View File

@ -11,3 +11,6 @@ vendor
# Build and test
build/
# Mac OS
.DS_Store

107
CCache.php Normal file
View 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;
}
}

View File

@ -5,6 +5,12 @@ Revision history
[![Build Status](https://scrutinizer-ci.com/g/mosbth/cimage/badges/build.png?b=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)
-------------------------------------

View File

View File

@ -27,6 +27,7 @@
"CRemoteImage.php",
"CWhitelist.php",
"CAsciiArt.php"
"CCache.php"
]
}
}

68
test/CCacheTest.php Normal file
View 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.");
}
}

View File

@ -5,6 +5,57 @@
*/
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
*
@ -14,15 +65,15 @@ class CImageDummyTest extends \PHPUnit_Framework_TestCase
{
$img = new CImage();
$img->setSaveFolder(CACHE_PATH . "/dummy");
$img->setSource('dummy', CACHE_PATH . "/dummy");
$img->setSaveFolder($this->cachepath);
$img->setSource(self::DUMMY, $this->cachepath);
$img->createDummyImage();
$img->generateFilename(null, false);
$img->save(null, null, false);
$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->setSaveFolder(CACHE_PATH . "/dummy");
$img->setSource('dummy', CACHE_PATH . "/dummy");
$img->setSaveFolder($this->cachepath);
$img->setSource(self::DUMMY, $this->cachepath);
$img->createDummyImage(200, 400);
$img->generateFilename(null, false);
$img->save(null, null, false);
$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.");
}
}

View File

@ -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.
*/
$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);
@ -961,11 +963,8 @@ if ($cacheControl) {
/**
* Prepare a dummy image and use it as source image.
*/
$dummyDir = getConfig('dummy_dir', $cachePath. "/" . $dummyFilename);
if ($dummyImage === true) {
is_writable($dummyDir)
or verbose("dummy dir not writable = $dummyDir");
$dummyDir = $cache->getPathToSubdir("dummy");
$img->setSaveFolder($dummyDir)
->setSource($dummyFilename, $dummyDir)
@ -993,24 +992,16 @@ if ($dummyImage === true) {
/**
* 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);
$srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc');
$srgb = getDefined('srgb', true, null);
if ($srgb || $srgbDefault) {
if (!is_writable($srgbDir)) {
if (is_writable($cachePath)) {
mkdir($srgbDir);
}
}
$filename = $img->convert2sRGBColorSpace(
$srcImage,
$imagePath,
$srgbDir,
$cache->getPathToSubdir("srgb"),
$srgbColorProfile,
$useCache
);
@ -1034,9 +1025,16 @@ if ($status) {
$text .= "PHP version = " . PHP_VERSION . "\n";
$text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n";
$text .= "Allow remote images = $allowRemote\n";
$text .= "Cache writable = " . is_writable($cachePath) . "\n";
$text .= "Cache dummy writable = " . is_writable($dummyDir) . "\n";
$text .= "Cache srgb writable = " . is_writable($srgbDir) . "\n";
$res = $cache->getStatusOfSubdir("");
$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";
$no = extension_loaded('exif') ? null : 'NOT';

View File

@ -14,7 +14,7 @@ return array(
* mode: 'production'
*/
//'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
* 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.
* This option requires PHP extension imagick and will silently fail
* if that is not installed.
*
* Default value:
* srgb_dir: $cachePath/srgb
* srgb_default: false
* srgb_colorprofile: __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc'
*/
//'srgb_dir' => __DIR__ . '/../cache/srgb/',
//'srgb_default' => false,
//'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
* and stored at the defined path. The dummy image can then be used
* inplace of an original image as a placeholder.
* The dummy_dir must be writable and it defaults to a subdir of the
* cache directory.
* Write protect the dummy_dir to prevent creation of new dummy images,
* but continue to use the existing ones.
* and stored as a image in the dir CACHE_PATH/dummy. The dummy image
* can then be used as a placeholder image.
* The dir CACHE_PATH/dummy is automatically created when needed.
* Write protect the CACHE_PATH/dummy to prevent creation of new
* dummy images, but continue to use the existing ones.
*
* Default value:
* dummy_enabled: true as default, disable dummy feature by setting
* to false.
* 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_filename' => 'dummy',
//'dummy_dir' => 'some writable directory',