PHP Classes

Your class

Recommend this page to a friend!

      GD2 Class  >  All threads  >  Your class  >  (Un) Subscribe thread alerts  
Subject:Your class
Summary:Some errors... .. .
Messages:7
Author:kankrelune
Date:2006-09-28 12:45:33
Update:2006-10-04 16:11:59
 

  1. Your class   Reply   Report abuse  
Picture of kankrelune kankrelune - 2006-09-28 12:45:33
Hi... first sorry for my bad english... .. .

I write to you for submit some optimisation/correction in your code... i work on a gd class actualy and for this raison i've look your class, it's a good work but you have made some errors... .. .

search the image type... you make preg_match("/.jpg/i", "$file")... but preg_match is very slower for this work type (no regular expression)... two posibilities...

replace preg_match by strrpos

if(strrpos($file,'.jpg') !== false)
$format = 'jpg';
elseif

but this method can bemake problème with double extension (it s unprobable but...) my_pics.jpg.gif... use elseif and not several if...

if(preg_match("/.jpg/i", "$file"))
{
$format = 'jpeg';
}
if (preg_match("/.gif/i", "$file"))
{
$format = 'gif';
}
if(preg_match("/.png/i", "$file"))
{
$format = 'png';
}

if my pics name is my_pics.gif.jpg the returned format is gif but my pics is in jpeg... .. .

the better method it's work with the extension

function getExtension($file)
{
if(false === ($pos = strrpos($file,'.')))
return '';

$ext = strtolower(substr($file,$pos+1));

return ($ext === 'jpg') ? 'jpeg' : $ext;
}

In CreateImageFromX() you have...

$extension = $format;
switch($extension)
{
case "gif": $image = imagecreatefromgif($file); break;

case "jpg":
case "jpeg":
$image = imagecreatefromjpeg($file);
break;

case "png":
$image = imagecreatefrompng($file);
break;
}

-1- $extension = $format; is totaly useless
-2- case "jpeg" can not exist
-3- your have not error handling in your class if i submit a .bmp image this can cause some problems
-4- why make several if and after switch for the same work... .. ?

function CreateImageFromX($file)
{
$ext = $this->getExtension($file);

if($ext === 'jpeg')
$image = imagecreatefromjpeg($file);
elseif($ext === 'gif')
$image = imagecreatefromgif($file);
elseif($ext === 'png')
$image = imagecreatefrompng($file);
else
{
$image = false;
// error handling
}

return $image;
}

it's must simple... no... .. ?

Use imageFilter is good but imageFilter is not available in php4 if the user server work with php4 the script crash if the user tel filter method... in your constructor make an attribut...

$this->imageFilter = function_exists('imagefilter');

and before apply filter make the simple test

if($this->imageFilter)
{
// imageFilter process
}
else //error handling

You have an error in CreateImageX()... for save the image in a file it's not

imagegif($im,'',$savefile);

and

imagepng($im,'',$savefile);

but

imagegif($im,$savefile);

and

imagepng($im,$savefile);

and for png save your have an error you always make file you don't display... .. .

you have not the good technique in IfSupportedImageTypes... you check if the file type is supported by your class but you dont check if is supported by gd for read or write... .. .

^_^

@ tchaoO°


  2. Re: Your class   Reply   Report abuse  
Picture of john mustafa yontar john mustafa yontar - 2006-09-29 13:39:03 - In reply to message 1 from kankrelune
thanks :D

  3. Re: Your class   Reply   Report abuse  
Picture of kankrelune kankrelune - 2006-10-02 10:11:35 - In reply to message 2 from john mustafa yontar
Re Hello...

You have changed the save bug... it's good... but for the jpeg format saving $quality is not defined... .. . ;o)

Change error_reporting to E_ALL in your php.ini it's a good way for clean coding... .. .

^_^

@ tchaOo°

  4. Re: Your class   Reply   Report abuse  
Picture of john mustafa yontar john mustafa yontar - 2006-10-03 16:50:24 - In reply to message 3 from kankrelune
ohhh man :D i'm stuppid :D this class in development time is 15 minute :D and very short time work in this class :(

  5. Re: Your class   Reply   Report abuse  
Picture of kankrelune kankrelune - 2006-10-03 17:17:53 - In reply to message 4 from john mustafa yontar
Not you're not stupid... it's a careless mistake... that arrives at everyone... .. . ;o)

If your are ok it take the class code, rewrite it with my ideas/manner an submit it in this forum for you can look it... .. .

@ tchaOo°

  6. Your class reworked   Reply   Report abuse  
Picture of kankrelune kankrelune - 2006-10-04 16:10:41 - In reply to message 5 from kankrelune
Ok i ve rewrite your class... if you find an interest in the modifications you can apply this to your class... i have commented the methods it's important for a good understanding of a code features... i ve add some feature (error handling, mirror effect...)

gd2.class.php

<?php


###################################################
# Class GD2 #
# Development By Mustafa Yontar rewrited by the Kankrelune #
# GPL Licanse #
# http://www.e4z.net - ra@e4z.net #
###################################################


/* Errors */
define('GD2_ERROR_NOTICE', 0);
define('GD2_ERROR_ERROR', 1);

class Gd2
{

var $useGdFilters;

var $imgInfos = array(
'type' => '',
'width' => 0,
'height' => 0
);

var $bgColor = array(
'red' => 255,
'green' => 255,
'blue' => 255
);

var $_gdIsLoaded;
var $_imgHandler;
var $_supportedTypes = array();
var $_errors = array(
'notices' => array(),
'errors' => array()
);


/*
* PHP 4 Constructor
*/
function Gd2($img = '', $red = 255, $green = 255, $blue = 255)
{
$this->__construct($img, $red, $green, $blue);
register_shutdown_function(array(&$this,'__destruct'));
}

/*
* PHP 5 Constructor
*/
function __construct($img = '', $red = 255, $green = 255, $blue = 255)
{
if(!$this->_gdIsLoaded = extension_loaded('gd'))
return $this->_setError('GD library is not installed in your system');
elseif(!function_exists('imagecreatetruecolor'))
return $this->_setError('This class require the version 2 of GD library');

$types = imageTypes();

if ($types & IMG_PNG)
{
$this->_supportedTypes['png']['read'] = true;
$this->_supportedTypes['png']['write'] = true;
}

if ($types & IMG_GIF || function_exists('imagegif'))
{
$this->_supportedTypes['gif']['read'] = true;
$this->_supportedTypes['gif']['write'] = true;
}
elseif (function_exists('imagecreatefromgif'))
{
$this->_supportedTypes['gif']['read'] = true;
$this->_supportedTypes['gif']['write'] = false;
}

if ($types & IMG_JPG)
{
$this->_supportedTypes['jpeg']['read'] = true;
$this->_supportedTypes['jpeg']['write'] = true;
}

$this->bgColor['red'] = min(255,max(0,$red));
$this->bgColor['green'] = min(255,max(0,$green));
$this->bgColor['blue'] = min(255,max(0,$blue));

$this->useGdFilters = function_exists('imagefilter');

if(is_file($img))
$this->createFromFile($img);
}

/*
* Destructor
*/
function __destruct() { $this->destroy(); }


/************************************ Publics Methods ************************************/



/*
* Image resource destroyer
*
* @return bool
* @access public
*/
function destroy()
{
if(is_resource($this->_imgHandler))
{
imagedestroy($this->_imgHandler);

return true;
}
return false;
}

/*
* Image resource destroyer
*
* @param string $file the file path
*
* @return bool
* @access public
*/
function createFromFile($file)
{
if(!$this->_gdIsLoaded)
return false;
elseif(!is_file($file))
return $this->_setError('Image file not found');

$this->_init();

$ext = $this->getExt($file);

if(!$this->_isSupported($ext,'read'))
return $this->_setError('Image "'.$ext.'" are not supported');

if ($ext === 'jpeg')
$this->_imgHandler = @imagecreatefromjpeg($file);
elseif($ext === 'png')
$this->_imgHandler = @imagecreatefrompng($file);
elseif($ext === 'gif')
$this->_imgHandler = @imagecreatefromgif($file);

if(!$this->_imgHandler)
return $this->_setError('Fail to create the image resource');

$this->imgInfos['type'] = $ext;
$this->imgInfos['width'] = imagesx($this->_imgHandler);
$this->imgInfos['height'] = imagesy($this->_imgHandler);

return true;
}

/*
* Resize the image to the specified dimensions
*
* @param int $new_width the image width
* @param int $new_height the image height
* @param int $x vertical offset for crop resizing
* @param int $y horizontal offset for crop resizing
* @param int $size image dimensions for crop resizing
*
* @return bool
* @access public
*/
function resizeImage( $new_width, $new_height, $x = 0, $y = 0,$size = 0)
{
if($this->_isReady())
{
$image_p = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_p, $this->_imgHandler, 0, 0,0,0, $new_width, $new_height, $this->imgInfos['width'], $this->imgInfos['height']);
imagedestroy($this->_imgHandler);
$this->_imgHandler = $image_p;

if(!empty($size))
{
$image2 = imagecreatetruecolor($size,$size);
imagecopyresampled($image2,$this->_imgHandler,$x,$y,0,0,$size,$size,$size,$size);
imagedestroy($this->_imgHandler);
$this->_imgHandler = $image2;
}

return true;
}
else return false;
}

/*
* Resize the image to the specified dimensions
*
* @param int $new_width the image width
* @param int $new_height the image height
* @param int $x vertical offset for crop resizing
* @param int $y horizontal offset for crop resizing
* @param int $size image dimensions for crop resizing
*
* @return bool
* @access public
*/
function oneSizeThumbnail($size)
{
if($this->_isReady())
{
if($this->imgInfos['width'] > $this->imgInfos['height'])
{
$size_percent = (int)($size / ($this->imgInfos['width'] / 100));
$new_height = (int) ($size_percent * ($this->imgInfos['height']/100));
$new_width = $size;
$y = ((int)$size - (int)$new_height) /2 ;
$x = 0;
}
else
{
$size_percent = (int)($size / ($this->imgInfos['height'] / 100));
$new_width = (int) ($size_percent * ($this->imgInfos['width']/100));
$new_height = $size;
$x = ($size - $new_width) / 2;
$y = 0;
}

return $this->resizeImage($new_width,$new_height,$x,$y,$size);
}
else return false;
}

/*
* Resize the image to the specified dimensions
*
* @param int $new_width the image width
* @param int $new_height the image height
* @param int $x vertical offset for crop resizing
* @param int $y horizontal offset for crop resizing
* @param int $size image dimensions for crop resizing
*
* @return bool
* @access public
*/
function maxSizeThumbnail($size)
{
if($this->_isReady())
{
if($this->imgInfos['width'] > $this->imgInfos['height'])
{
$size_percent = (int)($size / ($this->imgInfos['width'] / 100));
$new_height = (int) ($size_percent * ($this->imgInfos['height']/100));
$new_width = $size;
$y = ($size - $new_height) / 2;
$x = 0;
}
else
{
$size_percent = (int)($size / ($this->imgInfos['height'] / 100));
$new_width = (int) ($size_percent * ($this->imgInfos['width']/100));
$new_height = $size;
$x = ($size - $new_width) / 2;
$y = 0;
}

return $this->resizeImage($new_width,$new_height,$x,$y);
}
else return false;
}

/*
* Crop the image to the specified dimensions
*
* @param int $width the croped image width
* @param int $height the croped image height
* @param int $x vertical offset for croping
* @param int $y horizontal offset for croping
*
* @return bool
* @access public
*/
function cropImage($width,$height,$x,$y)
{
if($this->_isReady())
{
$image2 = imagecreatetruecolor($width, $height);
imagecopymerge($image2,$this->_imgHandler,0,0,$x,$y,$width, $height,100);
imagedestroy($this->_imgHandler);
$this->_imgHandler = $image2;

return true;
}
else return false;
}

/*
* Apply rotation to the image
*
* @param int $anle the rotation angle
* @param int $color the background color for some rotation
*
* @return bool
* @access public
*/
function imageRotate($angle,$color = 0)
{
if($this->_isReady())
{
if(!is_numeric($angle))
return $this->_setError('Invalid "angle" parameter');
elseif(empty($angle) || $angle === 360)
return true;

if(empty($color))
$color = imagecolorat($this->_imgHandler,0,0);
elseif(!is_array($color))
$color = $this->_hex2rgb($color);

$this->_imgHandler = imagerotate(
$this->_imgHandler,
$angle,
imageColorAllocate(
$this->_imgHandler,
$color['red'],
$color['green'],
$color['blue']
)
);
}
else return false;
}

/*
* Reverse the image
*
* @return bool
* @access public
*/
function mirror()
{
if($this->_isReady())
{
$tmpimage = imagecreatetruecolor($this->imgInfos['width'], $this->imgInfos['height']);

for ($x=0;$x<$this->imgInfos['width'];++$x)
imagecopy($tmpimage,$this->_imgHandler, $x, 0, $this->imgInfos['width'] - $x - 1, 0, 1, $this->imgInfos['height']);

imagedestroy($this->_imgHandler);
$this->_imgHandler = $tmpimage;

return true;
}
return false;
}

/*
* Apply Negate filter to the image
*
* @return bool
* @access public
*/
function effectNegate()
{
if($this->useGdFilters)
{
if($this->_isReady())
return imagefilter($this->_imgHandler, IMG_FILTER_NEGATE);
else
return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply Colorize filter to the image
*
* @param string | array $color the color of the filter
*
* @return bool
* @access public
*/
function effectColorize($color)
{
if($this->useGdFilters)
{
if($this->_isReady())
{
if(!is_array($color))
$color = $this->_hex2rgb($color);

return imagefilter(
$this->_imgHandler,
IMG_FILTER_COLORIZE,
$color['red'],
$color['green'],
$color['blue']
);
}
else return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply Grayscale filter to the image
*
* @return bool
* @access public
*/
function effectGrayscale()
{
if($this->useGdFilters)
{
if($this->_isReady())
return imagefilter($this->_imgHandler, IMG_FILTER_GRAYSCALE);
else
return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply Edge Detect filter to the image
*
* @return bool
* @access public
*/
function effectEdgeDetect()
{
if($this->useGdFilters)
{
if($this->_isReady())
return imagefilter($this->_imgHandler, IMG_FILTER_EDGEDETECT);
else
return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply Selective Blur filter to the image
*
* @return bool
* @access public
*/
function effectSelectiveBlur()
{
if($this->useGdFilters)
{
if($this->_isReady())
return imagefilter($this->_imgHandler, IMG_FILTER_SELECTIVE_BLUR);
else
return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply Contrast filter to the image
*
* @param int $val the effect level
*
* @return bool
* @access public
*/
function effectContrast($val)
{

if($this->useGdFilters)
{
if($this->_isReady())
{
return imagefilter($this->_imgHandler, IMG_FILTER_CONTRAST,$val);
}
else return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply Brightness filter to the image
*
* @param int $val the effect level
*
* @return bool
* @access public
*/
function effectBrightness($val)
{
if($this->useGdFilters)
{
if($this->_isReady())
return imagefilter($this->_imgHandler, IMG_FILTER_BRIGHTNESS,$val);
else
return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply Gausian Blur filter to the image
*
* @return bool
* @access public
*/
function effectGaussianBlur()
{
if($this->useGdFilters)
{
if($this->_isReady())
return imagefilter($this->_imgHandler, IMG_FILTER_GAUSSIAN_BLUR);
else
return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply Smooth filter to the image
*
* @param int $val the effect level
*
* @return bool
* @access public
*/
function effectSmooth($val)
{
if($this->useGdFilters)
{
if($this->_isReady())
return imagefilter($this->_imgHandler,IMG_FILTER_SMOOTH,$val);
else
return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply Emboss filter to the image
*
* @return bool
* @access public
*/
function effectEmboss()
{
if($this->useGdFilters)
{
if($this->_isReady())
return imagefilter($this->_imgHandler,IMG_FILTER_EMBOSS);
else
return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply Mean Removal filter to the image
*
* @return bool
* @access public
*/
function effectMeanRemoval()
{
if($this->useGdFilters)
{
if($this->_isReady())
return imagefilter($this->_imgHandler,IMG_FILTER_MEAN_REMOVAL);
else
return false;
}
else return $this->_setError('Image filters is not available on PHP4');
}

/*
* Apply negate filter to the image
*
* @return bool
* @access public
*/
function setTransparent($color)
{
if($this->_isReady())
{
if(!is_array($color))
$color = $this->_hex2rgb($color);

if(!imageistruecolor($this->_imgHandler))
$this->toTrueColor();

imagecolortransparent(
$this->_imgHandler,
imagecolorallocate(
$this->_imgHandler,
$color['red'],
$color['green'],
$color['blue']
)
);

}
else return false;
}

/*
* Convert an image with palette color to true color image
*
* @return bool
* @access public
*/
function toTrueColor()
{
if($this->_isReady())
{
if(!imageistruecolor($this->_imgHandler))
{
$img2 = imagecreatetruecolor($this->imgInfos['width'], $this->imgInfos['height']);

imagecopymerge(
$img2,
$this->_imgHandler,
0,
0,
0,
0,
$this->imgInfos['width'],
$this->imgInfos['height'],
$this->imgInfos['width'],
$this->imgInfos['height'],
100
);

imagedestroy($this->_imgHandler);
$this->_imgHandler = $img2;
}
return true;
}
return false;
}

/*
* set the background color for the created images
*
* @param string|array $color the color in hex or rgb (all "rgb" or "r" or "g" or "b") format
*
* @return bool
* @access public
*/
function setBgColor($color)
{
if(is_string($color))
{
$this->bgColor = $this->_hex2rgb($color);
return true;
}
elseif(is_array($color))
{
if($this->_isRgbColor($color))
{
$this->bgColor = $color;
return true;
}
elseif(isset($color['red']))
{
$this->bgColor['red'] = $color['red'];
return true;
}
elseif(isset($color['green']))
{
$this->bgColor['green'] = $color['green'];
return true;
}
elseif(isset($color['blue']))
{
$this->bgColor['blue'] = $color['blue'];
return true;
}
else return false;
}
else return false;
}

/*
* display the image
*
* @param string $type the output type
*
* @return bool
* @access public
*/
function display($type = '')
{
if($this->_isReady())
{
if($type === '') // i don't use empty because i don't remember if anyone of the IMAGETYPE_* constante have 0 for value
$type = $this->imgInfos['type'];
elseif(is_numeric($type))
$type = $this->type2Ext($type);
else
{
$type = strtolower($type);
if($type === 'jpg') $type = 'jpeg';
}

if(!$this->_isSupported($type,'write'))
return $this->_setError('Image "'.$type.'" not supported for output');

header('Content-type: image/'.$type);

if ($type === 'jpeg')
imagejpeg($this->_imgHandler);
elseif($type === 'png')
imagepng($this->_imgHandler);
elseif($type === 'gif')
imagegif($this->_imgHandler);

}
elseif($this->isError())
$this->_getErrorImage();
else
$this->_getErrorImage('No image handler found');
}


/*
* save the image
*
* @param string $filePath the save path
* @param int $quality output quality for jpeg
*
* @return bool
* @access public
*/
function save( $filePath, $quality = 75)
{
if($this->_isReady())
{
if(empty($filePath))
return $this->_setError('Can not save image file in a empty file path');

$type = $this->getExt($filePath);

if(!$this->_isSupported($type,'write'))
return $this->_setError('Image "'.$type.'" not supported for output');

if ($type === 'jpeg')
{
if(!is_numeric($quality))
$quality = 75;
else
$quality = min(100,max(0,$quality));

imagejpeg($this->_imgHandler,$filePath,$quality);
}
elseif ($type === 'png')
imagepng($this->_imgHandler,$filePath);
elseif ($type === 'gif')
imagegif($this->_imgHandler, $filePath);
}
}

/*
* convert an imagetype to the image extension string
*
* @param string $type the type of file you want get
*
* @return str on success or bool on fail
* @access public
*/
function type2Ext($type)
{
if($type === IMAGETYPE_GIF)
return 'gif';
elseif($type === IMAGETYPE_PNG)
return 'png';
elseif($type === IMAGETYPE_JPEG)
return 'jpeg';
else
return false;
}

/*
* Public isSupported method
*
* @param string $type the type of file you want check
* @param string|array $mode the mode you want check
*
* @return bool
* @access public
*/
function isSupported($type, $mode = 'read') { return $this->_isSupported(strtolower($type),(is_array($mode) ? $mode : strtolower($mode))); }


/*
* return the file extension
*
* @param string $fileName the file name or path
*
* @return str
* @access public
*/
function getExt($fileName)
{
if(empty($fileName))
return '';
elseif(false === ($pos = strrpos($fileName,'.')))
return '';

$ext = strtolower(substr($fileName,++$pos));

if($ext === 'jpg')
$ext = 'jpeg';

return $ext;
}

/*
* Check if the class have an error
*
* @return bool
* @access public
*/
function isError() { return !empty($this->_errors['errors']); }

/*
* Return the consigned errors
*
* @param bool $toStr returned the value on array or on string
* @param bool $html if return string the separator must be a normal line return with space or a html line break
*
*
* @return array | string
* @access public
*/
function getErrors( $toStr = false, $html = false)
{
if(empty($this->_errors['errors']))
return '';

if(!$toStr)
return $this->_errors['errors'];
elseif(count($this->_errors['errors']) === 1)
return $this->_errors['errors'][0];
else
{
$ret = '';

foreach($this->_errors['errors'] as $error)
$ret .= $error.($html ? '<br />' : "&nbsp;\n");

return $ret;
}
}

/*
* Return the last consigned error
*
* @return str
* @access public
*/
function getLastError()
{
if(empty($this->_errors['errors']))
return false;

return $this->_errors['errors'][(count($this->_errors)-1)];
}

/************************************ Privates Methodss ************************************/

/*
* Initialise the class attributs
*
* @return void
* @access private
*/
function _init()
{
$this->destroy();
$this->imgInfos = array();
}


/*
* Check if the class can work
*
* @return bool
* @access private
*/
function _isReady() { return ($this->_gdIsLoaded && !$this->isError() && is_resource($this->_imgHandler)); }

/*
* Check if an extension is supported for reading and/or writting
*
* @param string $type the type of file you want check
* @param string|array $mode the mode you want check
*
* @return bool
* @access private
*/
function _isSupported($type, $mode)
{
if(!isset($this->_supportedTypes[$type]))
return false;
elseif(is_array($mode))
return ($this->_supportedTypes[$type]['read'] && $this->_supportedTypes[$type]['write']);
elseif(!isset($this->_supportedTypes[$type][$mode]))
return false;
else
return $this->_supportedTypes[$type][$mode];
}

/*
* error handling
*
* @param string $msg the error message
* @param string|array $level the level of the error
*
* @return bool
* @access private
*/
function _setError( $msg, $level = GD2_ERROR_ERROR)
{
$msg = trim($msg);

if($level === GD2_ERROR_NOTICE)
{
$this->_errors['notices'][] = $msg;

return true;
}
else
{
$this->_errors['errors'][] = $msg;

return false;
}
}

/*
* create an image with the given error or with the consigned errors
*
* @param string $test the error message
*
* @return void
* @access private
*/
function _getErrorImage($text = '')
{
if(!$this->_gdIsLoaded)
{
$this->_gdIsNotLoaded();
return false;
}
elseif(empty($text))
$text = $this->getErrors(true);

$errors = explode("\n",$text);
$nbe = count($errors);

$width = ($this->imgInfos['width'] < 300) ? 300 : $this->imgInfos['width'];
$height = (30*$nbe)+60;
$finalheight = ($this->imgInfos['height'] < $height) ? $height : $this->imgInfos['height'];

$eImg = imagecreate($width,$finalheight);
$bg = imagecolorallocate($eImg, 255, 255, 255);
$textcolor = imagecolorallocate($eImg, 255, 0, 0);

for($i=0,$j=30;$i<$nbe;$i++,$j+=10)
imagestring($eImg, 2, 30, $j, trim($errors[$i]), $textcolor);

if($this->_isSupported('jpeg','write'))
{
header("Content-type: image/jpeg");
imagejpeg($eImg);
}
elseif($this->_isSupported('gif','write'))
{
header("Content-type: image/gif");
imagegif($eImg);
}
elseif($this->_isSupported('png','write'))
{
header("Content-type: image/png");
imagepng($eImg);
}
}

function _gdIsNotLoaded()
{
header("Content-type: image/jpeg");
echo base64_decode('/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4'.
'wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBkZWZhdWx0IHF1YWxpdHkK/9sAQwAIBgYHBgUIBwcHCQkIC'.
'gwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQk'.
'MCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyM'.
'jIy/8AAEQgAZAEsAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALU'.
'QAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXG'.
'BkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5S'.
'VlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29'.
'/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwA'.
'BAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDR'.
'EVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrK'.
'ztLW2t7i5usLDxMXGx8jJytLT1NXW19j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xKyxiKMKEDEFgMDgEgE+pAqWKKOCFIYY1jijUKiIMKoH'.
'AAA6CqjGSerMa9alUglGFnfdad+mvl18tijpv2j7fq/n+b5f2tfI35xs8iLO3Pbdu6d8980aB9o/wC'.
'Ec0v7Z5v2r7JF53nZ379g3bs85znOavJFHG0jJGqtI25yowWOAMn1OAB9AKIoo4IUhhjWOKNQqIgwq'.
'gcAADoKpRszKdVSi1bt+CsPoooqjEKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAK'.
'KKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAoooo'.
'AKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAoo'.
'ooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigA'.
'ooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiii'.
'gAooooAKKKKACiiigAooooAKKKKACiiigAooooA//2Q==');
}


/*
* Check if the given color is an rgb color array
*
* @param array $color the color to test
*
* @return bool
* @access private
*/
function _isRgbColor($color)
{
if(!isset($color['red'],$color['green'],$color['blue']))
return false;

return ($color['red'] < 255 && $color['red'] > 0 &&
$color['green'] < 255 && $color['green'] > 0 &&
$color['blue'] < 255 && $color['blue'] > 0);
}

/*
* convert an hex color to an rgb color array
*
* @param array $color the color to convert
*
* @return array
* @access private
*/
function _hex2rgb($hex)
{
$hex = strtr($hex,'#','');

if(strlen($hex) === 3)
return array(
'red' => hexdec($color{0}.$color{0}),
'green' => hexdec($color{1}.$color{1}),
'blue' => hexdec($color{2}.$color{2})
);
else
return array(
'red' => hexdec(substr($hex,0,2)),
'green' => hexdec(substr($hex,2,2)),
'blue' => hexdec(substr($hex,4,2))
);
}


}


?>



example.php

<?php

require('gd2.class.php');

$gd = new Gd2("DSC_0096.jpg");

if(isset($_GET['op']))
{
if($gd->useGdFilters)
{
switch((int)$_GET['op'])
{
case 1: $gd->resizeImage(150,150); break;
case 2: $gd->maxSizeThumbnail(150); break;
case 3: $gd->oneSizeThumbnail(150); break;
case 4: $gd->cropImage(200,200,50,50); break;
case 5: $gd->imageRotate(90); break;
case 6: $gd->mirror(); break;
case 7: $gd->effectBrightness(50); break;
case 8: $gd->effectSmooth(2); break;
case 9: $gd->effectEmboss(); break;
case 10: $gd->effectGaussianBlur(); break;
case 11: $gd->effectMeanRemoval(); break;
case 12: $gd->effectNegate(); break;
case 13: $gd->effectGrayscale(); break;
case 14: $gd->effectEdgeDetect(); break;
case 15: $gd->effectSelectiveBlur(); break;
case 16: $gd->effectContrast(-30); break;
case 17: $gd->effectColorize('#00ff00'); break;
case 18: $gd->createFromFile('DSC.jpg'); break;
}
}
else
{
switch((int)$_GET['op'])
{
case 1: $gd->resizeImage(150,150); break;
case 2: $gd->maxSizeThumbnail(150); break;
case 3: $gd->oneSizeThumbnail(150); break;
case 4: $gd->cropImage(200,200,50,50); break;
case 5: $gd->imageRotate(90); break;
case 6: $gd->mirror(); break;
case 7: $gd->createFromFile('DSC.jpg'); break;
}
}

$gd->display('png');
}
else
{
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>-[Gd2 Class Demo]-</title>
<style type="text/css">
body { background-color:#e2e2e2; }
th { background-color:#c0c0c0;text-align:center; }
img { margin-left:auto;margin-right:auto;border:none; }
table { margin-left:auto;margin-right:auto;text-align:center; }
.main { width:90%;border:1px solid #000000;padding:0px; }
.sub { width:100%;border:none; }
.cObj { width:90%;border:1px solid #000000;margin-left:auto;margin-right:auto;text-align:left; }
pre { margin-left:30%; }
.cObj th, .main th { font-size:28px; }
.sub th { font-size:16px;padding:5px; }
.sub td { padding:10px; }
</style>
</head>
<body>
<?php
$tests = array(
'',
'$gd->resizeImage(150,150);',
'$gd->maxSizeThumbnail(150);',
'$gd->oneSizeThumbnail(150);',
'$gd->cropImage(200,200,50,50);',
'$gd->imageRotate(90);',
'$gd->mirror();'
);

if($gd->useGdFilters)
{
$tests[] = '$gd->effectBrightness(50);';
$tests[] = '$gd->effectSmooth(30);';
$tests[] = '$gd->effectEmboss();';
$tests[] = '$gd->effectGaussianBlur();';
$tests[] = '$gd->effectMeanRemoval();';
$tests[] = '$gd->effectNegate();';
$tests[] = '$gd->effectGrayscale();';
$tests[] = '$gd->effectEdgeDetect();';
$tests[] = '$gd->effectSelectiveBlur();';
$tests[] = '$gd->effectContrast(-30);';
$tests[] = '$gd->effectColorize(\'#00ff00\');';
}

$tests[] = 'Error simulation';

$nb = count($tests);

echo '<table class="main">
<tr>
<th>Gd2 class demo samples</th>
</tr>
<tr>
<td>
<br />
<img src="DSC_0096.jpg" width="'.$gd->imgInfos['width'].'" height="'.$gd->imgInfos['height'].'" />
<br />
( Original Image )
</td>
</tr>
<tr>
<td>
<table class="sub">';

for($i=1;$i<$nb;$i+=2)
{
echo'<tr>
<th>'.$tests[$i].'</th><th>'.(isset($tests[$i+1]) ? $tests[$i+1] : '').'</th>
</tr>
<tr>
<td><img src="?op='.$i.'" /></td><td>'.(isset($tests[$i+1]) ? '<img src="?op='.($i+1).'" />' : '').'</td>
</tr>';
}


echo '</table>
</td>
</tr>
</table>
<table class="cObj">
<tr>
<th>Gd2 Class Object</th>
</tr>
<tr>
<td>
<br />
<pre>'.print_r($gd,true).'</pre>
</td>
</tr>
</table>
<br />
</body>
</html>';

$gd->destroy();
}
?>

  7. Re: Your class   Reply   Report abuse  
Picture of kankrelune kankrelune - 2006-10-04 16:11:59 - In reply to message 6 from kankrelune
Muarf... the code indent have been deleted... .. . :oS

@ tchaOo°