continuacion
Código PHP:
function imageTransform()
{
// Sets default values of the class' properties
// We need to do it this way for the variables to have default values PHP 4
// public properties
$this->chmodValue = 0755;
$this->preserveSourceFileTime = true;
$this->error = 0;
$this->jpegOutputQuality = 75;
$this->resizeIfGreater = true;
$this->resizeIfSmaller = true;
$this->maintainAspectRatio = true;
$this->resizeToHeight = -1;
$this->resizeToWidth = -1;
$this->targetFile = "";
$this->sourceFile = "";
}
/**
* returns an image identifier representing the image obtained from sourceFile, the image's width and height
* and the image's type
*
* @access private
*/
function create_image_from_source_file()
{
// performs some error checking first
// if source file does not exists
if (!is_file($this->sourceFile) || !file_exists($this->sourceFile)) {
// save the error level and stop the execution of the script
$this->error = 1;
return false;
// if source file is not readable
} elseif (!is_readable($this->sourceFile)) {
// save the error level and stop the execution of the script
$this->error = 2;
return false;
// if target file is same as source file and source file is not writable
} elseif ($this->targetFile == $this->sourceFile && !is_writable($this->sourceFile)) {
// save the error level and stop the execution of the script
$this->error = 3;
return false;
// get source file width, height and type
// and if it founds a not-supported file type
} elseif (!list($sourceImageWidth, $sourceImageHeight, $sourceImageType) = @getimagesize($this->sourceFile)) {
// save the error level and stop the execution of the script
$this->error = 4;
return false;
// if no errors so far
} else {
// creates an image from file using extension dependant function
// checks for file extension
switch ($sourceImageType) {
// if gif
case 1:
// the following part gets the transparency color for a gif file
// this code is from the PHP manual and is written by
// fred at webblake dot net and webmaster at webnetwizard dotco dotuk, thanks!
$fp = fopen($this->sourceFile, "rb");
$result = fread($fp, 13);
$colorFlag = ord(substr($result,10,1)) >> 7;
$background = ord(substr($result,11));
if ($colorFlag) {
$tableSizeNeeded = ($background + 1) * 3;
$result = fread($fp, $tableSizeNeeded);
$this->transparentColorRed = ord(substr($result, $background * 3, 1));
$this->transparentColorGreen = ord(substr($result, $background * 3 + 1, 1));
$this->transparentColorBlue = ord(substr($result, $background * 3 + 2, 1));
}
fclose($fp);
// -- here ends the code related to transparency handling
// creates an image from file
$sourceImageIdentifier = @imagecreatefromgif($this->sourceFile);
break;
// if jpg
case 2:
// creates an image from file
$sourceImageIdentifier = @imagecreatefromjpeg($this->sourceFile);
break;
// if png
case 3:
// creates an image from file
$sourceImageIdentifier = @imagecreatefrompng($this->sourceFile);
break;
default:
// if file has an unsupported extension
// note that we call this if the file is not gif, jpg or png even though the getimagesize function
// handles more image types
$this->error = 4;
return false;
}
}
// if the date/time of the target file should be the same as the source file's
if ($this->preserveSourceFileTime) {
// read the source file's date/time
$this->sourceFileTime = filemtime($this->sourceFile);
}
// returns an image identifier representing the image obtained from sourceFile and the image's width and height
return array($sourceImageIdentifier, $sourceImageWidth, $sourceImageHeight, $sourceImageType);
}