圖片處理類imageclsphp
<?php
/**
圖片處理類
*/
class imagecls
{
/**
* 文件信息
*/
var $file = array();
/**
* 保存目錄
*/
var $dir = ;
/**
* 錯誤代碼
*/
var $error_code = ;
/**
* 文件上傳最大KB
*/
var $max_size = ;
function es_imagecls()
{
}
private function checkSize($size)
{
return !($size > $this>max_size) || ( == $this>max_size);
}
/**
* 處理上傳文件
* @param array $file 上傳的文件
* @param string $dir 保存的目錄
* @return bool
*/
function init($file $dir = temp)
{
if(!is_array($file) || empty($file)
|| !$this>isUploadFile($file[tmp_name]) || trim($file[name]) == || $file[size] == )
{
$this>file = array();
$this>error_code = ;
return false;
}
else
{
$file[size] = intval($file[size]);
$file[name] = trim($file[name]);
$file[thumb] = ;
$file[ext] = $this>fileExt($file[name]);
$file[name] = htmlspecialchars($file[name] ENT_QUOTES);
$file[is_image] = $this>isImageExt($file[ext]);
$file[file_dir] = $this>getTargetDir($dir);
$file[prefix] = md(microtime(true))rand();
$file[target] = "/public/"$file[file_dir]/$file[prefix]jpg; //相對
$file[local_target] = APP_ROOT_PATH"public/"$file[file_dir]/$file[prefix]jpg;//物理
$this>file = &$file;
$this>error_code = ;
return true;
}
}
/**
* 保存文件
* @return bool
*/
function save()
{
if(empty($this>file) || empty($this>file[tmp_name]))
$this>error_code = ;
elseif(!$this>checkSize($this>file[size]))
$this>error_code = ;
elseif(!$this>file[is_image])
$this>error_code = ;
elseif(!$this>saveFile($this>file[tmp_name] $this>file[local_target]))
$this>error_code = ;
elseif($this>file[is_image] &&
(!$this>file[image_info] = $this>getImageInfo($this>file[local_target] true)))
{
$this>error_code = ;
@unlink($this>file[local_target]);
}
else
{
$this>error_code = ;
return true;
}
return false;
}
/**
* 獲取錯誤代碼
* @return number
*/
function error()
{
return $this>error_code;
}
/**
* 獲取文件擴展名
* @return string
*/
function fileExt($file_name)
{
return addslashes(strtolower(substr(strrchr($file_name ) )));
}
/**
* 根據擴展名判斷文件是否為圖像
* @param string $ext 擴展名
* @return bool
*/
function isImageExt($ext)
{
static $img_ext = array(jpg jpeg png bmpgifgiff);
return in_array($ext $img_ext) ? : ;
}
/**
* 獲取圖像信息
* @param string $target 文件路徑
* @return mixed
*/
function getImageInfo($target)
{
$ext = es_imagecls::fileExt($target);
$is_image = es_imagecls::isImageExt($ext);
if(!$is_image)
return false;
elseif(!is_readable($target))
return false;
elseif($image_info = @getimagesize($target))
{
list($width $height $type) = !empty($image_info) ? $image_info :
array( );
$size = $width * $height;
if($is_image && !in_array($type array()))
return false;
$image_info[type] =
strtolower (substr(image_type_to_extension($image_info[])));
return $image_info;
}
else
return false;
}
/**
* 獲取是否充許上傳文件
* @param string $source 文件路徑
* @return bool
*/
function isUploadFile($source)
{
return $source && ($source != none) &&
(is_uploaded_file($source) || is_uploaded_file(str_replace( $source)));
}
/**
* 獲取保存的路徑
* @param string $dir 指定的保存目錄
* @return string
*/
function getTargetDir($dir)
{
if (!is_dir(APP_ROOT_PATH"public/"$dir)) {
@mkdir(APP_ROOT_PATH"public/"$dir);
@chmod(APP_ROOT_PATH"public/"$dir );
}
return $dir;
}
/**
* 保存文件
* @param string $source 源文件路徑
* @param string $target 目錄文件路徑
* @return bool
*/
private function saveFile($source $target)
{
if(!es_imagecls::isUploadFile($source))
$succeed = false;
elseif(@copy($source $target))
$succeed = true;
elseif(function_exists(move_uploaded_file) &&
@move_uploaded_file($source $target))
$succeed = true;
elseif (@is_readable($source) &&
(@$fp_s = fopen($source rb)) && (@$fp_t = fopen($target wb)))
{
while (!feof($fp_s))
{
$s = @fread($fp_s * );
@fwrite($fp_t $s);
}
fclose($fp_s);
fclose($fp_t);
$succeed = true;
}
if($succeed)
{
$this>error_code = ;
@chmod($target );
@unlink($source);
}
else
{
$this>error_code = ;
}
return $succeed;
}
public function thumb($image$maxWidth=$maxHeight=$gen =
$interlace=true$filepath = $is_preview = true)
{
$info = es_imagecls::getImageInfo($image);
if($info !== false)
{
$srcWidth = $info[];
$srcHeight = $info[];
$type = $info[type];
$interlace = $interlace? :;
unset($info);
if($maxWidth > && $maxHeight > )
$scale = min($maxWidth/$srcWidth $maxHeight/$srcHeight);
// 計算縮放比例
elseif($maxWidth == )
$scale = $maxHeight/$srcHeight;
elseif($maxHeight == )
$scale = $maxWidth/$srcWidth;
$paths = pathinfo($image);
$paths[filename] = trim(strtolower($paths[basename])
""strtolower($paths[extension]));
$basefilename = explode("_"$paths[filename]);
$basefilename = $basefilename[];
if(empty($filepath))
{
if($is_preview)
$thumbname = $paths[dirname]/$basefilename
_$maxWidthx$maxHeightjpg;
else
$thumbname = $paths[dirname]/$basefilename
o_$maxWidthx$maxHeightjpg;
}
else
$thumbname = $filepath;
$thumburl = str_replace(APP_ROOT_PATH/$thumbname);
if($scale >= )
{
// 超過原圖大小不再縮略
$width = $srcWidth;
$height = $srcHeight;
if(!$is_preview)
{
//非預覽模式寫入原圖
file_put_contents($thumbnamefile_get_contents($image)); //用原圖寫入
return array(url=>$thumburlpath=>$thumbname);
}
}
else
{
// 縮略圖尺寸
$width = (int)($srcWidth*$scale);
$height = (int)($srcHeight*$scale);
}
if($gen == )
{
$width = $maxWidth;
$height = $maxHeight;
}
// 載入原圖
$createFun = imagecreatefrom($type==jpg?jpeg:$type);
if(!function_exists($createFun))
$createFun = imagecreatefromjpeg;
$srcImg = $createFun($image);
//創建縮略圖
if($type!=gif && function_exists(imagecreatetruecolor))
$thumbImg = imagecreatetruecolor($width $height);
else
$thumbImg = imagecreate($width $height);
$x = ;
$y = ;
if($gen == && $maxWidth > && $maxHeight > )
{
$resize_ratio = $maxWidth/$maxHeight;
$src_ratio = $srcWidth/$srcHeight;
if($src_ratio >= $resize_ratio)
{
$x = ($srcWidth ($resize_ratio * $srcHeight)) / ;
$width = ($height * $srcWidth) / $srcHeight;
}
else
{
$y = ($srcHeight ( ( / $resize_ratio) * $srcWidth)) / ;
$height = ($width * $srcHeight) / $srcWidth;
}
}
// 復制圖片
if(function_exists("imagecopyresampled"))
imagecopyresampled($thumbImg $srcImg $x $y $width $height
$srcWidth$srcHeight);
else
imagecopyresized($thumbImg $srcImg $x $y $width $height
$srcWidth$srcHeight);
if(gif==$type || png==$type) {
$background_color = imagecolorallocate($thumbImg ); // 指派一個綠色
imagecolortransparent($thumbImg$background_color);
// 設置為透明色若注釋掉該行則輸出綠色的圖
}
// 對jpeg圖形設置隔行掃描
if(jpg==$type || jpeg==$type)
imageinterlace($thumbImg$interlace);
// 生成圖片
imagejpeg($thumbImg$thumbname);
imagedestroy($thumbImg);
imagedestroy($srcImg);
return array(url=>$thumburlpath=>$thumbname);
}
return false;
}
public function make_thumb($srcImg$srcWidth$srcHeight$type$maxWidth=
$maxHeight=$gen = )
{
$interlace = $interlace? :;
if($maxWidth > && $maxHeight > )
$scale = min($maxWidth/$srcWidth $maxHeight/$srcHeight);
// 計算縮放比例
elseif($maxWidth == )
$scale = $maxHeight/$srcHeight;
elseif($maxHeight == )
$scale = $maxWidth/$srcWidth;
if($scale >= )
{
// 超過原圖大小不再縮略
$width = $srcWidth;
$height = $srcHeight;
}
else
{
// 縮略圖尺寸
$width = (int)($srcWidth*$scale);
$height = (int)($srcHeight*$scale);
}
if($gen == )
{
$width = $maxWidth;
$height = $maxHeight;
}
//創建縮略圖
if($type!=gif && function_exists(imagecreatetruecolor))
$thumbImg = imagecreatetruecolor($width $height);
else
$thumbImg = imagecreatetruecolor($width $height);
$x = ;
$y = ;
if($gen == && $maxWidth > && $maxHeight > )
{
$resize_ratio = $maxWidth/$maxHeight;
$src_ratio = $srcWidth/$srcHeight;
if($src_ratio >= $resize_ratio)
{
$x = ($srcWidth ($resize_ratio * $srcHeight)) / ;
$width = ($height * $srcWidth) / $srcHeight;
}
else
{
$y = ($srcHeight ( ( / $resize_ratio) * $srcWidth)) / ;
$height = ($width * $srcHeight) / $srcWidth;
}
}
// 復制圖片
if(function_exists("imagecopyresampled"))
imagecopyresampled($thumbImg $srcImg $x $y $width $height
$srcWidth$srcHeight);
else
imagecopyresized($thumbImg $srcImg $x $y $width $height
$srcWidth$srcHeight);
if(gif==$type || png==$type) {
$background_color = imagecolorallocate($thumbImg );
// 指派一個綠色
imagecolortransparent($thumbImg$background_color);
// 設置為透明色若注釋掉該行則輸出綠色的圖
}
// 對jpeg圖形設置隔行掃描
if(jpg==$type || jpeg==$type)
imageinterlace($thumbImg$interlace);
return $thumbImg;
}
public function water($source$water$alpha=$position="")
{
//檢查文件是否存在
if(!file_exists($source)||!file_exists($water))
return false;
//圖片信息
$sInfo = es_imagecls::getImageInfo($source);
$wInfo = es_imagecls::getImageInfo($water);
//如果圖片小於水印圖片不生成圖片
if($sInfo[""] < $wInfo[""] || $sInfo[] < $wInfo[])
return false;
if(is_animated_gif($source))
{
require_once APP_ROOT_PATH"system/utils/gif_encoderphp";
require_once APP_ROOT_PATH"system/utils/gif_readerphp";
$gif = new GIFReader();
$gif>load($source);
foreach($gif>IMGS[frames] as $k=>$img)
{
$im = imagecreatefromstring($gif>getgif($k));
//為im加水印
$sImage=$im;
$wCreateFun="imagecreatefrom"$wInfo[type];
if(!function_exists($wCreateFun))
$wCreateFun = imagecreatefromjpeg;
$wImage=$wCreateFun($water);
//設定圖像的混色模式
imagealphablending($wImage true);
switch (intval($position))
{
case : break;
//左上
case :
$posY=;
$posX=;
//生成混合圖像
imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha);
break;
//右上
case :
$posY=;
$posX=$sInfo[]$wInfo[];
//生成混合圖像
imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha);
break;
//左下
case :
$posY=$sInfo[]$wInfo[];
$posX=;
//生成混合圖像
imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha);
break;
//右下
case :
$posY=$sInfo[]$wInfo[];
$posX=$sInfo[]$wInfo[];
//生成混合圖像
imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha);
break;
//居中
case :
$posY=$sInfo[]/$wInfo[]/;
$posX=$sInfo[]/$wInfo[]/;
//生成混合圖像
imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha);
break;
}
//end im加水印
ob_start();
imagegif($sImage);
$content = ob_get_contents();
ob_end_clean();
$frames [ ] = $content;
$framed [ ] = $img[frameDelay];
}
$gif_maker = new GIFEncoder (
$frames
$framed
"bin" //bin為二進制 url為地址
);
$image_rs = $gif_maker>GetAnimation ( );
//如果沒有給出保存文件名默認為原圖像名
@unlink($source);
//保存圖像
file_put_contents($source$image_rs);
return true;
}
//建立圖像
$sCreateFun="imagecreatefrom"$sInfo[type];
if(!function_exists($sCreateFun))
$sCreateFun = imagecreatefromjpeg;
$sImage=$sCreateFun($source);
$wCreateFun="imagecreatefrom"$wInfo[type];
if(!function_exists($wCreateFun))
$wCreateFun = imagecreatefromjpeg;
$wImage=$wCreateFun($water);
//設定圖像的混色模式
imagealphablending($wImage true);
switch (intval($position))
{
case : break;
//左上
case :
$posY=;
$posX=;
//生成混合圖像
imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha);
break;
//右上
case :
$posY=;
$posX=$sInfo[]$wInfo[];
//生成混合圖像
imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha);
break;
//左下
case :
$posY=$sInfo[]$wInfo[];
$posX=;
//生成混合圖像
imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha);
break;
//右下
case :
$posY=$sInfo[]$wInfo[];
$posX=$sInfo[]$wInfo[];
//生成混合圖像
imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha);
break;
//居中
case :
$posY=$sInfo[]/$wInfo[]/;
$posX=$sInfo[]/$wInfo[]/;
//生成混合圖像
imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha);
break;
}
//如果沒有給出保存文件名默認為原圖像名
@unlink($source);
//保存圖像
imagejpeg($sImage$source);
imagedestroy($sImage);
}
}
if(!function_exists(image_type_to_extension))
{
function image_type_to_extension($imagetype)
{
if(empty($imagetype))
return false;
switch($imagetype)
{
case IMAGETYPE_GIF : return gif;
case IMAGETYPE_JPEG : return jpeg;
case IMAGETYPE_PNG : return png;
case IMAGETYPE_SWF : return swf;
case IMAGETYPE_PSD : return psd;
case IMAGETYPE_BMP : return bmp;
case IMAGETYPE_TIFF_II : return tiff;
case IMAGETYPE_TIFF_MM : return tiff;
case IMAGETYPE_JPC : return jpc;
case IMAGETYPE_JP : return jp;
case IMAGETYPE_JPX : return jpf;
case IMAGETYPE_JB : return jb;
case IMAGETYPE_SWC : return swc;
case IMAGETYPE_IFF : return aiff;
case IMAGETYPE_WBMP : return wbmp;
case IMAGETYPE_XBM : return xbm;
default : return false;
}
}
}
?>
get_spec_img()調用圖片類然後再用下面的方法保存不同規格的圖片並返回圖片連接
//獲取相應規格的圖片地址
//gen=:保持比例縮放不剪裁如高為則保證寬度按比例縮放 gen=保證長寬剪裁
function get_spec_image($img_path$width=$height=$gen=$is_preview=true)
{
if($width==)
$new_path = $img_path;
else
{
$img_name = substr($img_path);
$img_ext = substr($img_path);
if($is_preview)
$new_path = $img_name"_"$width"x"$height"jpg";
else
$new_path = $img_name"o_"$width"x"$height"jpg";
if(!file_exists($new_path))
{
require_once "imageclsphp";
$imagec = new imagecls();
$thumb = $imagec>thumb($img_path$width$height$gentrue"
"$is_preview);
if(app_conf("PUBLIC_DOMAIN_ROOT")!=)
{
$paths = pathinfo($new_path);
$path = str_replace("/"""$paths[dirname]);
$filename = $paths[basename];
$pathwithoupublic = str_replace("public/"""$path);
$file_data = @file_get_contents($path$file);
$img = @imagecreatefromstring($file_data);
if($img!==false)
{
$save_path = "public/"$path;
if(!is_dir($save_path))
{
@mk_dir($save_path);
}
@file_put_contents($save_path$name$file_data);
}
}
}
}
return $new_path;
}
使用方法:
//im:將店鋪圖片保存為種規格:小圖:x中圖x大圖x
$small_url=get_spec_image($data[image]);
$<span id="result_box" class="short_text" lang="en">
<span>middle_url</span></span>=get_spec_image($data[image]);
$big_url=get_spec_image($data[image]);
From:http://tw.wingwit.com/Article/program/PHP/201311/21164.html