PHP 處理圖片的類實(shí)現(xiàn)代碼
更新時間:2009年10月23日 21:35:58 作者:
PHP 處理圖片的類實(shí)現(xiàn)代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
<?php
/**
* author:yagas
* email:yagas60@21cn.com
*/
class Image
{
/** 類保護(hù)變量 */
protected $th_width = 100;
protected $th_height = 50;
protected $quality = 85; //圖片質(zhì)量
protected $transparent = 50; //水印透明度
protected $background = "255,255,255"; //背景顏色
/**
* 生成縮略圖文件
* @param $src 原圖文件
* @param $dst 目標(biāo)文件
*/
public function thumb($src, $dst=null, $output=true)
{
$thumb = array($this->th_width, $this->th_height);
$this->scale($src, $thumb, $dst, $output);
}
/**
* 對圖片按百分比進(jìn)行縮放處理
* @param string $src 原圖文件
* @param string $dst 輸入的目標(biāo)文件
* @param float/array $zoom 縮放比例,浮點(diǎn)類型時按百分比綻放,數(shù)組類型時按指定大小時行縮放
* @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)) {
//按百分比進(jìn)行縮放
$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);
}
/**
* 對圖片進(jìn)行裁切
* @param $src 原始文件
* @param $dst 目標(biāo)文件
* @param $output 是否生成目標(biāo)文件
*/
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);
//當(dāng)圖片大于縮略圖時進(jìn)行縮放
if($old_width > $width && $old_height>$height) {
$resize_im = $this->imageresize($src_im, $width);
//圖片比例不合規(guī)范時,重新計算比例進(jìn)行裁切
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;
}
/**
* 對圖片進(jìn)行縮放的處理
* @param resource $src_im 圖像GD對象
* @param integer $width 圖片的寬度
* @param integer $height 圖片的高度,如果不設(shè)置高度,將對圖片進(jìn)行等比例縮放
* @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;
//創(chuàng)建新的圖象并填充默認(rèn)的背景色
$im = imagecreatetruecolor($new_width, $new_height);
$rgb = explode(",", $this->background);
$white = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
imagefill($im, 0, 0, $white);
//對圖片進(jìn)行縮放
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;
}
}
?>
您可能感興趣的文章:
- PHP圖片處理之圖片旋轉(zhuǎn)和圖片翻轉(zhuǎn)實(shí)例
- PHP使用GIFEncoder類處理gif圖片實(shí)例
- PHPThumb圖片處理實(shí)例
- php多功能圖片處理類分享(php圖片縮放類)
- php筆記之:文章中圖片處理的使用
- 用C實(shí)現(xiàn)PHP擴(kuò)展 Image_Tool 圖片常用處理工具類的使用
- PHP圖片處理類 phpThumb參數(shù)用法介紹
- 讓php處理圖片變得簡單 基于gb庫的圖片處理類附實(shí)例代碼下載
- php圖片處理:加水印、縮略圖的實(shí)現(xiàn)(自定義函數(shù):watermark、thumbnail)
- php 從數(shù)據(jù)庫提取二進(jìn)制圖片的處理代碼
- php圖片處理函數(shù)獲取類型及擴(kuò)展名實(shí)例
相關(guān)文章
php導(dǎo)出中文內(nèi)容excel文件類實(shí)例
這篇文章主要介紹了php導(dǎo)出中文內(nèi)容excel文件類,實(shí)例分析了php操作帶有中文內(nèi)容的Excel文件及文件導(dǎo)出的實(shí)現(xiàn)方法,需要的朋友可以參考下2015-07-07PHP錯誤抑制符(@)導(dǎo)致引用傳參失敗Bug的分析
今天cici網(wǎng)友發(fā)來一個問題, 說是在函數(shù)調(diào)用參數(shù)前面使用錯誤抑制符號(@)的時候, 貌似引用傳參就失效了. 他想讓我?guī)退獯馂槭裁?2011-05-05Notice: Undefined index: page in E:\PHP\test.php on line 14
Notice: Undefined index: page in E:\PHP\test.php on line 142010-11-11ThinkPHP like模糊查詢,like多匹配查詢,between查詢,in查詢,一般查詢書寫方法
ThinkPHP的數(shù)據(jù)庫條件查詢語句有字符串式,數(shù)組式書寫方法,字符串式即是原生式而數(shù)組式的查詢語句因書寫方式與特定字符的原因比較復(fù)雜,今天為大家講解一下ThinkPHP數(shù)組式查詢語句的書寫方法2018-09-09php中3種方法統(tǒng)計字符串中每種字符的個數(shù)并排序
3種方法,統(tǒng)計字符串中每種字符的個數(shù)并排序,多種解法喲~ str_split()函數(shù)很重要2012-08-08