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);
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
283 # return the image type
284 function Type($FileName = NULL)
287 if (preg_match(
"/.*\\.jp[e]{0,1}g$/i", $FileName)) {
return IMGTYPE_JPEG; }
288 elseif (preg_match(
"/.*\\.gif$/i", $FileName)) {
return IMGTYPE_GIF; }
289 elseif (preg_match(
"/.*\\.bmp$/i", $FileName)) {
return IMGTYPE_BMP; }
290 elseif (preg_match(
"/.*\\.png$/i", $FileName)) {
return IMGTYPE_PNG; }
300 switch ($this->
Type())
302 # if the image type is known
308 # the image type isn't known
314 if (function_exists(
"finfo_open"))
316 # construct a handle to get mimetype info
317 $FInfoHandle = finfo_open(FILEINFO_MIME);
319 # if the handle is okay
322 # get the mimetype info for the file
323 $FInfoMime = finfo_file($FInfoHandle, $FilePath);
326 finfo_close($FInfoHandle);
328 # if the mimetype info fetch was successful
331 $Mimetype = $FInfoMime;
337 else if (function_exists(
"mime_content_type"))
339 # mime_content_type has been deprecated, but it may be
340 # the only way to get the mimetype for PHP < 5.3
341 $MimeType = mime_content_type($FilePath);
343 # if the mimetype info fetch was successful
346 $Mimetype = $MimeType;
354 # return the file name extension for the image
359 return Image::$AxisImageFileExtensions[$this->
Type()];
363 if (isset(Image::$AxisImageFileExtensions[$Type]))
365 return Image::$AxisImageFileExtensions[$Type];
374 # set/get the quality (0-100) for JPEG images created with SaveAs()
377 if ($NewSetting != NULL) { $this->JpegSaveQuality = $NewSetting; }
381 # return supported image formats
384 # start out assuming no formats are supported
387 # if JPEG is supported by PHP
388 if (function_exists(
"imagetypes") && defined(
"IMG_JPG")
389 && (imagetypes() & IMG_JPG))
391 # add JPEG to list of supported formats
395 # if GIF is supported by PHP
396 if (function_exists(
"imagetypes") && defined(
"IMG_GIF")
397 && (imagetypes() & IMG_GIF))
399 # add GIF to list of supported formats
403 # if PNG is supported by PHP
404 if (function_exists(
"imagetypes") && defined(
"IMG_PNG")
405 && (imagetypes() & IMG_PNG))
407 # add PNG to list of supported formats
411 # report to caller what formats are supported
415 # return names (upper-case extensions) of supported image formats
418 # assume that no formats are supported
419 $FormatNames = array();
421 # retrieve supported formats
424 # for each possible supported format
425 foreach (Image::$AxisImageFileExtensions as $ImageType => $ImageExtension)
427 # if format is supported
428 if ($ImageType & $SupportedFormats)
430 # add format extension to list of supported image format names
431 $FormatNames[] = strtoupper($ImageExtension);
435 # return supported image format names to caller
439 # return the error status set by the constructor or the last call to SaveAs()
445 # return string containing external command that failed
452 # ---- PRIVATE INTERFACE -------------------------------------------------
472 # image file extensions
473 private static $AxisImageFileExtensions = array(
482 # if we do not already have image info
483 if (!isset($this->ImageXSize))
485 # read size information from image object
486 $this->ImageXSize = imagesx($this->ImageObj);
487 $this->ImageYSize = imagesy($this->ImageObj);
493 if ($Format == NULL) { $Format = $this->
Type(); }
495 if (!function_exists(
"imagetypes")) {
return FALSE; }
500 return (imagetypes() & IMG_JPG) ? TRUE : FALSE;
504 return (imagetypes() & IMG_GIF) ? TRUE : FALSE;
512 return (imagetypes() & IMG_PNG) ? TRUE : FALSE;
522 # image type definitions (these are purposefully different from those defined by PHP GD lib)
523 define(
"IMGTYPE_UNKNOWN", 0);
524 define(
"IMGTYPE_JPEG", 1);
525 define(
"IMGTYPE_GIF", 2);
526 define(
"IMGTYPE_BMP", 4);
527 define(
"IMGTYPE_PNG", 8);
529 # error status definitions
530 define(
"AI_OKAY", 0);
531 define(
"AI_FILEUNREADABLE", 1);
532 define(
"AI_IMGOBJCREATEFAILED", 2);
533 define(
"AI_PPMCMDFAILED", 4);
534 define(
"AI_INTERNALERROR", 8);
535 define(
"AI_UNKNOWNTYPE", 16);
536 define(
"AI_UNSUPPORTEDFORMAT", 32);
537 define(
"AI_DESTINATIONUNWRITABLE", 64);
539 # supply imagetypes() function if not defined
540 if (!function_exists(
"imagetypes"))
542 # (returning 0 indicates no image types supported)
543 function imagetypes() {
return 0; }
static SupportedFormatNames()
SaveAs($FileName, $NewImageType=NULL)
ScaleTo($ScaledXSize, $ScaledYSize, $MaintainAspectRatio=FALSE)
CropTo($CroppedXSize, $CroppedYSize, $CroppedXOrigin=0, $CroppedYOrigin=0)
const AI_IMGOBJCREATEFAILED
Image($SourceFileName, $DebugLevel=0)
JpegQuality($NewSetting=NULL)
ImageFormatSupportedByPhp($Format=NULL)
Mimetype()
Get the MIME type for the image.
const AI_DESTINATIONUNWRITABLE
static SupportedFormats()
const AI_UNSUPPORTEDFORMAT
static Extension($Type=NULL)