Rename image class variables to match our current naming convention. Implement a unit test for
the image class
This commit is contained in:
@@ -7,89 +7,95 @@
|
|||||||
* Image class for handling screenshot and thumbnail image files.
|
* Image class for handling screenshot and thumbnail image files.
|
||||||
*/
|
*/
|
||||||
class Image {
|
class Image {
|
||||||
var $file; // absolute path from the docroot
|
var $sFile; // absolute path from the docroot
|
||||||
var $debug_log;
|
var $aDebugLog;
|
||||||
var $image;
|
var $oImage;
|
||||||
var $width;
|
var $iWidth;
|
||||||
var $height;
|
var $iHeight;
|
||||||
var $type;
|
var $iType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor:
|
* Constructor:
|
||||||
* $file is the full path to the image. $this->isLoaded()
|
* $sFile is the full path to the image. $this->isLoaded()
|
||||||
* should really be checked after making a new object.
|
* should really be checked after making a new object.
|
||||||
*/
|
*/
|
||||||
function Image($sRelativePath)
|
function Image($sPath, $bAbsolutePath=false)
|
||||||
{
|
{
|
||||||
$this->file = appdb_fullpath($sRelativePath);
|
/* if $bAbsolutePath is true we should use the $sPath without modification */
|
||||||
$info = @getimagesize($this->file);
|
/* otherwise use appdb_fullpath() to convert the relative $sPath into a absolute path */
|
||||||
|
if($bAbsolutePath)
|
||||||
|
$this->sFile = $sPath;
|
||||||
|
else /* relative path */
|
||||||
|
$this->sFile = appdb_fullpath($sPath);
|
||||||
|
|
||||||
|
$oInfo = @getimagesize($this->sFile);
|
||||||
|
|
||||||
if( empty($info) )
|
if( empty($oInfo) )
|
||||||
{
|
{
|
||||||
$this->set_debuglog("Failed to load file ".$this->file);
|
$this->set_debuglog("Failed to load file ".$this->sFile);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch( $info[2] )
|
switch( $oInfo[2] )
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
$data = imagecreatefromjpeg($this->file);
|
$oImage = imagecreatefromjpeg($this->sFile);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
$data = imagecreatefrompng($this->file);
|
$oImage = imagecreatefrompng($this->sFile);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default;
|
default;
|
||||||
$this->set_debuglog("Image type ({$info[2]}) unkown");
|
$this->set_debuglog("Image type ({$oInfo[2]}) unknown");
|
||||||
return;
|
return;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->image = $data;
|
$this->oImage = $oImage;
|
||||||
$this->width = $info[0];
|
$this->iWidth = $oInfo[0];
|
||||||
$this->height = $info[1];
|
$this->iHeight = $oInfo[1];
|
||||||
$this->type = $info[2];
|
$this->iType = $oInfo[2];
|
||||||
|
|
||||||
$this->set_debuglog("New image class created with as $file as"
|
$this->set_debuglog("New image class created with as $sFile as"
|
||||||
." file and {$info[2]} as type. Dimensions"
|
." file and {$oInfo[2]} as type. Dimensions"
|
||||||
." {$info[0]}x{$info[1]}");
|
." {$oInfo[0]}x{$oInfo[1]}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* isLoaded()
|
* isLoaded()
|
||||||
* This function should always be checked after loading a file
|
* This function should always be checked after loading a file
|
||||||
* with the constructor. Rteturns true if the image has been
|
* with the constructor. Returns true if the image has been
|
||||||
* succesfully loaded.
|
* succesfully loaded.
|
||||||
*/
|
*/
|
||||||
function isLoaded()
|
function isLoaded()
|
||||||
{
|
{
|
||||||
if($this->width > 0 AND $this->height > 0)
|
if($this->iWidth > 0 AND $this->iHeight > 0)
|
||||||
return true;
|
return true;
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the latest debug log made for the last function. If $full is
|
* Returns the latest debug log made for the last function. If $bFull is
|
||||||
* set it will return the full log as array of the object.
|
* set it will return the full log as array of the object.
|
||||||
*/
|
*/
|
||||||
function get_debuglog($full = 0)
|
function get_debuglog($bFull = 0)
|
||||||
{
|
{
|
||||||
if($full)
|
if($bFull)
|
||||||
return $this->debug_log;
|
return $this->aDebugLog;
|
||||||
else
|
else
|
||||||
return end($this->debug_log);
|
return end($this->aDebugLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_width()
|
function get_width()
|
||||||
{
|
{
|
||||||
return $this->width;
|
return $this->iWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_height()
|
function get_height()
|
||||||
{
|
{
|
||||||
return $this->height;
|
return $this->iHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -97,46 +103,46 @@ class Image {
|
|||||||
*/
|
*/
|
||||||
function get_image_resource()
|
function get_image_resource()
|
||||||
{
|
{
|
||||||
return $this->image;
|
return $this->oImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* make_thumb()
|
* make_thumb()
|
||||||
*
|
*
|
||||||
* Calculates resize based on one parameter and calculates the other
|
* Calculates resize based on one parameter and calculates the other
|
||||||
* with the right aspect ratio. If you want to use $new_height set
|
* with the right aspect ratio. If you want to use $iNewHeight set
|
||||||
* $new_width to 0.
|
* $iNewWidth to 0.
|
||||||
*
|
*
|
||||||
* If none are set APPDB_THUMBNAIL_WIDTH is used. If both are set
|
* If none are set APPDB_THUMBNAIL_WIDTH is used. If both are set
|
||||||
* $new_width is used.
|
* $iNewWidth is used.
|
||||||
*
|
*
|
||||||
* If you want to make a border, look at resize_image_border() comment
|
* If you want to make a border, look at resize_image_border() comment
|
||||||
* and set $border_width and $border_color as appropriate.
|
* and set $iBorderWidth and $sBorderColor as appropriate.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function make_thumb($new_width, $new_height, $border_width = 0, $border_color = '')
|
function make_thumb($iNewWidth, $iNewHeight, $iBorderWidth = 0, $sBorderColor = '')
|
||||||
{
|
{
|
||||||
|
|
||||||
if($new_width == 0 AND $new_height == 0)
|
if($iNewWidth == 0 AND $iNewHeight == 0)
|
||||||
{
|
{
|
||||||
$new_width = APPDB_THUMBNAIL_WIDTH;
|
$iNewWidth = APPDB_THUMBNAIL_WIDTH;
|
||||||
$new_height = $this->calculate_proportions($this->width, $this->height,$new_width);
|
$iNewHeight = $this->calculate_proportions($this->iWidth, $this->iHeight,$iNewWidth);
|
||||||
}
|
}
|
||||||
else if($new_width > 0)
|
else if($iNewWidth > 0)
|
||||||
{
|
{
|
||||||
$new_height = $this->calculate_proportions($this->width, $this->height,$new_width);
|
$iNewHeight = $this->calculate_proportions($this->iWidth, $this->iHeight,$iNewWidth);
|
||||||
}
|
}
|
||||||
else if($new_height > 0)
|
else if($iNewHeight > 0)
|
||||||
{
|
{
|
||||||
$new_width = $this->calculate_proportions($this->width, $this->height, 0, $new_height);
|
$iNewWidth = $this->calculate_proportions($this->iWidth, $this->iHeight, 0, $iNewHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->set_debuglog("Resizing image to $new_width x $new_height");
|
$this->set_debuglog("Resizing image to $iNewWidth x $iNewHeight");
|
||||||
|
|
||||||
if(!empty($border_color) and $border_width > 0)
|
if(!empty($sBorderColor) and $iBorderWidth > 0)
|
||||||
$this->resize_image_border($border_color,$border_width,$new_height,$new_width);
|
$this->resize_image_border($sBorderColor,$iBorderWidth,$iNewHeight,$iNewWidth);
|
||||||
else
|
else
|
||||||
$this->resize_image($new_width,$new_height);
|
$this->resize_image($iNewWidth,$iNewHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -148,96 +154,97 @@ class Image {
|
|||||||
* If none are set APPDB_SCREENSHOT_MAXWIDTH and APPDB_SCREENSHOT_MAXHEIGHT
|
* If none are set APPDB_SCREENSHOT_MAXWIDTH and APPDB_SCREENSHOT_MAXHEIGHT
|
||||||
* are used.
|
* are used.
|
||||||
*/
|
*/
|
||||||
function make_full($max_width = 0, $max_height = 0)
|
function make_full($iMaxWidth = 0, $iMaxHeight = 0)
|
||||||
{
|
{
|
||||||
if(!$max_width > 0)
|
if(!$iMaxWidth > 0)
|
||||||
$max_width = APPDB_SCREENSHOT_MAXWIDTH;
|
$iMaxWidth = APPDB_SCREENSHOT_MAXWIDTH;
|
||||||
|
|
||||||
if(!$max_height > 0)
|
if(!$iMaxHeight > 0)
|
||||||
$max_height = APPDB_SCREENSHOT_MAXHEIGHT;
|
$iMaxHeight = APPDB_SCREENSHOT_MAXHEIGHT;
|
||||||
|
|
||||||
if($this->width > $max_width)
|
if($this->iWidth > $iMaxWidth)
|
||||||
{
|
{
|
||||||
/* The width is too much */
|
/* The width is too much */
|
||||||
$new_width = $max_width;
|
$iNewWidth = $iMaxWidth;
|
||||||
$new_height = $this->calculate_proportions($this->width,$this->height,$new_width);
|
$iNewHeight = $this->calculate_proportions($this->iWidth,$this->iHeight,$iNewWidth);
|
||||||
|
|
||||||
/* Check if the height is also within the limits */
|
/* Check if the height is also within the limits */
|
||||||
if($new_height > $max_height )
|
if($iNewHeight > $iMaxHeight )
|
||||||
{
|
{
|
||||||
$new_width = $this->calculate_proportions($new_width,$new_height,0,$max_height);
|
$iNewWidth = $this->calculate_proportions($iNewWidth,$iNewHeight,0,$iMaxHeight);
|
||||||
$new_height = $max_height;
|
$iNewHeight = $iMaxHeight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if($this->height > $max_height)
|
else if($this->iHeight > $iMaxHeight)
|
||||||
{
|
{
|
||||||
/* Width was ok, height not */
|
/* Width was ok, height not */
|
||||||
$new_width = $this->calculate_proportions($this->width,$this->height,0,$max_height);
|
$iNewWidth = $this->calculate_proportions($this->iWidth,$this->iHeight,0,$iMaxHeight);
|
||||||
$new_height = $max_height;
|
$iNewHeight = $iMaxHeight;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* All ok */
|
/* All ok */
|
||||||
$new_width = $this->width;
|
$iNewWidth = $this->iWidth;
|
||||||
$new_height = $this->height;
|
$iNewHeight = $this->iHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->set_debuglog("Resizing image to $new_width x $new_height");
|
$this->set_debuglog("Resizing image to $iNewWidth x $iNewHeight");
|
||||||
|
|
||||||
$this->resize_image($new_width, $new_height);
|
$this->resize_image($iNewWidth, $iNewHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* resize_image()
|
* resize_image()
|
||||||
*
|
*
|
||||||
* Resizes the image with the width and height specified with
|
* Resizes the image with the width and height specified with
|
||||||
* $new_height and $new_width.
|
* $iNewHeight and $iNewWidth.
|
||||||
*/
|
*/
|
||||||
function resize_image($new_width, $new_height)
|
function resize_image($iNewWidth, $iNewHeight)
|
||||||
{
|
{
|
||||||
// GD 2.x
|
// GD 2.x
|
||||||
if(function_exists("imagecreatetruecolor"))
|
if(function_exists("imagecreatetruecolor"))
|
||||||
{
|
{
|
||||||
$new = imagecreatetruecolor($new_width, $new_height);
|
$oNewImage = imagecreatetruecolor($iNewWidth, $iNewHeight);
|
||||||
imagecopyresampled($new,$this->image,0,0,0,0,
|
imagecopyresampled($oNewImage,$this->oImage,0,0,0,0,
|
||||||
$new_width,$new_height,$this->width,$this->height);
|
$iNewWidth,$iNewHeight,$this->iWidth,$this->iHeight);
|
||||||
} else // GD 1.x
|
} else // GD 1.x
|
||||||
{
|
{
|
||||||
$new = imagecreate($new_width, $new_height);
|
$oNewImage = imagecreate($iNewWidth, $iNewHeight);
|
||||||
imagecopyresized($new,$this->image,0,0,0,0,
|
imagecopyresized($oNewImage,$this->oImage,0,0,0,0,
|
||||||
$new_width,$new_height,$this->width,$this->height);
|
$iNewWidth,$iNewHeight,$this->iWidth,$this->iHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->set_debuglog("imagecopyresized($new,$this->image,0,0,0,0,$new_width,$new_height,$this->width,$this->height);");
|
$this->set_debuglog("imagecopyresized($new,$this->oImage,0,0,0,0,$iNewWidth,"
|
||||||
imagedestroy($this->image);
|
."$iNewHeight,$this->iWidth,$this->iHeight);");
|
||||||
$this->image = $new;
|
imagedestroy($this->oImage);
|
||||||
$this->width = $new_witdh;
|
$this->oImage = $oNewImage;
|
||||||
$this->height= $new_height;
|
$this->iWidth = $iNewWidth;
|
||||||
|
$this->iHeight= $iNewHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* resize_image_border()
|
* resize_image_border()
|
||||||
*
|
*
|
||||||
* Resizes the image. With the $new_width + $border_width*2
|
* Resizes the image. With the $iNewWidth + $iBorderWidth*2
|
||||||
* and $new_height + $border_width*2 as size. $border_color is a
|
* and $iNewHeight + $iBorderWidth*2 as size. $sBorderColor is a
|
||||||
* HTML hexadecimal color (like #0000FF)
|
* HTML hexadecimal color (like #0000FF)
|
||||||
*/
|
*/
|
||||||
function resize_image_border($border_color, $border_width, $new_height, $new_width)
|
function resize_image_border($sBorderColor, $iBorderWidth, $iNewHeight, $iNewWidth)
|
||||||
{
|
{
|
||||||
|
|
||||||
$r = hexdec(substr($border_color, 1, 2));
|
$r = hexdec(substr($sBorderColor, 1, 2));
|
||||||
$g = hexdec(substr($border_color, 3, 2));
|
$g = hexdec(substr($sBorderColor, 3, 2));
|
||||||
$b = hexdec(substr($border_color, 5, 2));
|
$b = hexdec(substr($sBorderColor, 5, 2));
|
||||||
|
|
||||||
/* We multiply the border width by two because there are are borders
|
/* We multiply the border width by two because there are are borders
|
||||||
at both sides */
|
at both sides */
|
||||||
// GD 2.x
|
// GD 2.x
|
||||||
if(function_exists("imagecreatetruecolor"))
|
if(function_exists("imagecreatetruecolor"))
|
||||||
{
|
{
|
||||||
$new = imagecreatetruecolor($new_width + ($border_width*2), $new_height + ($border_width*2));
|
$new = imagecreatetruecolor($iNewWidth + ($iBorderWidth*2), $iNewHeight + ($iBorderWidth*2));
|
||||||
} else // GD 1.x
|
} else // GD 1.x
|
||||||
{
|
{
|
||||||
$new = imagecreate($new_width + ($border_width*2), $new_height + ($border_width*2));
|
$new = imagecreate($iNewWidth + ($iBorderWidth*2), $iNewHeight + ($iBorderWidth*2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make the border by filling it completely,
|
/* Make the border by filling it completely,
|
||||||
@@ -248,19 +255,23 @@ class Image {
|
|||||||
// GD 2.x
|
// GD 2.x
|
||||||
if(function_exists("imagecopyresampled"))
|
if(function_exists("imagecopyresampled"))
|
||||||
{
|
{
|
||||||
imagecopyresampled($new,$this->image,$border_width,$border_width,0,0, $new_width,$new_height,$this->width,$this->height);
|
imagecopyresampled($new,$this->oImage,$iBorderWidth,$iBorderWidth,
|
||||||
|
0,0, $iNewWidth,$iNewHeight,
|
||||||
|
$this->iWidth,$this->iHeight);
|
||||||
} else // GD 1.x
|
} else // GD 1.x
|
||||||
{
|
{
|
||||||
imagecopyresized($new,$this->image,$border_width,$border_width,0,0,$new_width,$new_height,$this->width,$this->height);
|
imagecopyresized($new,$this->oImage,$iBorderWidth,$iBorderWidth,
|
||||||
|
0,0,$iNewWidth,$iNewHeight,
|
||||||
|
$this->iWidth,$this->iHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->set_debuglog("imagecopyresized($new,$this->image,$border_width,$border_width,0,0,"
|
$this->set_debuglog("imagecopyresized($new,$this->oImage,$iBorderWidth,$iBorderWidth,0,0,"
|
||||||
." $new_width,$new_height,$this->width,$this->height); with a $border_width px border"
|
." $iNewWidth,$iNewHeight,$this->iWidth,$this->iHeight); with a $iBorderWidth px border"
|
||||||
." in $border_color");
|
." in $sBorderColor");
|
||||||
imagedestroy($this->image);
|
imagedestroy($this->oImage);
|
||||||
$this->image = $new;
|
$this->oImage = $new;
|
||||||
$this->width = $new_witdh;
|
$this->iWidth = $new_witdh;
|
||||||
$this->height= $new_height;
|
$this->iHeight= $iNewHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -275,48 +286,51 @@ class Image {
|
|||||||
* A warning for transparency. If you resize an image down with make_thumb()
|
* A warning for transparency. If you resize an image down with make_thumb()
|
||||||
* you loose the transparency on png images.
|
* you loose the transparency on png images.
|
||||||
*/
|
*/
|
||||||
function add_watermark($watermark,$min_mark_width = 0,$min_mark_height = 0)
|
function add_watermark($oWatermark, $iMinMarkWidth = 0, $iMinMarkHeight = 0)
|
||||||
{
|
{
|
||||||
|
$iWatermarkWidth = imagesx($oWatermark);
|
||||||
$watermark_width = imagesx($watermark);
|
$iWatermarkHeight = imagesy($oWatermark);
|
||||||
$watermark_height = imagesy($watermark);
|
|
||||||
|
|
||||||
if($this->width > $min_mark_width AND $this->height > $min_mark_height)
|
if($this->iWidth > $iMinMarkWidth AND
|
||||||
|
$this->iHeight > $iMinMarkHeight)
|
||||||
{
|
{
|
||||||
$watermark_x = $this->width - $watermark_width;
|
$iWatermark_x = $this->iWidth - $iWatermarkWidth;
|
||||||
$watermark_y = $this->height - $watermark_height;
|
$iWatermark_y = $this->iHeight - $iWatermarkHeight;
|
||||||
|
|
||||||
imagecopy($this->image, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height);
|
imagecopy($this->oImage, $oWatermark, $iWatermark_x, $iWatermark_y,
|
||||||
|
0, 0, $iWatermarkWidth, $iWatermarkHeight);
|
||||||
|
|
||||||
$this->set_debuglog("imagecopy($this->image, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height);");
|
$this->set_debuglog("imagecopy($this->oImage, $oWatermark,"
|
||||||
|
."$iWatermark_x, $iWatermark_y, 0, 0,"
|
||||||
|
."$iWatermarkWidth, $iWatermarkHeight);");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Output the image to a file set with $store.
|
* Output the image to a file set with $store.
|
||||||
*
|
*
|
||||||
* $type is optional and is set like the second index of getimagesize().
|
* $iType is optional and is set like the second index of getimagesize().
|
||||||
* If none (or 0) is set the orginal type of the file is used.
|
* If none (or 0) is set the orginal type of the file is used.
|
||||||
* If $store is not give, the current file name will be used.
|
* If $store is not give, the current file name will be used.
|
||||||
* $quality is the jpeg output quality (100 best, 0 worst). Default is 75
|
* $quality is the jpeg output quality (100 best, 0 worst). Default is 75
|
||||||
*/
|
*/
|
||||||
function output_to_file($store=null, $type = 0, $quality = 75)
|
function output_to_file($sOutputFilename=null, $iType = 0, $iQuality = 75)
|
||||||
{
|
{
|
||||||
if(!$store)
|
if(!$sOutputFilename)
|
||||||
$store = $this->file;
|
$sOutputFilename = $this->sFile;
|
||||||
if($type == 0)
|
if($iType == 0)
|
||||||
$type = $this->type;
|
$iType = $this->iType;
|
||||||
|
|
||||||
switch($type)
|
switch($iType)
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
imagejpeg($this->image,$store,$quality);
|
imagejpeg($this->oImage, $sOutputFilename, $iQuality);
|
||||||
$this->set_debuglog("Outputed file as jpeg to $store");
|
$this->set_debuglog("Outputed file as jpeg to $sOutputFilename");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
imagepng($this->image,$store);
|
imagepng($this->oImage, $sOutputFilename);
|
||||||
$this->set_debuglog("Outputed file as png to $store");
|
$this->set_debuglog("Outputed file as png to $sOutputFilename");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -330,31 +344,31 @@ class Image {
|
|||||||
/**
|
/**
|
||||||
* Output the files to the browser.
|
* Output the files to the browser.
|
||||||
*
|
*
|
||||||
* If $header is true a Content-type header with the correct type
|
* If $bHeader is true a Content-type header with the correct type
|
||||||
* is set.
|
* is set.
|
||||||
* $type is optional and is set like the second index of getimagesize().
|
* $iType is optional and is set like the second index of getimagesize().
|
||||||
* If none (or 0) is set the orginal type of the file is used.
|
* If none (or 0) is set the orginal type of the file is used.
|
||||||
*
|
*
|
||||||
* $quality is the jpeg output quality (100 best, 0 worst)
|
* $iQuality is the jpeg output quality (100 best, 0 worst)
|
||||||
*/
|
*/
|
||||||
function output_to_browser($header, $type = 0, $quality = 75)
|
function output_to_browser($bHeader, $iType = 0, $iQuality = 75)
|
||||||
{
|
{
|
||||||
if($type == 0 )
|
if($iType == 0 )
|
||||||
$type = $this->type;
|
$iType = $this->iType;
|
||||||
|
|
||||||
switch($type)
|
switch($iType)
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
if($header)
|
if($bHeader)
|
||||||
header('Content-type: image/jpeg');
|
header('Content-type: image/jpeg');
|
||||||
imagejpeg($this->image,'',$quality);
|
imagejpeg($this->oImage,'',$iQuality);
|
||||||
$this->set_debuglog("Outputed file as jpeg to browser");
|
$this->set_debuglog("Outputed file as jpeg to browser");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
if($header)
|
if($bHeader)
|
||||||
header('Content-type: image/png');
|
header('Content-type: image/png');
|
||||||
imagepng($this->image);
|
imagepng($this->oImage);
|
||||||
$this->set_debuglog("Outputed file as png to browser");
|
$this->set_debuglog("Outputed file as png to browser");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -369,8 +383,8 @@ class Image {
|
|||||||
*/
|
*/
|
||||||
function destroy()
|
function destroy()
|
||||||
{
|
{
|
||||||
if(is_resource($this->image))
|
if(is_resource($this->oImage))
|
||||||
imagedestroy($this->image);
|
imagedestroy($this->oImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -378,7 +392,7 @@ class Image {
|
|||||||
*/
|
*/
|
||||||
function delete()
|
function delete()
|
||||||
{
|
{
|
||||||
unlink($this->file);
|
unlink($this->sFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -386,17 +400,19 @@ class Image {
|
|||||||
* PRIVATE FUNCTIONS
|
* PRIVATE FUNCTIONS
|
||||||
************************/
|
************************/
|
||||||
|
|
||||||
function set_debuglog( $log ) {
|
function set_debuglog( $sLog )
|
||||||
$this->debug_log[] = $log;
|
{
|
||||||
|
$this->aDebugLog[] = $sLog;
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculate_proportions($width, $height, $new_width, $new_height = '0')
|
function calculate_proportions($iWidth, $iHeight,
|
||||||
|
$iNewWidth, $iNewHeight = '0')
|
||||||
{
|
{
|
||||||
if($new_width > 0)
|
if($iNewWidth > 0)
|
||||||
// we want to calculate the new height
|
// we want to calculate the new height
|
||||||
return ($height * $new_width) / $width;
|
return ($iHeight * $iNewWidth) / $iWidth;
|
||||||
else if( $new_height > 0 )
|
else if( $iNewHeight > 0 )
|
||||||
return ($width * $new_height) / $height;
|
return ($iWidth * $iNewHeight) / $iHeight;
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -406,16 +422,17 @@ class Image {
|
|||||||
|
|
||||||
class ImageResource extends Image {
|
class ImageResource extends Image {
|
||||||
|
|
||||||
function ImageResource($data,$type){
|
function ImageResource($oImage,$iType)
|
||||||
|
{
|
||||||
|
|
||||||
$this->image = $data;
|
$this->oImage = $oImage;
|
||||||
$this->width = imagesx($data);
|
$this->iWidth = imagesx($oImage);
|
||||||
$this->height = imagesy($data);
|
$this->iHeight = imagesy($oImage);
|
||||||
$this->type = $type;
|
$this->iType = $iType;
|
||||||
|
|
||||||
$this->set_debuglog("New image class created with as $data as"
|
$this->set_debuglog("New image class created with as $oImage as"
|
||||||
." image resource and $type as type. Dimensions"
|
." image resource and $iType as type. Dimensions"
|
||||||
." {$this->width}x{$this->height}");
|
." {$this->iWidth}x{$this->iHeight}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -5,9 +5,12 @@
|
|||||||
|
|
||||||
/* TODO: test the rest of the classes we have */
|
/* TODO: test the rest of the classes we have */
|
||||||
|
|
||||||
|
error_reporting(E_ALL ^ E_NOTICE);
|
||||||
|
|
||||||
include_once("test_user.php");
|
include_once("test_user.php");
|
||||||
echo "\n";
|
echo "\n";
|
||||||
include_once("test_db.php");
|
include_once("test_db.php");
|
||||||
|
echo "\n";
|
||||||
|
include_once("test_image.php");
|
||||||
|
|
||||||
?>
|
?>
|
||||||
248
unit_test/test_image.php
Normal file
248
unit_test/test_image.php
Normal file
@@ -0,0 +1,248 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/* unit tests for class image */
|
||||||
|
|
||||||
|
require_once("path.php");
|
||||||
|
require_once("test_common.php");
|
||||||
|
|
||||||
|
DEFINE(TEST_IMAGE_FILENAME, "/images/appdb_montage.jpg");
|
||||||
|
DEFINE(TEST_IMAGE_OUTPUT_FILENAME, "/tmp/tmpfile.png");
|
||||||
|
DEFINE(TEST_IMAGE_WIDTH, 391);
|
||||||
|
DEFINE(TEST_IMAGE_HEIGHT, 266);
|
||||||
|
DEFINE(TEST_IMAGE_WATERMARK, "/images/watermark.png");
|
||||||
|
|
||||||
|
function test_image_constructor()
|
||||||
|
{
|
||||||
|
test_start(__FUNCTION__);
|
||||||
|
|
||||||
|
$sImageFilename = TEST_IMAGE_FILENAME;
|
||||||
|
|
||||||
|
/* create a new image from a known image file */
|
||||||
|
$oImage = new Image($sImageFilename);
|
||||||
|
|
||||||
|
if(!$oImage->isLoaded())
|
||||||
|
{
|
||||||
|
echo "Error, unable to load image filename of ".$sImageFilename."\n";
|
||||||
|
echo "Internal filename is: ".$oImage->sFile."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* make sure the image size is correct */
|
||||||
|
if($oImage->get_width() != TEST_IMAGE_WIDTH)
|
||||||
|
{
|
||||||
|
echo "Expected width of ".TEST_IMAGE_WIDTH.", got ".$oImage->get_width()."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($oImage->get_height() != TEST_IMAGE_HEIGHT)
|
||||||
|
{
|
||||||
|
echo "Expected width of ".TEST_IMAGE_HEIGHT.", got ".$oImage->get_height()."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* test that isLoaded() returns false if we create an */
|
||||||
|
/* image object from a file that doesn't exist */
|
||||||
|
$oImage = new Image("somefilethatdoesntexist.png");
|
||||||
|
if($oImage->isLoaded())
|
||||||
|
{
|
||||||
|
echo "Error, isLoaded() returned true for a image that doesn't exist, expected false!\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_image_make_thumbnail()
|
||||||
|
{
|
||||||
|
test_start(__FUNCTION__);
|
||||||
|
|
||||||
|
$sImageFilename = TEST_IMAGE_FILENAME;
|
||||||
|
|
||||||
|
/* create a new image from a known image file */
|
||||||
|
$oImage = new Image($sImageFilename);
|
||||||
|
|
||||||
|
if(!$oImage->isLoaded())
|
||||||
|
{
|
||||||
|
echo "Error, unable to load image filename of ".$sImageFilename."\n";
|
||||||
|
echo "Internal filename is: ".$oImage->sFile."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$iWidth = 100;
|
||||||
|
$iHeight = ($iWidth * TEST_IMAGE_HEIGHT) / TEST_IMAGE_WIDTH; /* compute the expected height
|
||||||
|
from the ratio of the height
|
||||||
|
to width of the original image */
|
||||||
|
$iBorderWidth = 20;
|
||||||
|
$oImage->make_thumb($iWidth, $iHeight, $iBorderWidthm, "#0000FF");
|
||||||
|
|
||||||
|
/* did we get the correct size? */
|
||||||
|
$iActualWidth = $oImage->get_width();
|
||||||
|
if($iActualWidth != $iWidth)
|
||||||
|
{
|
||||||
|
echo "Expected width of $iWidth, got ".$iActualWidth."\n";
|
||||||
|
echo $oImage->get_debuglog(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$iActualHeight = $oImage->get_height();
|
||||||
|
if($iActualHeight != $iHeight)
|
||||||
|
{
|
||||||
|
echo "Expected height of $iHeight, got ".$iActualHeight."\n";
|
||||||
|
echo $oImage->get_debuglog(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_image_make_full()
|
||||||
|
{
|
||||||
|
test_start(__FUNCTION__);
|
||||||
|
|
||||||
|
$sImageFilename = TEST_IMAGE_FILENAME;
|
||||||
|
|
||||||
|
/* create a new image from a known image file */
|
||||||
|
$oImage = new Image($sImageFilename);
|
||||||
|
|
||||||
|
if(!$oImage->isLoaded())
|
||||||
|
{
|
||||||
|
echo "Error, unable to load image filename of ".$sImageFilename."\n";
|
||||||
|
echo "Internal filename is: ".$oImage->sFile."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$iWidth = 1000;
|
||||||
|
$iHeight = ($iWidth * TEST_IMAGE_HEIGHT) / TEST_IMAGE_WIDTH;
|
||||||
|
$oImage->make_full($iWidth, $iHeight);
|
||||||
|
|
||||||
|
/* we expect the width and height to be limited to the size of the image */
|
||||||
|
$iWidth = TEST_IMAGE_WIDTH;
|
||||||
|
$iHeight = TEST_IMAGE_HEIGHT;
|
||||||
|
|
||||||
|
/* did we get the correct size? */
|
||||||
|
$iActualWidth = $oImage->get_width();
|
||||||
|
if($iActualWidth != $iWidth)
|
||||||
|
{
|
||||||
|
echo "Expected width of $iWidth, got ".$iActualWidth."\n";
|
||||||
|
echo $oImage->get_debuglog(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$iActualHeight = $oImage->get_height();
|
||||||
|
if($iActualHeight != $iHeight)
|
||||||
|
{
|
||||||
|
echo "Expected height of $iHeight, got ".$iActualHeight."\n";
|
||||||
|
echo $oImage->get_debuglog(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_image_output_to_file()
|
||||||
|
{
|
||||||
|
test_start(__FUNCTION__);
|
||||||
|
|
||||||
|
$sImageFilename = TEST_IMAGE_FILENAME;
|
||||||
|
|
||||||
|
/* create a new image from a known image file */
|
||||||
|
$oImage = new Image($sImageFilename);
|
||||||
|
|
||||||
|
if(!$oImage->isLoaded())
|
||||||
|
{
|
||||||
|
echo "Error, unable to load image filename of ".$sImageFilename."\n";
|
||||||
|
echo "Internal filename is: ".$oImage->sFile."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* write the file to disk */
|
||||||
|
if(!$oImage->output_to_file(TEST_IMAGE_OUTPUT_FILENAME))
|
||||||
|
{
|
||||||
|
echo "image::output_to_file failed to output to filename of ".TEST_IMAGE_OUTPUT_FILENAME."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* check that we can load this file up */
|
||||||
|
$oImage2 = new Image(TEST_IMAGE_OUTPUT_FILENAME, true);
|
||||||
|
if(!$oImage2->isLoaded())
|
||||||
|
{
|
||||||
|
echo "Error, unable to load newly output image filename of ".TEST_IMAGE_OUTPUT_FILENAME."\n";
|
||||||
|
echo "Internal filename is: ".$oImage2->sFile."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* and make sure we can now remove it */
|
||||||
|
$oImage2->delete();
|
||||||
|
|
||||||
|
/* and check that it is unlinked by trying to open it up again */
|
||||||
|
$oImage2 = new Image(TEST_IMAGE_OUTPUT_FILENAME, true);
|
||||||
|
if($oImage2->isLoaded())
|
||||||
|
{
|
||||||
|
echo "Error, unlinking filename of ".TEST_IMAGE_OUTPUT_FILENAME." failed, we are able to\n";
|
||||||
|
echo " open up a file that should have been deleted.\n";
|
||||||
|
echo "Internal filename is: ".$oImage2->sFile."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_image_add_watermark()
|
||||||
|
{
|
||||||
|
test_start(__FUNCTION__);
|
||||||
|
|
||||||
|
$sImageFilename = TEST_IMAGE_FILENAME;
|
||||||
|
|
||||||
|
/* create a new image from a known image file */
|
||||||
|
$oImage = new Image($sImageFilename);
|
||||||
|
|
||||||
|
if(!$oImage->isLoaded())
|
||||||
|
{
|
||||||
|
echo "Error, unable to load image filename of ".$sImageFilename."\n";
|
||||||
|
echo "Internal filename is: ".$oImage->sFile."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* load the watermark up */
|
||||||
|
$oWatermark = new Image(TEST_IMAGE_WATERMARK);
|
||||||
|
if(!$oWatermark->isLoaded())
|
||||||
|
{
|
||||||
|
echo "Error, unable to load image filename of ".TEST_IMAGE_WATERMARK."\n";
|
||||||
|
echo "Internal filename is: ".$oWatermark->sFile."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$oImage->add_watermark($oWatermark->get_image_resource(), 50, 50);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(!test_image_constructor())
|
||||||
|
echo "test_image_constructor() failed!\n";
|
||||||
|
else
|
||||||
|
echo "test_image_constructor() passed\n";
|
||||||
|
|
||||||
|
if(!test_image_make_thumbnail())
|
||||||
|
echo "test_image_make_thumbnail() failed!\n";
|
||||||
|
else
|
||||||
|
echo "test_image_make_thumbnail() passed\n";
|
||||||
|
|
||||||
|
if(!test_image_make_full())
|
||||||
|
echo "test_image_make_full() failed!\n";
|
||||||
|
else
|
||||||
|
echo "test_image_make_full() passed\n";
|
||||||
|
|
||||||
|
if(!test_image_output_to_file())
|
||||||
|
echo "test_image_output_to_file() failed!\n";
|
||||||
|
else
|
||||||
|
echo "test_image_output_to_file() passed\n";
|
||||||
|
|
||||||
|
if(!test_image_add_watermark())
|
||||||
|
echo "test_image_add_watermark() failed!\n";
|
||||||
|
else
|
||||||
|
echo "test_image_add_watermark() passed\n";
|
||||||
|
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user