📄 class.image.php
字号:
<?php
/**
* this class provide functions to edit an image, e.g. resize, rotate, flip, crop
* @author Logan Cai cailongqun [at] yahoo [dot] com [dot] cn
* @link www.phpletter.com
* @version 0.9
* @since 14/May/2007
* @name Image
*
*/
class Image
{
var $_debug = false;
var $_errors = array();
var $gdInfo = array(); //keep all information of GD extension
var $_imgOrig = null; //the hanlder of original image
var $_imgFinal = null; //the handler of final image
var $imageFile = null;
var $transparentColorRed = null;
var $transparentColorGreen = null;
var $transparentColorBlue = null;
var $chmod = 0755;
var $_imgInfoOrig = array(
'name'=>'',
'ext'=>'',
'size'=>'',
'width'=>'',
'height'=>'',
'type'=>'',
'path'=>'',
);
var $_imgInfoFinal = array(
'name'=>'',
'ext'=>'',
'size'=>'',
'width'=>'',
'height'=>'',
'type'=>'',
'path'=>'',
);
var $_imgQuality = 90;
/**
* constructor
*
* @param boolean $debug
* @return Image
*/
function __construct($debug = false)
{
$this->enableDebug($debug);
$this->gdInfo = $this->getGDInfo();
}
function Image($debug = false)
{
$this->__construct($debug);
}
/**
* enable to debug
*
* @param boolean $value
*/
function enableDebug($value)
{
$this->_debug = ($value?true:false);
}
/**
* check if debug enable
* @return boolean
*/
function _isDebugEnable()
{
return $this->_debug;
}
/**
* append to errors array and shown the each error when the debug turned on
*
* @param string $string
* @return void
* @access private
* @copyright this function originally come from Andy's php
*/
function _debug($value)
{
$this->_errors[] = $value;
if ($this->_debug)
{
echo $value . "<br />\n";
}
}
/**
* show erros
*
*/
function showErrors()
{
if(sizeof($this->_errors))
{
foreach($this->_errors as $error)
{
echo $error . "<br />\n";
}
}
}
/**
* Load an image from the file system.
*
* @param string $filename
* @return bool
* @access public
* @copyright this function originally come from Andy's php
*/
function loadImage($filename)
{
$ext = strtolower($this->_getExtension($filename));
$func = 'imagecreatefrom' . ($ext == 'jpg' ? 'jpeg' : $ext);
if (!$this->_isSupported($filename, $ext, $func, false)) {
return false;
}
if($ext == "gif")
{
// the following part gets the transparency color for a gif file
// this code is from the PHP manual and is written by
// fred at webblake dot net and webmaster at webnetwizard dotco dotuk, thanks!
$fp = @fopen($filename, "rb");
$result = @fread($fp, 13);
$colorFlag = ord(substr($result,10,1)) >> 7;
$background = ord(substr($result,11));
if ($colorFlag) {
$tableSizeNeeded = ($background + 1) * 3;
$result = @fread($fp, $tableSizeNeeded);
$this->transparentColorRed = ord(substr($result, $background * 3, 1));
$this->transparentColorGreen = ord(substr($result, $background * 3 + 1, 1));
$this->transparentColorBlue = ord(substr($result, $background * 3 + 2, 1));
}
fclose($fp);
// -- here ends the code related to transparency handling
}
$this->_imgOrig = @$func($filename);
if ($this->_imgOrig == null) {
$this->_debug("The image could not be created from the '$filename' file using the '$func' function.");
return false;
}else
{
$this->imageFile = $filename;
$this->_imgInfoOrig = array(
'name'=>basename($filename),
'ext'=>$ext,
'size'=>filesize($filename),
'path'=>$filename,
);
$imgInfo = $this->_getImageInfo($filename);
if(sizeof($imgInfo))
{
foreach($imgInfo as $k=>$v)
{
$this->_imgInfoOrig[$k] = $v;
$this->_imgInfoFinal[$k] = $v;
}
}
}
return true;
}
/**
* Load an image from a string (eg. from a database table)
*
* @param string $string
* @return bool
* @access public
* @copyright this function originally come from Andy's php
*/
function loadImageFromString($string)
{
$this->imageFile = $filename;
$this->_imgOrig = imagecreatefromstring($string);
if (!$this->_imgOrig) {
$this->_debug('The image (supplied as a string) could not be created.');
return false;
}
return true;
}
/**
* Save the modified image
*
* @param string $filename
* @param int $quality
* @param string $forcetype
* @return bool
* @access public
* @copyright this function originally come from Andy's php
*/
function saveImage($filename, $quality = 90, $forcetype = '')
{
if ($this->_imgFinal == null) {
$this->_debug('No changes intend to be made.');
return false;
}
$ext = ($forcetype == '') ? $this->_getExtension($filename) : strtolower($forcetype);
$func = 'image' . ($ext == 'jpg' ? 'jpeg' : $ext);
if (!$this->_isSupported($filename, $ext, $func, true))
{
return false;
}
$saved = false;
switch($ext)
{
case 'gif':
if ($this->gdInfo['Truecolor Support'] && imageistruecolor($this->_imgFinal))
{
imagetruecolortopalette($this->_imgFinal, false, 255);
}
case 'png':
$saved = $func($this->_imgFinal, $filename);
break;
case 'jpg':
$saved = $func($this->_imgFinal, $filename, $quality);
break;
}
if ($saved === false)
{
$this->_debug("The image could not be saved to the '$filename' file as the file type '$ext' using the '$func' function.");
return false;
}else
{
$this->_imgInfoFinal['size'] = @filesize($filename);
@chmod($filename, intval($this->chmod, 8));
}
return true;
}
/**
* Shows the masked image without any saving
*
* @param string $type
* @param int $quality
* @return bool
* @access public
* @copyright this function originally come from Andy's php
*/
function showImage($type = '', $quality = '')
{
if ($this->_imgFinal == null) {
$this->_debug('There is no cropped image to show.');
return false;
}
$type = (!empty($type)?$type:$this->_imgInfoOrig['ext']);
$quality = (!empty($quality)?$quality:$this->_imgQuality);
$type = strtolower($type);
$func = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
$head = 'image/' . ($type == 'jpg' ? 'jpeg' : $type);
if (!$this->_isSupported('[showing file]', $type, $func, false)) {
return false;
}
header("Content-type: $head");
switch($type)
{
case 'gif':
if ($this->gdInfo['Truecolor Support'] && imageistruecolor($this->_imgFinal))
{
@imagetruecolortopalette($this->_imgFinal, false, 255);
}
case 'png':
$func($this->_imgFinal);
break;
case 'jpg':
$func($this->_imgFinal, '', $quality);
break;
}
return true;
}
/**
* Used for cropping image
*
* @param int $dst_x
* @param int $dst_y
* @param int $dst_w
* @param int $dst_h
* @return bool
* @access public
* @copyright this function originally come from Andy's php
*/
function crop($dst_x, $dst_y, $dst_w, $dst_h)
{
if ($this->_imgOrig == null) {
$this->_debug('The original image has not been loaded.');
return false;
}
if (($dst_w <= 0) || ($dst_h <= 0)) {
$this->_debug('The image could not be cropped because the size given is not valid.');
return false;
}
if (($dst_w > imagesx($this->_imgOrig)) || ($dst_h > imagesy($this->_imgOrig))) {
$this->_debug('The image could not be cropped because the size given is larger than the original image.');
return false;
}
$this->_createFinalImageHandler($dst_w, $dst_h);
if ($this->gdInfo['Truecolor Support'])
{
if(!@imagecopyresampled($this->_imgFinal, $this->_imgOrig, 0, 0, $dst_x, $dst_y, $dst_w, $dst_h, $dst_w, $dst_h))
{
$this->_debug('Unable crop the image.');
return false;
}
} else
{
if(!@imagecopyresized($this->_imgFinal, $this->_imgOrig, 0, 0, $dst_x, $dst_y, $dst_w, $dst_h, $dst_w, $dst_h))
{
$this->_debug('Unable crop the image.');
return false;
}
}
$this->_imgInfoFinal['width'] = $dst_w;
$this->_imgInfoFinal['height'] = $dst_h;
return true;
}
/**
* Resize the Image in the X and/or Y direction
* If either is 0 it will be scaled proportionally
*
* @access public
*
* @param mixed $new_x
* @param mixed $new_y
* @param boolean $constraint keep to resize the image proportionally
* @param boolean $unchangeIfsmaller keep the orignial size if the orignial smaller than the new size
*
*
* @return mixed none or PEAR_error
*/
function resize( $new_x, $new_y, $constraint= false, $unchangeIfsmaller=false)
{
if(!$this->_imgOrig)
{
$this->_debug('No image fould.');
return false;
}
$new_x = intval($new_x);
$new_y = intval($new_y);
if($new_x <=0 || $new_y <= 0)
{
$this->_debug('either of new width or height can be zeor or less.');
}else
{
if($constraint)
{
if($new_x < 1 && $new_y < 1)
{
$new_x = $this->_imgInfoOrig['width'];
$new_y = $this->_imgInfoOrig['height'];
}elseif($new_x < 1)
{
$new_x = floor($new_y / $this->_imgInfoOrig['height'] * $this->_imgInfoOrig['width']);
}elseif($new_y < 1)
{
$new_y = floor($new_x / $this->_imgInfoOrig['width'] * $this->_imgInfoOrig['height']);
}else
{
$scale = min($new_x/$this->_imgInfoOrig['width'], $new_y/$this->_imgInfoOrig['height']) ;
$new_x = floor($scale*$this->_imgInfoOrig['width']);
$new_y = floor($scale*$this->_imgInfoOrig['height']);
}
}
if($unchangeIfsmaller)
{
if($this->_imgInfoOrig['width'] < $new_x && $this->_imgInfoOrig['height'] < $new_y )
{
$new_x = $this->_imgInfoOrig['width'];
$new_y = $this->_imgInfoOrig['height'];
}
}
if(is_null($this->_imgOrig))
{
$this->loadImage($filePath);
}
if(sizeof($this->_errors) == 0)
{
return $this->_resize($new_x, $new_y);
}
}
return false;
} // End resize
/**
* resize the image and return the thumbnail image details array("width"=>, "height"=>, "name")
*
* @param string $fileName
* @param int $new_x the thumbnail width
* @param int $new_y the thumbnail height
* @param string $mode can be save, view and both
* @return unknown
*/
function _resize( $new_x, $new_y)
{
$this->_createFinalImageHandler($new_x, $new_y);
// hacks fot transparency of png24 files
if ($this->_imgInfoOrig['type'] == 'png')
{
@imagealphablending($this->_imgFinal, false);
if(function_exists('ImageCopyResampled'))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -