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

Add all code into one single script #73

This commit is contained in:
Mikael Roos 2015-01-27 22:40:28 +01:00
parent 5e08e5ed80
commit 3452ea1908
8 changed files with 4050 additions and 42 deletions

View File

@ -1,8 +1,8 @@
<?php
/**
* Get a image from a remote server using HTTP GET and If-Modified-Since.
*
*/
* Get a image from a remote server using HTTP GET and If-Modified-Since.
*
*/
class CRemoteImage
{
/**

View File

@ -280,6 +280,7 @@ Revision history
v0.6.x (latest)
* Combine all code into one singel script, `webroot/img_single.php` #73.
* Disallow hotlinking/leeching by configuration #46.
* Alias-name is without extension #47.
* Option `alias` now requires `password` to work #47.

65
create-img-single.bash Executable file
View File

@ -0,0 +1,65 @@
#!/bin/bash
#
# Paths and settings
#
TARGET="webroot/img_single.php"
NEWLINES="\n\n\n"
#
# Specify the utilities used
#
ECHO="printf"
#
# Main, start by checking basic usage
#
if [ $# -gt 0 ]
then
$ECHO "Usage: $0\n"
exit 1
fi
#
# Print out details on cache-directory
#
$ECHO "Creating webroot/img_single.php by combining the following files:"
$ECHO "\n"
$ECHO "\n webroot/img_single_header.php"
$ECHO "\n CHttpGet.php"
$ECHO "\n CRemoteImage.php"
$ECHO "\n CImage.php"
$ECHO "\n webroot/img.php"
$ECHO "\n"
$ECHO "\nPress enter to continue. "
read answer
#
# Create the $TARGET file
#
cat webroot/img_single_header.php > $TARGET
$ECHO "$NEWLINES" >> $TARGET
tail -n +2 CHttpGet.php >> $TARGET
$ECHO "$NEWLINES" >> $TARGET
tail -n +2 CRemoteImage.php >> $TARGET
$ECHO "$NEWLINES" >> $TARGET
tail -n +2 CImage.php >> $TARGET
$ECHO "$NEWLINES" >> $TARGET
tail -n +2 webroot/img.php >> $TARGET
$ECHO "$NEWLINES" >> $TARGET
$ECHO "\nDone."
$ECHO "\n"
$ECHO "\n"

View File

@ -1,6 +1,10 @@
<?php
/**
* Resize images on the fly using CImage, configuration is made in file named.
* Resize and crop images on the fly, store generated images in a cache.
*
* @author Mikael Roos mos@dbwebb.se
* @example http://dbwebb.se/opensource/cimage
* @link https://github.com/mosbth/cimage
*
*/
@ -114,12 +118,49 @@ function verbose($msg = null)
/**
* Get configuration options from file.
* Get configuration options from file, if the file exists, else use $config
* if its defined or create an empty $config.
*/
$configFile = __DIR__.'/'.basename(__FILE__, '.php').'_config.php';
$config = require $configFile;
call_user_func($config['error_reporting']);
if (is_file($configFile)) {
$config = require $configFile;
} else if (!isset($config)) {
$config = array();
}
/**
* Set mode as strict, production or development.
* Default is production environment.
*/
$mode = getConfig('mode', 'production');
// Settings for any mode
set_time_limit(20);
ini_set('gd.jpeg_ignore_warning', 1);
if (!extension_loaded('gd')) {
errorPage("Extension gd is nod loaded.");
}
// Specific settings for each mode
if ($mode == 'strict') {
error_reporting(0);
ini_set('display_errors', 0);
} else if ($mode == 'production') {
error_reporting(0);
ini_set('display_errors', 0);
} else if ($mode == 'development') {
error_reporting(-1);
ini_set('display_errors', 1);
} else {
errorPage("Unknown mode: $mode");
}
@ -246,7 +287,9 @@ if ($allowRemote && $passwordMatch !== false) {
* in config-file.
*/
$shortcut = get(array('shortcut', 'sc'), null);
$shortcutConfig = getConfig('shortcut', array());
$shortcutConfig = getConfig('shortcut', array(
'sepia' => "&f=grayscale&f0=brightness,-10&f1=contrast,-20&f2=colorize,120,60,0,0&sharpen",
));
verbose("shortcut = $shortcut");
@ -266,9 +309,8 @@ if (isset($shortcut)
$srcImage = get('src')
or errorPage('Must set src-attribute.');
// Check for valid/invalid characters
$imagePath = getConfig('image_path', null);
$imagePath = getConfig('image_path', __DIR__ . '/img/');
$imagePathConstraint = getConfig('image_path_constraint', true);
$validFilename = getConfig('valid_filename', '#^[a-z0-9A-Z-/_\.:]+$#');
@ -302,15 +344,41 @@ verbose("src = $srcImage");
/**
* Manage size constants from config file, use constants to replace values
* for width and height.
*/
$sizeConstant = getConfig('size_constant', function () {
// Set sizes to map constant to value, easier to use with width or height
$sizes = array(
'w1' => 613,
'w2' => 630,
);
// Add grid column width, useful for use as predefined size for width (or height).
$gridColumnWidth = 30;
$gridGutterWidth = 10;
$gridColumns = 24;
for ($i = 1; $i <= $gridColumns; $i++) {
$sizes['c' . $i] = ($gridColumnWidth + $gridGutterWidth) * $i - $gridGutterWidth;
}
return $sizes;
});
$sizes = call_user_func($sizeConstant);
/**
* width, w - set target width, affecting the resulting image width, height and resize options
*/
$newWidth = get(array('width', 'w'));
$maxWidth = getConfig('max_width', 2000);
$sizeConstant = getConfig('size_constant', array());
// Check to replace predefined size
$sizes = call_user_func($sizeConstant);
if (isset($sizes[$newWidth])) {
$newWidth = $sizes[$newWidth];
}
@ -358,7 +426,17 @@ verbose("new height = $newHeight");
* aspect-ratio, ar - affecting the resulting image width, height and resize options
*/
$aspectRatio = get(array('aspect-ratio', 'ar'));
$aspectRatioConstant = getConfig('aspect_ratio_constant', array());
$aspectRatioConstant = getConfig('aspect_ratio_constant', function () {
return array(
'3:1' => 3/1,
'3:2' => 3/2,
'4:3' => 4/3,
'8:5' => 8/5,
'16:10' => 16/10,
'16:9' => 16/9,
'golden' => 1.618,
);
});
// Check to replace predefined aspect ratio
$aspectRatios = call_user_func($aspectRatioConstant);
@ -735,11 +813,16 @@ EOD;
/**
* Get the cachepath from config.
*/
$cachePath = getConfig('cache_path', __DIR__ . '/../cache/');
/**
* Load, process and output the image
*/
$cachePath = getConfig('cache_path', null);
$img->log("Incoming arguments: " . print_r(verbose(), 1))
->setSaveFolder($cachePath)
->useCache($useCache)

View File

@ -7,8 +7,22 @@
*/
return array(
/**
* Set mode as 'strict', 'production' or 'development'.
*
* Default values:
* mode: 'production'
*/
//'mode' => 'production', // 'development',
/**
* Where are the sources for the classfiles.
*
* Default values:
* autoloader: null
* cimage_class: null
*/
'autoloader' => __DIR__ . '/../autoload.php',
//'cimage_class' => __DIR__ . '/../CImage.php',
@ -20,8 +34,8 @@ return array(
* End all paths with a slash.
*
* Default values:
* image_path: No default value
* cache_path: No default value
* image_path: __DIR__ . '/img/'
* cache_path: __DIR__ . '/../cache/'
* alias_path: null
*/
'image_path' => __DIR__ . '/img/',
@ -149,6 +163,7 @@ return array(
*/
/**
* Create custom convolution expressions, matrix 3x3, divisor and
* offset.
@ -190,11 +205,14 @@ return array(
* Create custom shortcuts for more advanced expressions.
*
* Default values.
* shortcut: array()
* shortcut: array(
* 'sepia' => "&f=grayscale&f0=brightness,-10&f1=contrast,-20&f2=colorize,120,60,0,0&sharpen",
* )
*/
/*
'shortcut' => array(
'sepia' => "&f=grayscale&f0=brightness,-10&f1=contrast,-20&f2=colorize,120,60,0,0&sharpen",
),
),*/
@ -211,8 +229,9 @@ return array(
* &width=c24 // results in spanning whole grid 24*30+((24-1)*10)=950
*
* Default values.
* size_constant: array()
* size_constant: As specified by the function below.
*/
/*
'size_constant' => function () {
// Set sizes to map constant to value, easier to use with width or height
@ -231,7 +250,7 @@ return array(
}
return $sizes;
},
},*/
@ -239,9 +258,9 @@ return array(
* Predefined aspect ratios.
*
* Default values.
* aspect_ratio_constant: array()
* aspect_ratio_constant: As the function below.
*/
'aspect_ratio_constant' => function () {
/*'aspect_ratio_constant' => function () {
return array(
'3:1' => 3/1,
'3:2' => 3/2,
@ -251,20 +270,5 @@ return array(
'16:9' => 16/9,
'golden' => 1.618,
);
},
/**
* Set error reporting to match development or production environment
*/
'error_reporting' => function () {
error_reporting(-1); // Report all type of errors
ini_set('display_errors', 1); // Display all errors
set_time_limit(20);
ini_set('gd.jpeg_ignore_warning', 1); // Ignore warning of corrupt jpegs
if (!extension_loaded('gd')) {
throw new Exception("Extension gd is nod loaded.");
}
},
},*/
);

3834
webroot/img_single.php Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,21 @@
<?php
/**
* Resize and crop images on the fly, store generated images in a cache.
*
* @author Mikael Roos mos@dbwebb.se
* @example http://dbwebb.se/opensource/cimage
* @link https://github.com/mosbth/cimage
*
*/
/**
* Change configuration details in the array below or create a separate file
* where you store the configuration details. Name the config file same name as
* this file and add '_config.php'. If this file is named 'img.php' then name the
* config file 'img_config.php'.
*
*/
$config = array(
);