.

ImageToolsGD Class Reference
[ImageTools]

Wrapper around common image operations based upon GD. More...

Inheritance diagram for ImageToolsGD:
IImageTools

List of all members.

Public Member Functions

  create_from_binary_data ($data)
  Create an IImageInformation from a file.
  create_from_file ($file)
  Create an IImageInformation from a file.
  crop (IImageInformation $src, $x, $y, $width, $height)
  Cuts portion of image.
  fit (IImageInformation $src, $width, $height, $backgroundcolor=0xFFFFFF)
  Fit image in given height and width.
  resize (IImageInformation $src, $width, $height)
  Resize given image.
  resize_fit (IImageInformation $src, $width, $height)
  Resize given image.
  rotate (IImageInformation $src, $degrees, $backgroundcolor=0xFFFFFF)
  Rotate image.
  watermark (IImageInformation $src, $text=false)
  Add a Watermark.

Detailed Description

Wrapper around common image operations based upon GD.

Author:
Gerd Riesselmann

Definition at line 8 of file imagetools.gd.cls.php.


Member Function Documentation

ImageToolsGD::create_from_binary_data ( data  ) 

Create an IImageInformation from a file.

Returns:
IImageInformation

Implements IImageTools.

Definition at line 42 of file imagetools.gd.cls.php.

00042                                                        {
00043                 $ret = false;
00044                 $tmp = tempnam(Config::get_value(Config::TEMP_DIR), 'imgi');
00045                 if ($tmp) {
00046                         if (file_put_contents($tmp, $data) !== false) {
00047                                 $ret = $this->create_from_file($tmp);
00048                         }
00049                 }
00050                 unlink($tmp);
00051                 return $ret;
00052         }
ImageToolsGD::create_from_file ( file  ) 

Create an IImageInformation from a file.

Returns:
IImageInformation

Implements IImageTools.

Definition at line 14 of file imagetools.gd.cls.php.

00014                                                 {
00015                 $ret = false;
00016                 $imageinfo = getimagesize($file);
00017                 if ($imageinfo !== false) {
00018                         $handle = false;
00019                         switch($imageinfo[2]) {
00020                                 case IMAGETYPE_JPEG:
00021                                         $handle = imagecreatefromjpeg($file);
00022                                         break;
00023                                 case IMAGETYPE_PNG:
00024                                         $handle = imagecreatefrompng($file);
00025                                         break;
00026                                 case IMAGETYPE_GIF:
00027                                         $handle = imagecreatefromgif($file);
00028                                         break;
00029                         }
00030                         if ($handle) {
00031                                 $ret = new ImageInformationGD($handle, $imageinfo[2]);
00032                         }
00033                 } 
00034                 return $ret;
00035         }
ImageToolsGD::crop ( IImageInformation src,
x,
y,
width,
height  
)

Cuts portion of image.

Returns:
IImageInformation

Implements IImageTools.

Definition at line 85 of file imagetools.gd.cls.php.

00085                                                                               {
00086                 $handle = imagecreatetruecolor($width, $height);
00087                 imagecopy($handle, $src->handle, 0, 0, $x, $y, $width, $height);
00088                 return new ImageInformationGD($handle, $src->type);
00089         }
ImageToolsGD::fit ( IImageInformation src,
width,
height,
backgroundcolor = 0xFFFFFF  
)

Fit image in given height and width.

If image is larger than given size, it will be downsampled. If it is smaller, it will be untouched. Background is filled with $backgroundcolor

Returns:
IImageInformation False on failure

Implements IImageTools.

Definition at line 98 of file imagetools.gd.cls.php.

00098                                                                                                   {
00099                 $handle = imagecreatetruecolor($width, $height);
00100                 imagefill($handle, 0, 0, $backgroundcolor);
00101                 
00102                 Load::components('imagetoolscalculator');
00103                 $rect = ImageToolsCalculator::fit($src->get_width(), $src->get_height(), $width, $height);
00104                 $rect = ImageToolsCalculator::center($rect->width, $rect->height, $width, $height);
00105                 
00106                 imagecopyresampled($handle, $src->handle, $rect->x, $rect->y, 0, 0, $rect->width, $rect->height, $src->get_width(), $src->get_height());
00107                 return new ImageInformationGD($handle, $src->type);
00108         }       
ImageToolsGD::resize ( IImageInformation src,
width,
height  
)

Resize given image.

Returns:
IImageInformation

Implements IImageTools.

Definition at line 59 of file imagetools.gd.cls.php.

00059                                                                         {
00060                 $handle = imagecreatetruecolor($width, $height);
00061                 imagecopyresampled($handle, $src->handle, 0, 0, 0, 0, $width, $height, $src->get_width(), $src->get_height());
00062                 return new ImageInformationGD($handle, $src->type);             
00063         }
ImageToolsGD::resize_fit ( IImageInformation src,
width,
height  
)

Resize given image.

Image is not streched, so ratio is not changed.

Resizing an image of 200 x 100 to 100 x 100 will result in a image of size 100 x 50

Returns:
IImageInformation False on failure

Implements IImageTools.

Definition at line 72 of file imagetools.gd.cls.php.

00072                                                                             {
00073                 Load::components('imagetoolscalculator');
00074                 $rect = ImageToolsCalculator::fit($src->get_width(), $src->get_height(), $width, $height);
00075                 $handle = imagecreatetruecolor($rect->width, $rect->height);
00076                 imagecopyresampled($handle, $src->handle, 0, 0, 0, 0, $rect->width, $rect->height, $src->get_width(), $src->get_height());
00077                 return new ImageInformationGD($handle, $src->type);
00078         }       
ImageToolsGD::rotate ( IImageInformation src,
degrees,
backgroundcolor = 0xFFFFFF  
)

Rotate image.

Parameters:
IImageInformation  $src Image to rotate
float  $degrees Degrees to rotate (0-360)
int  $backgroundcolor Backgroundcolor of new portions
Returns:
IImageInformation False on failure

Implements IImageTools.

Definition at line 148 of file imagetools.gd.cls.php.

00148                                                                                               {
00149                 $rotated = imagerotate($src->handle, $degrees, $backgroundcolor);
00150                 return new ImageInformationGD($rotated, $src->type);
00151         }
ImageToolsGD::watermark ( IImageInformation src,
text = false  
)

Add a Watermark.

Parameters:
IImageInformation  $src Image to add watermark to
string  $text Text of Watermark, if emtpy "© {Application Title}" is taken
Returns:
IImageInformation

Implements IImageTools.

Definition at line 118 of file imagetools.gd.cls.php.

00118                                                                          {
00119                 if (empty($text)) {
00120                         $text = '© ' . Config::get_value(Config::TITLE);
00121                 }
00122                 $w = $src->get_width();
00123                 $h = $src->get_height();
00124                 $size = 40;
00125                 $font = Load::get_module_dir('lib.imagetools'). '3rdparty/arial.ttf';
00126                 $watermark = imagecreatetruecolor($w, $h);
00127                 imagecopy($watermark, $src->handle, 0, 0, 0, 0, $w, $h);
00128                 $color = imagecolorallocatealpha($watermark, 0xFF, 0xFF, 0xFF, 0x70);
00129                 if ($w >= $h) {
00130                         // Landscape
00131                         imagettftext($watermark, $size, 0, 3, $h - $sie - 3, $color, $font, $text);
00132                 }
00133                 else {
00134                         // Portrait
00135                         imagettftext($watermark, $size, 90, $w - $size - 3, $h - 3, $color, $font, $text);
00136                 }               
00137                 return new ImageInformationGD($watermark, $src->type);  
00138         }

The documentation for this class was generated from the following file: