CreateThumbnail 1.02
CreateThumbnail

CreateThumbnail is a powerful and customizable class for PHP 5 to create thumbnail images at 72 dpi.
Generated thumbnail (output file) will replace any existing file with the same output file name.



Main features

Input file can be GIF, JPEG or PNG images

Customizable compression

Allows sharpen and brightness filters

Preserve transparency for PNG and GIF images (except when applying sharpen filter).

Generate output log as array variable

Throw exceptions on critical errors



original
Input JPEG file
(140 Kb)

sharpen_soft_br_1_85 normal_75 normal_45 sharpen_soft_br_1_45
sharpen: soft
brightness: 1
quality: 85
final weight: 7 kb
default settings
quality: 75
final weight: 4.6 kb
quality: 45
final weight: 3 kb
sharpen: soft
brightness: 1
quality: 45
final weight: 3.6 kb


Methods
Public methods:

Methods Return value Description
new CreateThumbnail (str $sourceFile, $str outputFile, int $width, [int $height, [int $quality, [str $sharpen, [int $brightness, [str $prefix, [str $suffix], [str $altText ]]]]]]) object Instanciate the class.
All params should be omitted with this method because passing parameters is not the correct way to instanciate a class.
open (string $imageFile) object Opens and reads image file (input).
process ( [string $outputFile] ) object Create processed image file.
If not specified, output file is created in the source folder.
setWidth (integer $pixels) object Set output image width in pixels.
See below additional infos about this parameter.
setHeight (integer $pixels) object Set output image height in pixels.
See below additional infos about this parameter.
setQuality (integer $qualityCoeff) object Set output image quality, from 1 (smallest file) to 100 (biggest file but best quality).
Default is 75.
setSharpen (string $sharpenStyle) object Set output image sharpen.
Possibles values are:
      soft
      medium
      hard
      none (or empty). In this case, no sharpen filter is applied.
setBrightness (integer $brightnessCoeff) object Set output image brightness.
Possibles values are from -10 (dark) to 10 (bright). 0 or empty means no modification.
setPrefix (string $outputPrefix) object Adds specified prefix to output file name.
setSuffix (string $outputSuffix) object Adds specified suffix to output file name.
setAlt (string $altText) object Optional string that will be part of process log (see below).
getLog ( ["open"] | ["process"] ) array Returns full log (default) or specified part log.
getName () string Shortcut to getLog(["process"]["longName"]).
getWidth () integer Shortcut to getLog(["process"]["width"]).
getHeight () integer Shortcut to getLog(["process"]["height"]).
getAlt () string Shortcut to getLog(["process"]["alt"]). If no ALT text was specified with setAlt(), getAlt() returns getLog(["process"]["shortName"])

Public static methods:

Methods Return value Description
getThisDoc () string Returns a basic documentation.
getThisVersion () number Returns this class' version.

Error constants:

Name Description
ERROR_INTERNAL_GENERAL Generic internal error.
ERROR_OPEN_GENERAL Generic open error.
ERROR_OPEN_BADFILE Not a valid image file.
ERROR_PROCESS_GENERAL Generic process error.
ERROR_WRITE_GENERAL Generic write error.


About width and height parameters
Output image's size is calculated depending on width and height parameters:



Usage examples
Here is the simplest possible CreateThumbnail usage:

$data = new CreateThumbnail();
$data->setWidth($outputWidth)->setHeight($outputHeight); // output size in pixels
try {
	$data->open($sourceFile); // $sourceFile as string
	$data->process($outputFile); // $outputFile as string
}catch (Exception $e){
	echo $e->getCode()." - ".$e->getMessage();
}


Full options usage example:

$data = new CreateThumbnail();
$data->setWidth($pixels);
$data->setWidth($outputWidth); // in pixels
$data->setHeight($outputHeight); // in pixels
// $data->setPrefix($outputPrefixName); // suffix for output file name (string)
// $data->setSuffix($outputSuffixName); // prefix for output file name (string)
$data->setQuality($qualityCoeff); // integer from 1 to 100
$data->setSharpen($sharpenStyle); // "none" or "soft" or "medium" or "hard"
$data->setBrightness(brightnessCoeff); // integer from -10 to 10
// $data->setAlt("My image");

$sourceFile = "myFolder/myImage.jpg";
$outputFile = "myOutputFolder/thumb"; // if not specified, output file is created in the source folder

try {
	$data->open($sourceFile);
	// ----- image opening OK : go on
	try {
		$data->process($outputFile);
		echo '<img src="'.$data->getName().'" width="'.$data->getWidth().'" 
height="'.$data->getHeight().'" alt="'.$data->getAlt().'" />'; }catch (Exception $e){ echo $e->getCode().' - '.$e->getMessage(); } }catch (Exception $e){ echo $e->getCode().' - '.$e->getMessage(); } echo "OPEN LOG:".print_r($data->getLog("open"), 1); echo "PROCESS LOG:".print_r($data->getLog("process"), 1);


Limitations


Credits


Requirements

PHP 5+ with GD library 2.0.1 bundled version (2.0.28 or newer recommended)

PHP >= 5.1 for sharpen filter



Free download
CreateThumbnail is distributed as freeware, so it can be used for free but can not be sold.

CreateThumbnail


Disclaimer
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Version history
1.02 2012.02.08 Shortcuts for getLog(['process']) added.
getThisVersion() added.
setAlt() added.
1.01 2010.10.11 First public release.