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 -------------------------------------------------- 19 # image type definitions 20 # (these are purposefully different from those defined by PHP GD lib) 32 # save source file name 36 $this->JpegSaveQuality = 80;
38 $this->FailedCommand =
"";
40 # get GD library version 41 if (extension_loaded(
"gd"))
43 if (in_array(
"imagecreatetruecolor", get_extension_funcs(
"gd")))
57 # if source file is readable 60 # if support is available for this image type 63 # create PHP image object 64 switch ($this->
Type())
66 case self::IMGTYPE_JPEG:
67 if ($this->DebugLevel > 1) { print(
"AI: file format is JPEG<br>\n"); }
68 $this->ImageObj = imagecreatefromjpeg($this->SourceFileName);
71 case self::IMGTYPE_GIF:
72 if ($this->DebugLevel > 1) { print(
"AI: file format is GIF<br>\n"); }
73 $this->ImageObj = imagecreatefromgif($this->SourceFileName);
76 case self::IMGTYPE_BMP:
77 if ($this->DebugLevel > 1) { print(
"AI: file format is BMP<br>\n"); }
78 $this->ImageObj = imagecreatefrombmp($this->SourceFileName);
81 case self::IMGTYPE_PNG:
82 if ($this->DebugLevel > 1) { print(
"AI: file format is PNG<br>\n"); }
83 $this->ImageObj = imagecreatefrompng($this->SourceFileName);
91 # if PHP image object creation failed 92 if (FALSE === $this->ImageObj)
100 # set error status to indicate unsupported image format 111 # save image with a new name and (optionally) a new type 112 function SaveAs($FileName, $NewImageType = NULL)
114 # assume we will succeed 117 # if destination file exists and is not writable 118 if (file_exists($FileName) && (is_writable($FileName) != TRUE))
123 # else if destination directory is not writable 124 elseif (is_writable(dirname($FileName)) != TRUE)
131 # if no image type specified try to determine based on file name or use source file type 132 if ($NewImageType == NULL)
134 if ($this->
Type($FileName) != self::IMGTYPE_UNKNOWN)
135 { $NewImageType = $this->
Type($FileName); }
137 { $NewImageType = $this->
Type(); }
140 # if input and output types both supported 143 # if image cropping or scaling was requested 144 if (isset($this->CroppedXSize)
145 || isset($this->ScaledXSize)
146 || isset($this->ScaledYSize))
148 # determine destination image size 149 if (isset($this->ScaledXSize) && isset($this->ScaledYSize)
150 && ($this->MaintainAspectRatio != TRUE))
155 elseif (isset($this->ScaledXSize)
156 || ($this->ScaledXSize > $this->ScaledYSize))
159 $DstYSize = ($this->ScaledXSize * $this->
YSize())
162 elseif (isset($this->ScaledYSize))
164 $DstXSize = ($this->ScaledYSize * $this->
XSize())
168 elseif (isset($this->CroppedXSize))
175 $DstXSize = $this->
XSize();
176 $DstYSize = $this->
YSize();
179 # create destination image object 180 if (($NewImageType == self::IMGTYPE_GIF) || ($this->GDVersion < 2))
182 $DstImage = imagecreate($DstXSize, $DstYSize);
186 $DstImage = imagecreatetruecolor($DstXSize, $DstYSize);
187 imagealphablending($DstImage, FALSE);
188 imagesavealpha($DstImage, TRUE);
191 # determine area of source image to use 192 if (isset($this->CroppedXSize))
199 $SrcXSize = $this->
XSize();
200 $SrcYSize = $this->
YSize();
203 # copy/scale portion of original image to destination image 204 if ($this->GDVersion >= 2)
206 imagecopyresampled($DstImage, $this->ImageObj,
208 $this->CroppedXOrigin, $this->CroppedYOrigin,
209 $DstXSize, $DstYSize,
210 $SrcXSize, $SrcYSize);
214 imagecopyresized($DstImage, $this->ImageObj,
216 $this->CroppedXOrigin, $this->CroppedYOrigin,
217 $DstXSize, $DstYSize,
218 $SrcXSize, $SrcYSize);
226 # save image to new file 227 switch ($NewImageType)
229 case self::IMGTYPE_GIF:
230 imagegif($DstImage, $FileName);
233 case self::IMGTYPE_JPEG:
234 imagejpeg($DstImage, $FileName, $this->JpegSaveQuality);
237 case self::IMGTYPE_PNG:
238 imagepng($DstImage, $FileName, 9);
241 case self::IMGTYPE_BMP:
242 imagebmp($DstImage, $FileName);
252 # set error status to indicate unsupported image format 257 # report success or failure to caller 261 # return the X (horizontal) image size in pixels 268 # return the Y (vertical) image size in pixels 275 # specify the size to scale the image to for the next SaveAs() 278 # save size for scaling 284 # specify the size to crop the image to for the next SaveAs() 287 # save origin and size for cropping 301 function Type($FileName = NULL)
304 if (is_readable($FileName))
306 switch (exif_imagetype($FileName))
308 case IMAGETYPE_GIF:
return self::IMGTYPE_GIF;
309 case IMAGETYPE_JPEG:
return self::IMGTYPE_JPEG;
310 case IMAGETYPE_PNG:
return self::IMGTYPE_PNG;
311 case IMAGETYPE_BMP:
return self::IMGTYPE_BMP;
314 if (preg_match(
"/.*\\.jp[e]{0,1}g$/i", $FileName))
315 {
return self::IMGTYPE_JPEG; }
316 elseif (preg_match(
"/.*\\.gif$/i", $FileName))
317 {
return self::IMGTYPE_GIF; }
318 elseif (preg_match(
"/.*\\.bmp$/i", $FileName))
319 {
return self::IMGTYPE_BMP; }
320 elseif (preg_match(
"/.*\\.png$/i", $FileName))
321 {
return self::IMGTYPE_PNG; }
322 return self::IMGTYPE_UNKNOWN;
333 self::IMGTYPE_JPEG =>
"image/jpeg",
334 self::IMGTYPE_PNG =>
"image/png",
335 self::IMGTYPE_GIF =>
"image/gif",
336 self::IMGTYPE_BMP =>
"image/bmp",
339 if (isset($MimeTypes[$this->
Type()]))
341 return $MimeTypes[$this->
Type()];
347 if (strlen(trim($FilePath)))
349 if (function_exists(
"mime_content_type"))
351 $MimeType = mime_content_type($FilePath);
353 elseif (function_exists(
"finfo_open"))
355 $FInfoHandle = finfo_open(FILEINFO_MIME);
358 $Mimetype = finfo_file($FInfoHandle, $FilePath);
359 finfo_close($FInfoHandle);
367 # return the file name extension for the image 370 return Image::$AxisImageFileExtensions[$this->
Type()];
381 if (isset(Image::$AxisImageFileExtensions[$Type]))
383 return Image::$AxisImageFileExtensions[$Type];
391 # set/get the quality (0-100) for JPEG images created with SaveAs() 394 if ($NewSetting != NULL) { $this->JpegSaveQuality = $NewSetting; }
398 # return supported image formats 401 # start out assuming no formats are supported 404 # if JPEG is supported by PHP 405 if (defined(
"IMG_JPG") && (imagetypes() & IMG_JPG))
407 # add JPEG to list of supported formats 408 $Supported |= self::IMGTYPE_JPEG;
411 # if GIF is supported by PHP 412 if (defined(
"IMG_GIF") && (imagetypes() & IMG_GIF))
414 # add GIF to list of supported formats 415 $Supported |= self::IMGTYPE_GIF;
418 # if PNG is supported by PHP 419 if (defined(
"IMG_PNG") && (imagetypes() & IMG_PNG))
421 # add PNG to list of supported formats 422 $Supported |= self::IMGTYPE_PNG;
425 # if BMP is supported by PHP 426 if (defined(
"IMG_BMP") && (imagetypes() & IMG_BMP))
428 # add BMP to list of supported formats 429 $Supported |= self::IMGTYPE_BMP;
432 # report to caller what formats are supported 436 # return names (upper-case extensions) of supported image formats 439 # assume that no formats are supported 440 $FormatNames = array();
442 # retrieve supported formats 445 # for each possible supported format 446 foreach (Image::$AxisImageFileExtensions as $ImageType => $ImageExtension)
448 # if format is supported 449 if ($ImageType & $SupportedFormats)
451 # add format extension to list of supported image format names 452 $FormatNames[] = strtoupper($ImageExtension);
456 # return supported image format names to caller 460 # return the error status set by the constructor or the last call to SaveAs() 466 # return string containing external command that failed 473 # ---- PRIVATE INTERFACE ------------------------------------------------- 493 # image file extensions 494 private static $AxisImageFileExtensions = array(
495 self::IMGTYPE_JPEG =>
"jpg",
496 self::IMGTYPE_GIF =>
"gif",
497 self::IMGTYPE_BMP =>
"bmp",
498 self::IMGTYPE_PNG =>
"png",
503 # if we do not already have image info 504 if (!isset($this->ImageXSize))
506 # read size information from image object 507 $this->ImageXSize = imagesx($this->ImageObj);
508 $this->ImageYSize = imagesy($this->ImageObj);
514 if ($Format == NULL) { $Format = $this->
Type(); }
516 if (!function_exists(
"imagetypes")) {
return FALSE; }
520 case self::IMGTYPE_JPEG:
521 return (imagetypes() & IMG_JPG) ? TRUE : FALSE;
524 case self::IMGTYPE_GIF:
525 return (imagetypes() & IMG_GIF) ? TRUE : FALSE;
528 case self::IMGTYPE_BMP:
529 if (defined(
"IMG_BMP"))
531 return (imagetypes() & IMG_BMP) ? TRUE : FALSE;
539 case self::IMGTYPE_PNG:
540 return (imagetypes() & IMG_PNG) ? TRUE : FALSE;
550 # error status definitions 551 define(
"AI_OKAY", 0);
552 define(
"AI_FILEUNREADABLE", 1);
553 define(
"AI_IMGOBJCREATEFAILED", 2);
554 define(
"AI_PPMCMDFAILED", 4);
555 define(
"AI_INTERNALERROR", 8);
556 define(
"AI_UNKNOWNTYPE", 16);
557 define(
"AI_UNSUPPORTEDFORMAT", 32);
558 define(
"AI_DESTINATIONUNWRITABLE", 64);
560 # supply imagetypes() function if not defined 561 if (!function_exists(
"imagetypes"))
563 # (returning 0 indicates no image types supported) 564 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)
static ExtensionForType($Type)
return the file name extension for the image, given a type.
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()
const AI_UNSUPPORTEDFORMAT