複製程式碼 程式碼如下:
/**
* 作者:yagas
* 信箱: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_imageresize($src_imageresize($src_imageresize($src_imageresize)($src_imageresize) , $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);
$old_height = imagesy($src_im);
$capture = imagecreatetruecolor($width, $height);
$rgb = explode(",", $this->background);
$white = imagecolorallocate($capture, $rgb[0], $rgbb [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) $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);
}
/**🎝🎝 >*/
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物件
*/
受保護函數IM($file)
{
if(!file_exists($file)) die('檔案不存在。');
$info = getimagesize($file);
switch($info['mime'])
{
case 'image/gif':
$mim = imagecreatefromgif($file);
case 'image/png':
$mim = imagecreatefrompng($file);
imagealphablending($mim, false);
imagesavealpha($mim, true); >休息;
case 'image/jpeg':
$mim = imagecreatefromjpeg($file);
休息;
預設值:
die('檔案格式錯誤。');
}
返回$mim;
}
/**
* 對圖片進行縮放的處理
* @param resource $src_im 圖像GD物件
* @param integer $width 圖片的寬度
* @param integer $height 圖片的高度,如果不設定高度,將對圖片進行等比例縮放
* @return resuorce $im 傳回一個GD物件
*/
受保護函數imageresize($src_im, $width, $height=null) {
$old_width = imagesx($src_im);
$old_height = imagesy($src_im);
$比例=$old_width/$old_height;
$new_width = $width; )?圓形($new_width / $proportion):$height;
//建立新的映像並填入預設的背景色
$im = imagecreatetruecolor($new_width, $new_height);
$rgbb =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) ;
回傳$im;
}
/**
* 類別變數賦值
*/
public function __set($key, $value)
{
$this->$key = $value;
}
/**
* 取得類別變數值
*/
public function __get($key)
{
return $this->$key;
}
}
? >
以上就介紹了處理圖片的軟體PHP處理圖片的類實現代碼,包括了處理圖片的軟體方面的內容,希望對PHP教程有興趣的朋友得到幫助。