澳门新葡萄京官网注册PHP 处理图片的类实现代码_php技巧_脚本之家

复制代码 代码如下:

复制代码 代码如下:

复制代码 代码如下:

/**
* author:yagas
* email:yagas60@21cn.com
*/
class Image
{
/** 类爱抚变量 */
protected $th_width = 100;
protected $th_height = 50;
protected $quality = 85; //图片品质
protected $transparent = 50; //水印折射率
protected $background = “255,255,255”; //背景颜色
/**
* 生成缩略图像和文字件
* @param $src 原图像和文字件
* @param $dst 目的文件
*/
public function thumb($src, $dst=null, $output=true)
{
$thumb = array($this->th_width, $this->th_height);
$this->scale($src, $thumb, $dst, $output);
}
/**
* 对图纸按百分比进行缩放管理
* @param string $src 原图像和文字件
* @param string $dst 输入的目的文件
* @param float/array $zoom
缩放比例,浮点类型时按百分比开放,数组类型时按钦赐大时辰行缩放
* @param boolean $output 是还是不是变动文书输出
*/
public function scale($src, $dst=null, $zoom=1, $output=true)
{
if(!file_exists($src)) die(‘File not exists.’);
if(!$zoom) die(‘the zoom undefine.’);
$src_im = $this->IM($src);
$old_width = imagesx($src_im);
if(is_float($zoom)) {
//按百分比举行缩放
$new_width = $old_width * $zoom;
}
elseif(is_array($zoom)) {
//显明的缩放尺寸
$new_width = $zoom[0];
}
//是还是不是定义的缩放的高度
if(!isset($zoom[1])) {
//等比例缩放
$resize_im = $this->imageresize($src_im, $new_width);
}
else {
//非等比例缩放
$resize_im = $this->imageresize($src_im, $new_width,
$zoom[澳门新葡萄京官网注册 ,1]);
}
if(!$output) {
header(“Content-type: image/jpeg”);
imagejpeg($resize_im, null, $this->quality);
}
else {
$new_file = empty($dst)? $src:$dst;
imagejpeg($resize_im, $new_file, $this->quality);
}
imagedestroy($im);
imagedestroy($nIm);
}
/**
* 对图片张开裁切
* @param $src 原始文本
* @param $dst 目的文件
* @param $output 是或不是变动目的文件
*/
public function capture($src, $dst=null, $output=true) {
if(!file_exists($src)) die(‘File not exists.’);
$width = $this->th_width;
$height = $this->th_height;
$src_im = $this->IM($src);
$old_width = imagesx($src_im);
$old_height = imagesy($src_im);
$capture = imagecreatetruecolor($width, $height);
$rgb = explode(“,”, $this->background);
$white = imagecolorallocate($capture, $rgb[0], $rgb[1],
$rgb[2]);
imagefill($capture, 0, 0, $white);
//当图片大于缩略图时开展缩放
if($old_width > $width && $old_height>$height) {
$resize_im = $this->imageresize($src_im, $width);
//图片比例不合标准时,重新总计比例实行裁切
if(imagesy($resize_im) < $height) {
$proportion = $old_height/$this->th_height;
$resize_im = $this->imageresize($src_im,
$old_width/$proportion);
}
$posx = 0;
$posy = 0;
}
else {
//图片小于缩略图时将图纸居中彰显
$posx = ($width-$old_width)/2;
$posy = ($height-$old_height)/2;
$resize_im = $src_im;
}
imagecopy($capture, $resize_im, $posx, $posy, 0, 0,
imagesx($resize_im), imagesy($resize_im));
if(!$output) {
header(“Content-type: image/jpeg”);
imagejpeg($capture, null, $this->quality);
}
else {
$new_file = empty($dst)? $src:$dst;
imagejpeg($capture, $new_file, $this->quality);
}
imagedestroy($src_im);
@imagedestroy($resize_im);
imagedestroy($capture);
}
/**
* 写入水印图片
* @param $src 要求写入水印的图片
* @param $mark 水印图片
* @param $transparent 水印发光度
*/
public function mark($src, $mark, $dst=”, $output=true)
{
$mark_info = getimagesize($mark);
$src_info = getimagesize($src);
list($mw,$mh) = $mark_info;
list($sw,$sh) = $src_info;
$px = $sw – $mw;
$py = $sh – $mh;
$im = $this->IM($src);
$mim = $this->IM($mark);
imagecopymerge($im, $mim, $px, $py, 0, 0, $mw, $mh,
$this->transparent);
if($output){
$new_file = empty($dst)? $src:$dst;
imagejpeg($im, $new_file, $this->quality);
}
else
{
header(‘Content-type: image/jpeg’);
imagejpeg($im);
}
imagedestroy($im);
imagedestroy($mim);
}
/**
* 通过文件,获取不相同的GD对象
*/
protected function IM($file)
{
if(!file_exists($file)) die(‘File not exists.’);
$info = getimagesize($file);
switch($info[‘mime’])
{
case ‘image/gif’:
$mim = imagecreatefromgif($file);
break;
case ‘image/png’:
$mim = imagecreatefrompng($file);
imagealphablending($mim, false);
imagesavealpha($mim, true);
break;
case ‘image/jpeg’:
$mim = imagecreatefromjpeg($file);
break;
default:
die(‘File format errors.’);
}
return $mim;
}
/**
* 对图纸展开缩放的拍卖
* @param resource $src_im 图像GD对象
* @param integer $width 图片的宽度
* @param integer $height
图片的莫斯中国科学技术大学学,即使不设置中度,将对图片举行等比例缩放
* @return resuorce $im 重回八个GD对象
*/
protected function imageresize($src_im, $width, $height=null) {
$old_width = imagesx($src_im);
$old_height = imagesy($src_im);
$proportion = $old_width/$old_height;
$new_width = $width;
$new_height = is_null($height)? round($new_width /
$proportion):$height;
//创制新的图象并填充私下认可的背景象
$im = imagecreatetruecolor($new_width, $new_height);
$rgb = explode(“,”, $this->background);
$white = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
imagefill($im, 0, 0, $white);
//对图片打开缩放
imagecopyresized($im, $src_im, 0, 0, 0, 0, $new_width, $new_height,
$old_width, $old_height);
return $im;
}
/**
* 类变量赋值
*/
public function __set($key, $value)
{
$this->$key = $value;
}
/**
* 获取类变量值
*/
public function __get($key)
{
return $this->$key;
}
}
?>

th_width, $this->th_height); $this->scale($src, $thumb, $dst,
$output); } /** * 对图片按百分比实行缩放管理 * @param string $src
原图像和文字件 * @param string $dst 输入的对象文件 * @param float/array
$zoom 缩放比例,浮点类型时按百分比开放,数组类型时按钦赐大小时行缩放 *
@param boolean $output 是或不是变动文书输出 */ public function scale($src,
$dst=null, $zoom=1, $output=true) { if die; if die; $src_im =
$this->IM; $old_width = imagesx; if { //按百分比进行缩放 $new_width
= $old_width * $zoom; } elseif { //分明的缩放尺寸 $new_width =
$zoom[0]; } //是不是定义的缩放的惊人 if { //等比例缩放 $resize_im =
$this->imageresize; } else { //非等比例缩放 $resize_im =
$this->imageresize($src_im, $new_width, $zoom[1]); } if {
header(“Content-type: image/jpeg”); imagejpeg($resize_im, null,
$this->quality); } else { $new_file = empty? $src:$dst;
imagejpeg($resize_im, $new_file, $this->quality); } imagedestroy;
imagedestroy; } /** * 对图片实行裁切 * @param $src 原始文本 *
@param $dst 目的文件 * @param $output 是或不是变动目的文件 */ public
function capture($src, $dst=null, $output=true) { if die; $width =
$this->th_width; $height = $this->th_height; $src_im =
$this->IM; $old_width = imagesx; $old_height = imagesy; $capture =
imagecreatetruecolor; $rgb = explode(“,”, $this->background); $white
= imagecolorallocate($capture, $rgb[0], $rgb[1], $rgb[2]卡塔尔国;
imagefill($capture, 0, 0, $white卡塔尔(قطر‎; //当图片大于缩略图时打开缩放
if($old_width > $width && $old_height>$height) { $resize_im =
$this->imageresize; //图片比例不合规范时,重新计算比例举行裁切 if
< $heightState of Qatar { $proportion = $old_height/$this->th_height;
$resize_im = $this->imageresize($src_im, $old_width/$proportion卡塔尔国;
} $posx = 0; $posy = 0; } else { //图片小于缩略图时将图纸居中显示 $posx
= /2; $posy = /2; $resize_im = $src_im; } imagecopy($capture,
$resize_im, $posx, $posy, 0, 0, imagesx, imagesy; if {
header(“Content-type: image/jpeg”); imagejpeg($capture, null,
$this->quality); } else { $new_file = empty? $src:$dst;
imagejpeg($capture, $new_file, $this->quality); } imagedestroy;
@imagedestroy; imagedestroy; } /** * 写入水印图片 * @param $src
要求写入水印的图样 * @param $mark 水印图片 * @param $transparent
水印光滑度 */ public function mark($src, $mark, $dst=”, $output=true)
{ $mark_info = getimagesize; $src_info = getimagesize = $mark_info;
list = $src_info; $px = $sw – $mw; $py = $sh – $mh; $im = $this->IM;
$mim = $this->IM; imagecopymerge($im, $mim, $px, $py, 0, 0, $mw, $mh,
$this->transparent); if{ $new_file = empty? $src:$dst;
imagejpeg($im, $new_file, $this->quality); } else {
header(‘Content-type: image/jpeg’); imagejpeg; } imagedestroy;
imagedestroy; } /** * 通过文件,获取不一致的GD对象 */ protected
function IM { if die; $info = getimagesize; switch { case ‘image/gif’:
$mim = imagecreatefromgif; break; case ‘image/png’: $mim =
imagecreatefrompng; imagealphablending; imagesavealpha; break; case
‘image/jpeg’: $mim = imagecreatefromjpeg; break; default: die(‘File
format errors.’); } return $mim; } /** * 对图纸张开缩放的管理 *
@param resource $src_im 图像GD对象 * @param integer $width 图片的升幅
* @param integer $height
图片的冲天,假若不安装高度,将对图纸展开等比例缩放 * @return resuorce
$im 重回一个GD对象 */ protected function imageresize($src_im, $width,
$height=null) { $old_width = imagesx; $old_height = imagesy;
$proportion = $old_width/$old_height; $new_width = $width;
$new_height = is_null? round($new_width / $proportionState of Qatar:$height;
//创制新的图象并填充私下认可的背景象 $im = imagecreatetruecolor($new_width,
$new_height); $rgb = explode(“,”, $this->background); $white =
imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]State of Qatar; imagefill;
//对图纸张开缩放 imagecopyresized($im, $src_im, 0, 0, 0, 0,
$new_width, $new_height, $old_width, $old_height); return $im; }
/** * 类变量赋值 */ public function __set { $this->$key =
$value; } /** * 获取类变量值 */ public function __get { return
$this->$key; } } ?>

“.$all_path.”
“; echo “宽度:”.$src_w.”px “; echo “长度:”.$src_h.”px “; echo “
大小:”.$file[“size”].” bytes”; switch//剖断源图片文件类型 { case
1://gif $src_im = imagecreatefromgif;//从源图片文件拿到图像 break; case
2://jpg $src_im = imagecreatefromjpeg; break; case 3://png $src_im =
imagecreatefrompng; break; //case 6: //$src_im=imagecreatefromwbmp;
//break; default: die; exit; } if { //$iinfo = getimagesize; $dst_im =
imagecreatetruecolor; //依据原图尺寸成立一个同等大小的真彩色位图 $white
= imagecolorallocate;//白 //给新图填充背景观 $black =
imagecolorallocate;//黑 $red = imagecolorallocate;//红 $orange =
imagecolorallocate;//橙 imagefill;
imagecopymerge($dst_im,$src_im,0,0,0,0,$src_w,$src_h,100State of Qatar;//原图图像写入新建真彩位图中
//imagefilledrectangle($dst_im,1,$src_h-15,80,$src_h,$white卡塔尔(قطر‎; switch
{ case 1: //加水印字符串
imagestring($dst_im,5,5,$src_h-20,$waterstring,$orange卡塔尔国;//文字水印,字体5号颜色栗褐,坐落于背景图左下角
break; case 2: //加水印图片 $lim_size = getimagesize;
//拿到水印图像尺寸,新闻 switch //判定水印图片文件类型 { case 1://gif
$src_lim = imagecreatefromgif; //拿到水印图像 break; case 2://jpg
$src_lim = imagecreatefromjpeg; break; case 3://png $src_lim =
imagecreatefrompng; break; //case 6: //$src_im=imagecreatefromwbmp;
//break; default: die; exit; } $src_lw = /2;
//水印坐落于背景图正宗旨width定位 $src_lh = /2; //height定位
imagecopymerge($dst_im,$src_lim,$src_lw,$src_lh,0,0,$lim_size[0],$lim_size[1],$waterclearly卡塔尔国;//
合併几个图像,设置水印透明度$waterclearly imagedestroy; break; } switch
{ case 1:
imagegif($dst_im,$all_path,$imclearly卡塔尔;//生成gif文件,图片清晰度0-100
break; case 2:
imagejpeg($dst_im,$all_path,$imclearly卡塔尔(قطر‎;//生成jpg文件,图片清晰度0-100
break; case 3:
imagepng($dst_im,$all_path,$imclearly卡塔尔(قطر‎;//生成png文件,图片清晰度0-100
break; //case 6: //imagewbmp; break; } //释放慢存 imagedestroy; } if {
$sall_path = $path_sim.time().”.”.$filetype; if (file_exists { echo
“同名文件已经存在了”; exit; } if // 原图尺寸 <= 缩略图尺寸 {
$dst_sim = imagecreatetruecolor; //新建缩略图真彩位图
imagecopymerge($dst_sim,$src_im,0,0,0,0,$src_w,$src_h,100卡塔尔(قطر‎;
//原图图像写入新建真彩位图中 } if // 原图尺寸 > 缩略图尺寸 { $dst_sh
= $dst_sw/$src_w*$src_h; $dst_sim = imagecreatetruecolor;
//新建缩略图真彩位图
imagecopyresampled($dst_sim,$src_im,0,0,0,0,$dst_sw,$dst_sh,$src_w,$src_h卡塔尔(قطر‎;
//原图图像写入新建真彩位图中 } switch { case 1:
imagegif($dst_sim,$sall_path,$simclearly卡塔尔(قطر‎;//生成gif文件,图片清晰度0-100
break; case 2:
imagejpeg($dst_sim,$sall_path,$simclearly卡塔尔;//生成jpg文件,图片清晰度0-100
break; case 3:
imagepng($dst_sim,$sall_path,$simclearly卡塔尔(قطر‎;//生成png文件,图片清晰度0-100
break; //case 6: //imagewbmp; break; } //释放慢存 imagedestroy; }
//释放慢存 imagedestroy; } ?> php等比例生成缩略图函数2复制代码 代码如下: function reSizeImg($imgSrc,
$resize_width, $resize_height, $isCut=false卡塔尔 { //图片的门类 $type =
substr ( strrchr ; //最初化图象 if { $im = imagecreatefromjpeg ; } if {
$im = imagecreatefromgif ; } if { $im = imagecreatefrompng ; }
//指标图象地址 $full_length = strlen ; $type_length = strlen ;
$name_length = $full_length – $type_length; $name = substr ( $imgSrc,
0, $name_length – 1 ); $dstimg = $name . “_s.” . $type; $width =
imagesx ; $height = imagesy ; //生成图象 //退换后的图象的比重
$resize_ratio = / ; //实际图象的比重 $ratio = ; if //裁图 { if ($ratio
>= $resize_ratioState of Qatar //中度优先 { $newimg = imagecreatetruecolor (
$resize_width, $resize_height ); imagecopyresampled ( $newimg, $im, 0,
0, 0, 0, $resize_width, $resize_height, * $resize_ratio), $height );
ImageJpeg ; } if ($ratio < $resize_ratio卡塔尔国 //宽度优先 { $newimg =
imagecreatetruecolor ( $resize_width, $resize_height );
imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $resize_width,
$resize_height, $width, / $resize_ratio) ); ImageJpeg ; } } else
//不裁图 { if ($ratio >= $resize_ratio) { $newimg =
imagecreatetruecolor ( $resize_width, / $ratio ); imagecopyresampled (
$newimg, $im, 0, 0, 0, 0, $resize_width, / $ratio, $width, $height );
ImageJpeg ; } if ($ratio < $resize_ratio) { $newimg =
imagecreatetruecolor * $ratio, $resize_height ); imagecopyresampled (
$newimg, $im, 0, 0, 0, 0, * $ratio, $resize_height, $width, $height );
ImageJpeg ; } } ImageDestroy ; }

发表评论

电子邮件地址不会被公开。 必填项已用*标注