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);
181 # determine area of source image to use 182 if (isset($this->CroppedXSize))
189 $SrcXSize = $this->
XSize();
190 $SrcYSize = $this->
YSize();
193 # copy/scale portion of original image to destination image 194 if ($this->GDVersion >= 2)
196 imagecopyresampled($DstImage, $this->ImageObj,
198 $this->CroppedXOrigin, $this->CroppedYOrigin,
199 $DstXSize, $DstYSize,
200 $SrcXSize, $SrcYSize);
204 imagecopyresized($DstImage, $this->ImageObj,
206 $this->CroppedXOrigin, $this->CroppedYOrigin,
207 $DstXSize, $DstYSize,
208 $SrcXSize, $SrcYSize);
216 # save image to new file 217 switch ($NewImageType)
220 imagegif($DstImage, $FileName);
224 imagejpeg($DstImage, $FileName, $this->JpegSaveQuality);
228 imagepng($DstImage, $FileName, 9);
232 imagewbmp($DstImage, $FileName);
241 # set error status to indicate unsupported image format 246 # report success or failure to caller 250 # return the X (horizontal) image size in pixels 257 # return the Y (vertical) image size in pixels 264 # specify the size to scale the image to for the next SaveAs() 267 # save size for scaling 273 # specify the size to crop the image to for the next SaveAs() 276 # save origin and size for cropping 290 function Type($FileName = NULL)
293 if (is_readable($FileName))
295 switch (exif_imagetype($FileName))
303 if (preg_match(
"/.*\\.jp[e]{0,1}g$/i", $FileName))
305 elseif (preg_match(
"/.*\\.gif$/i", $FileName))
307 elseif (preg_match(
"/.*\\.bmp$/i", $FileName))
309 elseif (preg_match(
"/.*\\.png$/i", $FileName))
320 switch ($this->
Type())
322 # if the image type is known 328 # the image type isn't known 334 if (function_exists(
"finfo_open"))
336 # construct a handle to get mimetype info 337 $FInfoHandle = finfo_open(FILEINFO_MIME);
339 # if the handle is okay 342 # get the mimetype info for the file 343 $FInfoMime = finfo_file($FInfoHandle, $FilePath);
346 finfo_close($FInfoHandle);
348 # if the mimetype info fetch was successful 351 $Mimetype = $FInfoMime;
357 else if (function_exists(
"mime_content_type"))
359 # mime_content_type has been deprecated, but it may be 360 # the only way to get the mimetype for PHP < 5.3 361 $MimeType = mime_content_type($FilePath);
363 # if the mimetype info fetch was successful 366 $Mimetype = $MimeType;
374 # return the file name extension for the image 379 return Image::$AxisImageFileExtensions[$this->
Type()];
383 if (isset(Image::$AxisImageFileExtensions[$Type]))
385 return Image::$AxisImageFileExtensions[$Type];
394 # set/get the quality (0-100) for JPEG images created with SaveAs() 397 if ($NewSetting != NULL) { $this->JpegSaveQuality = $NewSetting; }
401 # return supported image formats 404 # start out assuming no formats are supported 407 # if JPEG is supported by PHP 408 if (function_exists(
"imagetypes") && defined(
"IMG_JPG")
409 && (imagetypes() & IMG_JPG))
411 # add JPEG to list of supported formats 415 # if GIF is supported by PHP 416 if (function_exists(
"imagetypes") && defined(
"IMG_GIF")
417 && (imagetypes() & IMG_GIF))
419 # add GIF to list of supported formats 423 # if PNG is supported by PHP 424 if (function_exists(
"imagetypes") && defined(
"IMG_PNG")
425 && (imagetypes() & IMG_PNG))
427 # add PNG to list of supported formats 431 # report to caller what formats are supported 435 # return names (upper-case extensions) of supported image formats 438 # assume that no formats are supported 439 $FormatNames = array();
441 # retrieve supported formats 444 # for each possible supported format 445 foreach (Image::$AxisImageFileExtensions as $ImageType => $ImageExtension)
447 # if format is supported 448 if ($ImageType & $SupportedFormats)
450 # add format extension to list of supported image format names 451 $FormatNames[] = strtoupper($ImageExtension);
455 # return supported image format names to caller 459 # return the error status set by the constructor or the last call to SaveAs() 465 # return string containing external command that failed 472 # ---- PRIVATE INTERFACE ------------------------------------------------- 492 # image file extensions 493 private static $AxisImageFileExtensions = array(
502 # if we do not already have image info 503 if (!isset($this->ImageXSize))
505 # read size information from image object 506 $this->ImageXSize = imagesx($this->ImageObj);
507 $this->ImageYSize = imagesy($this->ImageObj);
513 if ($Format == NULL) { $Format = $this->
Type(); }
515 if (!function_exists(
"imagetypes")) {
return FALSE; }
520 return (imagetypes() & IMG_JPG) ? TRUE : FALSE;
524 return (imagetypes() & IMG_GIF) ? TRUE : FALSE;
532 return (imagetypes() & IMG_PNG) ? TRUE : FALSE;
542 # image type definitions (these are purposefully different from those defined by PHP GD lib) 543 define(
"IMGTYPE_UNKNOWN", 0);
544 define(
"IMGTYPE_JPEG", 1);
545 define(
"IMGTYPE_GIF", 2);
546 define(
"IMGTYPE_BMP", 4);
547 define(
"IMGTYPE_PNG", 8);
549 # error status definitions 550 define(
"AI_OKAY", 0);
551 define(
"AI_FILEUNREADABLE", 1);
552 define(
"AI_IMGOBJCREATEFAILED", 2);
553 define(
"AI_PPMCMDFAILED", 4);
554 define(
"AI_INTERNALERROR", 8);
555 define(
"AI_UNKNOWNTYPE", 16);
556 define(
"AI_UNSUPPORTEDFORMAT", 32);
557 define(
"AI_DESTINATIONUNWRITABLE", 64);
559 # supply imagetypes() function if not defined 560 if (!function_exists(
"imagetypes"))
562 # (returning 0 indicates no image types supported) 563 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