4 # A PHP Object to Support Image File Manipulation 6 # Copyright 1999-2013 Axis Data 7 # This code is free software that can be used or redistributed under the 8 # terms of Version 2 of the GNU General Public License, as published by the 9 # Free Software Foundation (http://www.fsf.org). 11 # Part of the AxisPHP library v1.2.5 12 # For more information see http://www.axisdata.com/AxisPHP/ 17 # ---- PUBLIC INTERFACE -------------------------------------------------- 24 # save source file name 28 $this->JpegSaveQuality = 80;
30 $this->FailedCommand =
"";
32 # get GD library version 33 if (extension_loaded(
"gd"))
35 if (in_array(
"imagecreatetruecolor", get_extension_funcs(
"gd")))
49 # if source file is readable 52 # if support is available for this image type 55 # create PHP image object 56 switch ($this->
Type())
59 if ($this->DebugLevel > 1) { print(
"AI: file format is JPEG<br>\n"); }
60 $this->ImageObj = imagecreatefromjpeg($this->SourceFileName);
64 if ($this->DebugLevel > 1) { print(
"AI: file format is GIF<br>\n"); }
65 $this->ImageObj = imagecreatefromgif($this->SourceFileName);
69 if ($this->DebugLevel > 1) { print(
"AI: file format is BMP<br>\n"); }
70 $this->ImageObj = imagecreatefrombmp($this->SourceFileName);
74 if ($this->DebugLevel > 1) { print(
"AI: file format is PNG<br>\n"); }
75 $this->ImageObj = imagecreatefrompng($this->SourceFileName);
83 # if PHP image object creation failed 84 if (FALSE === $this->ImageObj)
92 # set error status to indicate unsupported image format 103 # save image with a new name and (optionally) a new type 104 function SaveAs($FileName, $NewImageType = NULL)
106 # assume we will succeed 109 # if destination file exists and is not writable 110 if (file_exists($FileName) && (is_writable($FileName) != TRUE))
115 # else if destination directory is not writable 116 elseif (is_writable(dirname($FileName)) != TRUE)
123 # if no image type specified try to determine based on file name or use source file type 124 if ($NewImageType == NULL)
127 { $NewImageType = $this->
Type($FileName); }
129 { $NewImageType = $this->
Type(); }
132 # if input and output types both supported 135 # if image cropping or scaling was requested 136 if (isset($this->CroppedXSize)
137 || isset($this->ScaledXSize)
138 || isset($this->ScaledYSize))
140 # determine destination image size 141 if (isset($this->ScaledXSize) && isset($this->ScaledYSize)
142 && ($this->MaintainAspectRatio != TRUE))
147 elseif (isset($this->ScaledXSize)
148 || ($this->ScaledXSize > $this->ScaledYSize))
151 $DstYSize = ($this->ScaledXSize * $this->
YSize())
154 elseif (isset($this->ScaledYSize))
156 $DstXSize = ($this->ScaledYSize * $this->
XSize())
160 elseif (isset($this->CroppedXSize))
167 $DstXSize = $this->
XSize();
168 $DstYSize = $this->
YSize();
171 # create destination image object 172 if (($NewImageType ==
IMGTYPE_GIF) || ($this->GDVersion < 2))
174 $DstImage = imagecreate($DstXSize, $DstYSize);
178 $DstImage = imagecreatetruecolor($DstXSize, $DstYSize);
179 imagealphablending($DstImage, FALSE);
180 imagesavealpha($DstImage, TRUE);
183 # determine area of source image to use 184 if (isset($this->CroppedXSize))
191 $SrcXSize = $this->
XSize();
192 $SrcYSize = $this->
YSize();
195 # copy/scale portion of original image to destination image 196 if ($this->GDVersion >= 2)
198 imagecopyresampled($DstImage, $this->ImageObj,
200 $this->CroppedXOrigin, $this->CroppedYOrigin,
201 $DstXSize, $DstYSize,
202 $SrcXSize, $SrcYSize);
206 imagecopyresized($DstImage, $this->ImageObj,
208 $this->CroppedXOrigin, $this->CroppedYOrigin,
209 $DstXSize, $DstYSize,
210 $SrcXSize, $SrcYSize);
218 # save image to new file 219 switch ($NewImageType)
222 imagegif($DstImage, $FileName);
226 imagejpeg($DstImage, $FileName, $this->JpegSaveQuality);
230 imagepng($DstImage, $FileName, 9);
234 imagewbmp($DstImage, $FileName);
243 # set error status to indicate unsupported image format 248 # report success or failure to caller 252 # return the X (horizontal) image size in pixels 259 # return the Y (vertical) image size in pixels 266 # specify the size to scale the image to for the next SaveAs() 269 # save size for scaling 275 # specify the size to crop the image to for the next SaveAs() 278 # save origin and size for cropping 292 function Type($FileName = NULL)
295 if (is_readable($FileName))
297 switch (exif_imagetype($FileName))
305 if (preg_match(
"/.*\\.jp[e]{0,1}g$/i", $FileName))
307 elseif (preg_match(
"/.*\\.gif$/i", $FileName))
309 elseif (preg_match(
"/.*\\.bmp$/i", $FileName))
311 elseif (preg_match(
"/.*\\.png$/i", $FileName))
322 switch ($this->
Type())
324 # if the image type is known 330 # the image type isn't known 336 if (function_exists(
"finfo_open"))
338 # construct a handle to get mimetype info 339 $FInfoHandle = finfo_open(FILEINFO_MIME);
341 # if the handle is okay 344 # get the mimetype info for the file 345 $FInfoMime = finfo_file($FInfoHandle, $FilePath);
348 finfo_close($FInfoHandle);
350 # if the mimetype info fetch was successful 353 $Mimetype = $FInfoMime;
359 else if (function_exists(
"mime_content_type"))
361 # mime_content_type has been deprecated, but it may be 362 # the only way to get the mimetype for PHP < 5.3 363 $MimeType = mime_content_type($FilePath);
365 # if the mimetype info fetch was successful 368 $Mimetype = $MimeType;
376 # return the file name extension for the image 381 return Image::$AxisImageFileExtensions[$this->
Type()];
385 if (isset(Image::$AxisImageFileExtensions[$Type]))
387 return Image::$AxisImageFileExtensions[$Type];
396 # set/get the quality (0-100) for JPEG images created with SaveAs() 399 if ($NewSetting != NULL) { $this->JpegSaveQuality = $NewSetting; }
403 # return supported image formats 406 # start out assuming no formats are supported 409 # if JPEG is supported by PHP 410 if (function_exists(
"imagetypes") && defined(
"IMG_JPG")
411 && (imagetypes() & IMG_JPG))
413 # add JPEG to list of supported formats 417 # if GIF is supported by PHP 418 if (function_exists(
"imagetypes") && defined(
"IMG_GIF")
419 && (imagetypes() & IMG_GIF))
421 # add GIF to list of supported formats 425 # if PNG is supported by PHP 426 if (function_exists(
"imagetypes") && defined(
"IMG_PNG")
427 && (imagetypes() & IMG_PNG))
429 # add PNG to list of supported formats 433 # report to caller what formats are supported 437 # return names (upper-case extensions) of supported image formats 440 # assume that no formats are supported 441 $FormatNames = array();
443 # retrieve supported formats 446 # for each possible supported format 447 foreach (Image::$AxisImageFileExtensions as $ImageType => $ImageExtension)
449 # if format is supported 450 if ($ImageType & $SupportedFormats)
452 # add format extension to list of supported image format names 453 $FormatNames[] = strtoupper($ImageExtension);
457 # return supported image format names to caller 461 # return the error status set by the constructor or the last call to SaveAs() 467 # return string containing external command that failed 474 # ---- PRIVATE INTERFACE ------------------------------------------------- 494 # image file extensions 495 private static $AxisImageFileExtensions = array(
504 # if we do not already have image info 505 if (!isset($this->ImageXSize))
507 # read size information from image object 508 $this->ImageXSize = imagesx($this->ImageObj);
509 $this->ImageYSize = imagesy($this->ImageObj);
515 if ($Format == NULL) { $Format = $this->
Type(); }
517 if (!function_exists(
"imagetypes")) {
return FALSE; }
522 return (imagetypes() & IMG_JPG) ? TRUE : FALSE;
526 return (imagetypes() & IMG_GIF) ? TRUE : FALSE;
534 return (imagetypes() & IMG_PNG) ? TRUE : FALSE;
544 # image type definitions (these are purposefully different from those defined by PHP GD lib) 545 define(
"IMGTYPE_UNKNOWN", 0);
546 define(
"IMGTYPE_JPEG", 1);
547 define(
"IMGTYPE_GIF", 2);
548 define(
"IMGTYPE_BMP", 4);
549 define(
"IMGTYPE_PNG", 8);
551 # error status definitions 552 define(
"AI_OKAY", 0);
553 define(
"AI_FILEUNREADABLE", 1);
554 define(
"AI_IMGOBJCREATEFAILED", 2);
555 define(
"AI_PPMCMDFAILED", 4);
556 define(
"AI_INTERNALERROR", 8);
557 define(
"AI_UNKNOWNTYPE", 16);
558 define(
"AI_UNSUPPORTEDFORMAT", 32);
559 define(
"AI_DESTINATIONUNWRITABLE", 64);
561 # supply imagetypes() function if not defined 562 if (!function_exists(
"imagetypes"))
564 # (returning 0 indicates no image types supported) 565 function imagetypes() {
return 0; }
static SupportedFormatNames()
SaveAs($FileName, $NewImageType=NULL)
ScaleTo($ScaledXSize, $ScaledYSize, $MaintainAspectRatio=FALSE)
CropTo($CroppedXSize, $CroppedYSize, $CroppedXOrigin=0, $CroppedYOrigin=0)
const AI_DESTINATIONUNWRITABLE
JpegQuality($NewSetting=NULL)
ImageFormatSupportedByPhp($Format=NULL)
const AI_IMGOBJCREATEFAILED
Mimetype()
Get the MIME type for the image.
Type($FileName=NULL)
Get the image type.
__construct($SourceFileName, $DebugLevel=0)
static SupportedFormats()
static Extension($Type=NULL)
const AI_UNSUPPORTEDFORMAT