php圖片處理類class,php 圖片

本文目錄一覽:

求PHP圖片處理類。

裁剪圖片。合併,可以通過相應的函數進行,先確定圖片的後綴,再確定使用的函數,具體可查看手冊,getimagesize,imagecreatefromgif,imagecreatefromjpeg,imagecopy

thinkphp Image.class.php類的問題

這僅僅是個函數名而已,跟close沒什麼關係,不像是連接數據庫,最後不用的時候關閉連接

php上傳圖片時有什麼常用的類可以做做等比

功能:

1.按比例縮小/放大

2.填充背景色

3.按區域裁剪

4.添加水印,包括水印的位置,透明度等

?php

/** 縮略圖生成類,支持imagemagick及gd庫兩種處理

*   Date:   2013-07-15

*   Author: fdipzone

*   Ver:    1.2

*

*   Func:

*   public  set_config: 設置參數

*   public  create_thumb: 生成縮略圖

*   private fit: 縮略圖片

*   private crop: 裁剪圖片

*   private gd_fit: GD庫縮略圖片

*   private gd_crop: GD庫裁剪圖片

*   private get_size: 獲取要轉換的size

*   private get_crop_offset: 獲取裁圖的偏移量

*   private add_watermark: 添加水印

*   private check_handler: 判斷處理程序是否已安裝

*   private create_dirs: 創建目錄

*   private exists: 判斷參數是否存在

*   private to_log: 記錄log

*   private hex2rgb: hex顏色轉rgb顏色

*   private get_file_ext: 獲取圖片類型

*

*   ver:    1.1 增加GD庫處理

*   ver:    1.2 增加width,height錯誤參數處理

*               增加當圖片colorspace不為RGB時作轉RGB處理

*               修正使用crop保存為gif時出現透明無效區域問題,使用+repage參數,刪除透明無效區域即可

*

*   tips:建議使用imagemagick

*        GD庫不支持透明度水印,如果必須使用透明水印,請將水印圖片做成有透明度。

*        GD庫輸出gif如加透明水印,會有問題。

*/

class PicThumb{ // class start

private $_log = null;            // log file

private $_handler = null;        // 進行圖片處理的程序,imagemagick/gd庫

private $_type = ‘fit’;          // fit or crop

private $_source = null;         // 原圖路徑

private $_dest = null;           // 縮略圖路徑

private $_watermark = null;      // 水印圖片

private $_opacity = 75;          // 水印圖片透明度,gd庫不支持

private $_gravity = ‘SouthEast’; // 水印擺放位置 NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast

private $_geometry = ‘+10+10’;   // 水印定位,gd庫不支持

private $_croppos = ‘TL’;        // 截圖的位置 TL TM TR ML MM MR BL BM BR

private $_bgcolor = null;        // 填充的背景色

private $_quality = 90;          // 生成的圖片質量

private $_width = null;          // 指定區域寬度

private $_height = null;         // 指定區域高度

// 初始化

public function __construct($logfile=”){

if($logfile!=”){

$this-_log = $logfile;

}

}

// 設置參數

public function set_config($param=array()){

$this-_handler = $this-exists($param, ‘handler’)? strtolower($param[‘handler’]) : null;

$this-_type = $this-exists($param, ‘type’)? strtolower($param[‘type’]) : ‘fit’;

$this-_watermark = $this-exists($param, ‘watermark’)? $param[‘watermark’] : null;

$this-_opacity = $this-exists($param, ‘opacity’)? $param[‘opacity’] : 75;

$this-_gravity = $this-exists($param, ‘gravity’)? $param[‘gravity’] : ‘SouthEast’;

$this-_geometry = $this-exists($param, ‘geometry’)? $param[‘geometry’] : ‘+10+10’;

$this-_croppos = $this-exists($param, ‘croppos’)? $param[‘croppos’] : ‘TL’;

$this-_bgcolor = $this-exists($param, ‘bgcolor’)? $param[‘bgcolor’] : null;

$this-_quality = $this-exists($param, ‘quality’)? $param[‘quality’] : 90;

$this-_width = $this-exists($param, ‘width’)? $param[‘width’] : null;

$this-_height = $this-exists($param, ‘height’)? $param[‘height’] : null;

}

/** 創建縮略圖

* @param String $source 原圖

* @param String $dest   目標圖

* @return boolean

*/

public function create_thumb($source, $dest){

// 檢查使用的handler是否已安裝

if(!$this-check_handler()){

$this-to_log(‘handler not installed’);

return false;

}

// 判斷區域寬高是否正確

if(!is_numeric($this-_width) || !is_numeric($this-_height) || $this-_width=0 || $this-_height=0){

$this-to_log(‘width or height invalid’);

return false;

}

// 判斷源文件是否存在

if(!file_exists($source)){

$this-to_log($source.’ not exists’);

return false;

}

// 創建目標文件路徑

if(!$this-create_dirs($dest)){

$this-to_log(dirname($dest).’ create fail’);

return false;

}

$this-_source = $source;   // 源文件

$this-_dest = $dest;       // 目標文件

// 處理圖片

switch($this-_type){

case ‘fit’:

if($this-_handler==’imagemagick’){

return $this-fit();

}else{

return $this-gd_fit();

}

break;

case ‘crop’:

if($this-_handler==’imagemagick’){

return $this-crop();

}else{

return $this-gd_crop();

}

break;

default:

$this-to_log($this-_type.’ not fit and crop’);

return false;

}

}

/** 按比例壓縮或拉伸圖片

* @return boolean

*/

private function fit(){

// 判斷是否填充背景

$bgcolor = ($this-_bgcolor!=null)?

sprintf(” -background ‘%s’ -gravity center -extent ‘%sx%s’ “, $this-_bgcolor, $this-_width, $this-_height) : “”;

// 判斷是否要轉為RGB

$source_info = getimagesize($this-_source);

$colorspace = (!isset($source_info[‘channels’]) || $source_info[‘channels’]!=3)? ‘ -colorspace RGB ‘ : ”;

// 命令行

$cmd = sprintf(“convert -resize ‘%sx%s’ ‘%s’ %s -quality %s %s ‘%s'”, $this-_width, $this-_height, $this-_source, $bgcolor, $this-_quality, $colorspace, $this-_dest);

// 記錄執行的命令

$this-to_log($cmd);

// 執行命令

exec($cmd);

// 添加水印

$this-add_watermark($this-_dest);

return is_file($this-_dest)? true : false;

}

/** 裁剪圖片

* @return boolean

*/

private function crop(){

// 獲取生成的圖片尺寸

list($pic_w, $pic_h) = $this-get_size();

// 獲取截圖的偏移量

list($offset_w, $offset_h) = $this-get_crop_offset($pic_w, $pic_h);

// 判斷是否要轉為RGB

$source_info = getimagesize($this-_source);

$colorspace = (!isset($source_info[‘channels’]) || $source_info[‘channels’]!=3)? ‘ -colorspace RGB ‘ : ”;

// 命令行

$cmd = sprintf(“convert -resize ‘%sx%s’ ‘%s’ -quality %s %s -crop %sx%s+%s+%s +repage ‘%s'”, $pic_w, $pic_h, $this-_source, $this-_quality, $colorspace, $this-_width, $this-_height, $offset_w, $offset_h, $this-_dest);

// 記錄執行的命令

$this-to_log($cmd);

// 執行命令

exec($cmd);

// 添加水印

$this-add_watermark($this-_dest);

return is_file($this-_dest)? true : false;

}

/** GD庫按比例壓縮或拉伸圖片

* @return boolean

*/

private function gd_fit(){

// 獲取生成的圖片尺寸

list($pic_w, $pic_h) = $this-get_size();

list($owidth, $oheight, $otype) = getimagesize($this-_source);

switch($otype){

case 1: $source_img = imagecreatefromgif($this-_source); break;

case 2: $source_img = imagecreatefromjpeg($this-_source); break;

case 3: $source_img = imagecreatefrompng($this-_source); break;

default: return false;

}

// 按比例縮略/拉伸圖片

$new_img = imagecreatetruecolor($pic_w, $pic_h);

imagecopyresampled($new_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);

// 判斷是否填充背景

if($this-_bgcolor!=null){

$bg_img = imagecreatetruecolor($this-_width, $this-_height);

$rgb = $this-hex2rgb($this-_bgcolor);

$bgcolor =imagecolorallocate($bg_img, $rgb[‘r’], $rgb[‘g’], $rgb[‘b’]);

imagefill($bg_img, 0, 0, $bgcolor);

imagecopy($bg_img, $new_img, (int)(($this-_width-$pic_w)/2), (int)(($this-_height-$pic_h)/2), 0, 0, $pic_w, $pic_h);

$new_img = $bg_img;

}

// 獲取目標圖片的類型

$dest_ext = $this-get_file_ext($this-_dest);

// 生成圖片

switch($dest_ext){

case 1: imagegif($new_img, $this-_dest, $this-_quality); break;

case 2: imagejpeg($new_img, $this-_dest, $this-_quality); break;

case 3: imagepng($new_img, $this-_dest, (int)(($this-_quality-1)/10)); break;

}

if(isset($source_img)){

imagedestroy($source_img);

}

if(isset($new_img)){

imagedestroy($new_img);

}

// 添加水印

$this-add_watermark($this-_dest);

return is_file($this-_dest)? true : false;

}

/** GD庫裁剪圖片

* @return boolean

*/

private function gd_crop(){

// 獲取生成的圖片尺寸

list($pic_w, $pic_h) = $this-get_size();

// 獲取截圖的偏移量

list($offset_w, $offset_h) = $this-get_crop_offset($pic_w, $pic_h);

list($owidth, $oheight, $otype) = getimagesize($this-_source);

switch($otype){

case 1: $source_img = imagecreatefromgif($this-_source); break;

case 2: $source_img = imagecreatefromjpeg($this-_source); break;

case 3: $source_img = imagecreatefrompng($this-_source); break;

default: return false;

}

// 按比例縮略/拉伸圖片

$tmp_img = imagecreatetruecolor($pic_w, $pic_h);

imagecopyresampled($tmp_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);

// 裁剪圖片

$new_img = imagecreatetruecolor($this-_width, $this-_height);

imagecopyresampled($new_img, $tmp_img, 0, 0, $offset_w, $offset_h, $this-_width, $this-_height, $this-_width, $this-_height);

// 獲取目標圖片的類型

$dest_ext = $this-get_file_ext($this-_dest);

// 生成圖片

switch($dest_ext){

case 1: imagegif($new_img, $this-_dest, $this-_quality); break;

case 2: imagejpeg($new_img, $this-_dest, $this-_quality); break;

case 3: imagepng($new_img, $this-_dest, (int)(($this-_quality-1)/10)); break;

}

if(isset($source_img)){

imagedestroy($source_img);

}

if(isset($tmp_img)){

imagedestroy($tmp_img);

}

if(isset($new_img)){

imagedestroy($new_img);

}

// 添加水印

$this-add_watermark($this-_dest);

return is_file($this-_dest)? true : false;

}

/** 獲取目標圖生成的size

* @return Array $width, $height

*/

private function get_size(){

list($owidth, $oheight) = getimagesize($this-_source);

$width = (int)($this-_width);

$height = (int)($this-_height);

switch($this-_type){

case ‘fit’:

$pic_w = $width;

$pic_h = (int)($pic_w*$oheight/$owidth);

if($pic_h$height){

$pic_h = $height;

$pic_w = (int)($pic_h*$owidth/$oheight);

}

break;

case ‘crop’:

$pic_w = $width;

$pic_h = (int)($pic_w*$oheight/$owidth);

if($pic_h$height){

$pic_h = $height;

$pic_w = (int)($pic_h*$owidth/$oheight);

}

break;

}

return array($pic_w, $pic_h);

}

/** 獲取截圖的偏移量

* @param int $pic_w 圖寬度

* @param int $pic_h 圖高度

* @return Array $offset_w, $offset_h

*/

private function get_crop_offset($pic_w, $pic_h){

$offset_w = 0;

$offset_h = 0;

switch(strtoupper($this-_croppos)){

case ‘TL’:

$offset_w = 0;

$offset_h = 0;

break;

case ‘TM’:

$offset_w = (int)(($pic_w-$this-_width)/2);

$offset_h = 0;

break;

case ‘TR’:

$offset_w = (int)($pic_w-$this-_width);

$offset_h = 0;

break;

case ‘ML’:

$offset_w = 0;

$offset_h = (int)(($pic_h-$this-_height)/2);

break;

case ‘MM’:

$offset_w = (int)(($pic_w-$this-_width)/2);

$offset_h = (int)(($pic_h-$this-_height)/2);

break;

case ‘MR’:

$offset_w = (int)($pic_w-$this-_width);

$offset_h = (int)(($pic_h-$this-_height)/2);

break;

case ‘BL’:

$offset_w = 0;

$offset_h = (int)($pic_h-$this-_height);

break;

case ‘BM’:

$offset_w = (int)(($pic_w-$this-_width)/2);

$offset_h = (int)($pic_h-$this-_height);

break;

case ‘BR’:

$offset_w = (int)($pic_w-$this-_width);

$offset_h = (int)($pic_h-$this-_height);

break;

}

return array($offset_w, $offset_h);

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/284773.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-22 15:42
下一篇 2024-12-22 15:42

相關推薦

  • Idea新建文件夾沒有java class的解決方法

    如果你在Idea中新建了一個文件夾,卻沒有Java Class,應該如何解決呢?下面從多個方面來進行解答。 一、檢查Idea設置 首先,我們應該檢查Idea的設置是否正確。打開Id…

    編程 2025-04-29
  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • 用Python繪製酷炫圖片

    在本篇文章中,我們將展示如何使用Python繪製酷炫的圖片。 一、安裝Python繪圖庫 在使用Python繪製圖片之前,我們需要先安裝Python繪圖庫。Python有很多繪圖庫…

    編程 2025-04-29
  • Python Class括號中的參數用法介紹

    本文將對Python中類的括號中的參數進行詳細解析,以幫助初學者熟悉和掌握類的創建以及參數設置。 一、Class的基本定義 在Python中,通過使用關鍵字class來定義類。類包…

    編程 2025-04-29
  • 使用axios獲取返回圖片

    使用axios獲取返回圖片是Web開發中很常見的需求。本文將介紹如何使用axios獲取返回圖片,並從多個方面進行詳細闡述。 一、安裝axios 使用axios獲取返回圖片前,首先需…

    編程 2025-04-29
  • Python 圖片轉表格

    本文將詳細介紹如何使用Python將圖片轉為表格。大家平時在處理一些資料的時候難免會遇到圖片轉表格的需求。比如從PDF文檔中提取表格等場景。當然,這個功能也可以通過手動複製、粘貼,…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • Python緩存圖片的處理方式

    本文將從多個方面詳細闡述Python緩存圖片的處理方式,包括緩存原理、緩存框架、緩存策略、緩存更新和緩存清除等方面。 一、緩存原理 緩存是一種提高應用程序性能的技術,在網絡應用中流…

    編程 2025-04-29
  • Python如何抓取圖片數據

    Python是一門強大的編程語言,能夠輕鬆地進行各種數據抓取與處理。抓取圖片數據是一個非常常見的需求。在這篇文章中,我們將從多個方面介紹Python如何抓取圖片數據。 一、使用ur…

    編程 2025-04-29
  • Avue中如何按照後端返回的鏈接顯示圖片

    Avue是一款基於Vue.js、Element-ui等技術棧的可視化開發框架,能夠輕鬆搭建前端頁面。在開發中,我們使用到的圖片通常都是存儲在後端服務器上的,那麼如何使用Avue來展…

    編程 2025-04-28

發表回復

登錄後才能評論