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

Introduced default values for almost all confguration options #72

This commit is contained in:
Mikael Roos
2015-01-14 20:47:48 +01:00
parent 8e9a3b68f0
commit 81e70b7acd
3 changed files with 111 additions and 38 deletions

View File

@@ -278,9 +278,16 @@ http://dbwebb.se/opensource/cimage
Revision history Revision history
------------------------------------- -------------------------------------
v0.6.x (latest)
* Cleaned up `img_config.php` and introduced default values for almost all options #72.
v0.6.2 (2015-01-14) v0.6.2 (2015-01-14)
* Added support for download of remote images #43. * Added support for download of remote images #43.
v0.6.1 (2015-01-08) v0.6.1 (2015-01-08)
* Adding compare-page for comparing images. Issue #20. * Adding compare-page for comparing images. Issue #20.

View File

@@ -126,8 +126,10 @@ call_user_func($config['error_reporting']);
/** /**
* Set default timezone if not set or if its set in the config-file. * Set default timezone if not set or if its set in the config-file.
*/ */
if (isset($config['default_timezone'])) { $defaultTimezone = getConfig('default_timezone', null);
date_default_timezone_set($config['default_timezone']);
if ($defaultTimezone) {
date_default_timezone_set($defaultTimezone);
} else if (!ini_get('default_timezone')) { } else if (!ini_get('default_timezone')) {
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
} }
@@ -193,16 +195,16 @@ if ($allowRemote && $passwordMatch !== false) {
* shortcut, sc - extend arguments with a constant value, defined * shortcut, sc - extend arguments with a constant value, defined
* in config-file. * in config-file.
*/ */
$shortcut = get(array('shortcut', 'sc'), null); $shortcut = get(array('shortcut', 'sc'), null);
$shortcutConfig = getConfig('shortcut', array());
verbose("shortcut = $shortcut"); verbose("shortcut = $shortcut");
if (isset($shortcut) if (isset($shortcut)
&& isset($config['shortcut']) && isset($shortcutConfig[$shortcut])) {
&& isset($config['shortcut'][$shortcut])) {
parse_str($config['shortcut'][$shortcut], $get); parse_str($shortcutConfig[$shortcut], $get);
verbose("shortcut-constant = {$config['shortcut'][$shortcut]}"); verbose("shortcut-constant = {$shortcutConfig[$shortcut]}");
$_GET = array_merge($_GET, $get); $_GET = array_merge($_GET, $get);
} }
@@ -216,19 +218,22 @@ $srcImage = get('src')
// Check for valid/invalid characters // Check for valid/invalid characters
preg_match($config['valid_filename'], $srcImage) $imagePath = getConfig('image_path', null);
or errorPage('Filename contains invalid characters.'); $imagePathConstraint = getConfig('image_path_constraint', true);
$validFilename = getConfig('valid_filename', '#^[a-z0-9A-Z-/_\.:]+$#');
preg_match($validFilename, $srcImage)
or errorPage('Filename contains invalid characters.');
if ($allowRemote && $img->isRemoteSource($srcImage)) { if ($allowRemote && $img->isRemoteSource($srcImage)) {
// If source is a remote file, ignore local file checks. // If source is a remote file, ignore local file checks.
} else if ($config['image_path_constraint']) { } else if ($imagePathConstraint) {
// Check that the image is a file below the directory 'image_path'. // Check that the image is a file below the directory 'image_path'.
$pathToImage = realpath($config['image_path'] . $srcImage); $pathToImage = realpath($imagePath . $srcImage);
$imageDir = realpath($config['image_path']); $imageDir = realpath($imagePath);
is_file($pathToImage) is_file($pathToImage)
or errorPage( or errorPage(
@@ -243,7 +248,6 @@ if ($allowRemote && $img->isRemoteSource($srcImage)) {
); );
} }
verbose("src = $srcImage"); verbose("src = $srcImage");
@@ -251,10 +255,12 @@ verbose("src = $srcImage");
/** /**
* width, w - set target width, affecting the resulting image width, height and resize options * width, w - set target width, affecting the resulting image width, height and resize options
*/ */
$newWidth = get(array('width', 'w')); $newWidth = get(array('width', 'w'));
$maxWidth = getConfig('max_width', 2000);
$sizeConstant = getConfig('size_constant', array());
// Check to replace predefined size // Check to replace predefined size
$sizes = call_user_func($config['size_constant']); $sizes = call_user_func($sizeConstant);
if (isset($sizes[$newWidth])) { if (isset($sizes[$newWidth])) {
$newWidth = $sizes[$newWidth]; $newWidth = $sizes[$newWidth];
} }
@@ -265,7 +271,7 @@ if ($newWidth[strlen($newWidth)-1] == '%') {
or errorPage('Width % not numeric.'); or errorPage('Width % not numeric.');
} else { } else {
is_null($newWidth) is_null($newWidth)
or ($newWidth > 10 && $newWidth <= $config['max_width']) or ($newWidth > 10 && $newWidth <= $maxWidth)
or errorPage('Width out of range.'); or errorPage('Width out of range.');
} }
@@ -277,6 +283,7 @@ verbose("new width = $newWidth");
* height, h - set target height, affecting the resulting image width, height and resize options * height, h - set target height, affecting the resulting image width, height and resize options
*/ */
$newHeight = get(array('height', 'h')); $newHeight = get(array('height', 'h'));
$maxHeight = getConfig('max_height', 2000);
// Check to replace predefined size // Check to replace predefined size
if (isset($sizes[$newHeight])) { if (isset($sizes[$newHeight])) {
@@ -289,7 +296,7 @@ if ($newHeight[strlen($newHeight)-1] == '%') {
or errorPage('Height % out of range.'); or errorPage('Height % out of range.');
} else { } else {
is_null($newHeight) is_null($newHeight)
or ($newHeight > 10 && $newHeight <= $config['max_height']) or ($newHeight > 10 && $newHeight <= $maxHeight)
or errorPage('Hight out of range.'); or errorPage('Hight out of range.');
} }
@@ -300,10 +307,11 @@ verbose("new height = $newHeight");
/** /**
* aspect-ratio, ar - affecting the resulting image width, height and resize options * aspect-ratio, ar - affecting the resulting image width, height and resize options
*/ */
$aspectRatio = get(array('aspect-ratio', 'ar')); $aspectRatio = get(array('aspect-ratio', 'ar'));
$aspectRatioConstant = getConfig('aspect_ratio_constant', array());
// Check to replace predefined aspect ratio // Check to replace predefined aspect ratio
$aspectRatios = call_user_func($config['aspect_ratio_constant']); $aspectRatios = call_user_func($aspectRatioConstant);
$negateAspectRatio = ($aspectRatio[0] == '!') ? true : false; $negateAspectRatio = ($aspectRatio[0] == '!') ? true : false;
$aspectRatio = $negateAspectRatio ? substr($aspectRatio, 1) : $aspectRatio; $aspectRatio = $negateAspectRatio ? substr($aspectRatio, 1) : $aspectRatio;
@@ -335,9 +343,11 @@ verbose("crop to fit = $cropToFit");
/** /**
* Set default background color from config file. * Set default background color from config file.
*/ */
if (isset($config['background_color'])) { $backgroundColor = getConfig('background_color', null);
$img->setDefaultBackgroundColor($config['background_color']);
verbose("Using default background_color = {$config['background_color']}"); if ($backgroundColor) {
$img->setDefaultBackgroundColor($backgroundColor);
verbose("Using default background_color = $backgroundColor");
} }
@@ -578,11 +588,12 @@ verbose("dpr = $dpr");
* convolve - image convolution as in http://php.net/manual/en/function.imageconvolution.php * convolve - image convolution as in http://php.net/manual/en/function.imageconvolution.php
*/ */
$convolve = get('convolve', null); $convolve = get('convolve', null);
$convolutionConstant = getConfig('convolution_constant', array());
// Check if the convolve is matching an existing constant // Check if the convolve is matching an existing constant
if ($convolve && isset($config['convolution_constant'])) { if ($convolve && isset($convolutionConstant)) {
$img->addConvolveExpressions($config['convolution_constant']); $img->addConvolveExpressions($convolutionConstant);
verbose("convolve constant = " . print_r($config['convolution_constant'], 1)); verbose("convolve constant = " . print_r($convolutionConstant, 1));
} }
verbose("convolve = " . print_r($convolve, 1)); verbose("convolve = " . print_r($convolve, 1));
@@ -598,6 +609,22 @@ verbose("upscale = $upscale");
/**
* Get details for post processing
*/
$postProcessing = getConfig('postprocessing', array(
'png_filter' => false,
'png_filter_cmd' => '/usr/local/bin/optipng -q',
'png_deflate' => false,
'png_deflate_cmd' => '/usr/local/bin/pngout -q',
'jpeg_optimize' => false,
'jpeg_optimize_cmd' => '/usr/local/bin/jpegtran -copy none -optimize',
));
/** /**
* Display image if verbose mode * Display image if verbose mode
*/ */
@@ -633,10 +660,12 @@ EOD;
/** /**
* Load, process and output the image * Load, process and output the image
*/ */
$cachePath = getConfig('cache_path', null);
$img->log("Incoming arguments: " . print_r(verbose(), 1)) $img->log("Incoming arguments: " . print_r(verbose(), 1))
->setSaveFolder($config['cache_path']) ->setSaveFolder($cachePath)
->useCache($useCache) ->useCache($useCache)
->setSource($srcImage, $config['image_path']) ->setSource($srcImage, $imagePath)
->setOptions( ->setOptions(
array( array(
// Options for calculate dimensions // Options for calculate dimensions
@@ -679,12 +708,12 @@ $img->log("Incoming arguments: " . print_r(verbose(), 1))
->setJpegQuality($quality) ->setJpegQuality($quality)
->setPngCompression($compress) ->setPngCompression($compress)
->useOriginalIfPossible($useOriginal) ->useOriginalIfPossible($useOriginal)
->generateFilename($config['cache_path']) ->generateFilename($cachePath)
->useCacheIfPossible($useCache) ->useCacheIfPossible($useCache)
->load() ->load()
->preResize() ->preResize()
->resize() ->resize()
->postResize() ->postResize()
->setPostProcessingOptions($config['postprocessing']) ->setPostProcessingOptions($postProcessing)
->save() ->save()
->output(); ->output();

View File

@@ -27,15 +27,21 @@ return array(
* Check that the imagefile is a file below 'image_path' using realpath(). * Check that the imagefile is a file below 'image_path' using realpath().
* Security constraint to avoid reaching images outside image_path. * Security constraint to avoid reaching images outside image_path.
* This means that symbolic links to images outside the image_path will fail. * This means that symbolic links to images outside the image_path will fail.
*
* Default value:
* image_path_constraint: true
*/ */
'image_path_constraint' => true, //'image_path_constraint' => false,
/** /**
* A regexp for validating characters in the image filename. * A regexp for validating characters in the image filename.
*
* Default value:
* valid_filename: '#^[a-z0-9A-Z-/_\.:]+$#'
*/ */
'valid_filename' => '#^[a-z0-9A-Z-/_\.:]+$#', //'valid_filename' => '#^[a-z0-9A-Z-/_\.:]+$#',
@@ -50,17 +56,19 @@ return array(
* remote_password: false // as in do not use password * remote_password: false // as in do not use password
* remote_pattern: null // use default values from CImage * remote_pattern: null // use default values from CImage
*/ */
//'remote_allow' => true, 'remote_allow' => true,
//'remote_password' => false, // "secret-password", //'remote_password' => false, // "secret-password",
//'remote_pattern' => '#^https?://#', //'remote_pattern' => '#^https?://#',
/** /**
* Set default timezone, it defaults to UTC if not specified. * Set default timezone.
* *
* Default values.
* default_timezone: ini_get('default_timezone') or 'UTC'
*/ */
//'default_timezone' => 'UTC', //'default_timezone' => 'UTC',
@@ -69,9 +77,12 @@ return array(
* This is basically a security constraint to avoid using resources on creating * This is basically a security constraint to avoid using resources on creating
* large (unwanted) images. * large (unwanted) images.
* *
* Default values.
* max_width: 2000
* max_height: 2000
*/ */
'max_width' => 2000, //'max_width' => 2000,
'max_height' => 2000, //'max_height' => 2000,
@@ -83,6 +94,8 @@ return array(
* the alpha value is between 00 (opaqe) and 7F (transparent), * the alpha value is between 00 (opaqe) and 7F (transparent),
* that is between 00000000-FFFFFF7F. * that is between 00000000-FFFFFF7F.
* *
* Default values.
* background_color: As specified by CImage
*/ */
//'background_color' => "FFFFFF", //'background_color' => "FFFFFF",
//'background_color' => "FFFFFF7F", //'background_color' => "FFFFFF7F",
@@ -92,7 +105,19 @@ return array(
/** /**
* Post processing of images using external tools, set to true or false * Post processing of images using external tools, set to true or false
* and set command to be executed. * and set command to be executed.
*
* Default values.
*
* png_filter: false
* png_filter_cmd: '/usr/local/bin/optipng -q'
*
* png_deflate: false
* png_deflate_cmd: '/usr/local/bin/pngout -q'
*
* jpeg_optimize: false
* jpeg_optimize_cmd: '/usr/local/bin/jpegtran -copy none -optimize'
*/ */
/*
'postprocessing' => array( 'postprocessing' => array(
'png_filter' => false, 'png_filter' => false,
'png_filter_cmd' => '/usr/local/bin/optipng -q', 'png_filter_cmd' => '/usr/local/bin/optipng -q',
@@ -103,22 +128,29 @@ return array(
'jpeg_optimize' => false, 'jpeg_optimize' => false,
'jpeg_optimize_cmd' => '/usr/local/bin/jpegtran -copy none -optimize', 'jpeg_optimize_cmd' => '/usr/local/bin/jpegtran -copy none -optimize',
), ),
*/
/** /**
* Create custom convolution expressions, matrix 3x3, divisor and * Create custom convolution expressions, matrix 3x3, divisor and
* offset. * offset.
*
* Default values.
* convolution_constant: array()
*/ */
/*
'convolution_constant' => array( 'convolution_constant' => array(
//'sharpen' => '-1,-1,-1, -1,16,-1, -1,-1,-1, 8, 0', //'sharpen' => '-1,-1,-1, -1,16,-1, -1,-1,-1, 8, 0',
//'sharpen-alt' => '0,-1,0, -1,5,-1, 0,-1,0, 1, 0', //'sharpen-alt' => '0,-1,0, -1,5,-1, 0,-1,0, 1, 0',
), ),
*/
/** /**
* Create custom shortcuts for more advanced expressions. * Create custom shortcuts for more advanced expressions.
*
* Default values.
* shortcut: array()
*/ */
'shortcut' => array( 'shortcut' => array(
'sepia' => "&f=grayscale&f0=brightness,-10&f1=contrast,-20&f2=colorize,120,60,0,0&sharpen", 'sepia' => "&f=grayscale&f0=brightness,-10&f1=contrast,-20&f2=colorize,120,60,0,0&sharpen",
@@ -137,6 +169,9 @@ return array(
* &width=w1 // results in width=613 * &width=w1 // results in width=613
* &width=c2 // results in spanning two columns with a gutter, 30*2+10=70 * &width=c2 // results in spanning two columns with a gutter, 30*2+10=70
* &width=c24 // results in spanning whole grid 24*30+((24-1)*10)=950 * &width=c24 // results in spanning whole grid 24*30+((24-1)*10)=950
*
* Default values.
* size_constant: array()
*/ */
'size_constant' => function () { 'size_constant' => function () {
@@ -163,6 +198,8 @@ return array(
/** /**
* Predefined aspect ratios. * Predefined aspect ratios.
* *
* Default values.
* aspect_ratio_constant: array()
*/ */
'aspect_ratio_constant' => function () { 'aspect_ratio_constant' => function () {
return array( return array(