4、圖像壓縮
對圖像進(jìn)行壓
縮處理非常簡單,因?yàn)榫鸵粋€(gè)函數(shù)
參數(shù)1:目標(biāo)圖像資源(畫布)
參數(shù)2:等待壓縮圖像資源
參數(shù)3:目標(biāo)點(diǎn)的x坐標(biāo)
參數(shù)4:目標(biāo)點(diǎn)的y坐標(biāo)
參數(shù)5:原圖的x坐標(biāo)
參數(shù)6:原圖的y坐標(biāo)
參數(shù)7:目的地寬度(畫布寬)
參數(shù)8:目的地高度(畫布高)
參數(shù)9:原圖寬度
參數(shù)10:原圖高度
imagecopyresampled($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
封裝的圖像壓縮類
<?php
/*
* 圖像壓縮處理類
*/
class
Thumb
{
private
$_filename
;
//等待壓縮的圖像
private
$_thumb_path
=
'thumb/'
;
//壓縮圖像的保存目錄
public
function
__set(
$p
,
$v
)
{
if
(property_exists(
$this
,
$p
)){
$this
->
$p
=
$v
;
}
}
//構(gòu)造方法初始化需要壓縮的圖像
public
function
__construct(
$file
)
{
if
(!
file_exists
(
$file
)){
echo
'文件有誤,不能壓縮'
;
return
;
}
$this
-> _filename =
$file
;
}
//圖像壓縮處理
function
makeThumb(
$area_w
,
$area_h
)
{
$src_image
= imagecreatefrompng(
$this
->_filename);
$res
=
getimagesize
(
$this
->_filename);
echo
'<pre>'
;
var_dump(
$res
);
die
;
$dst_x
= 0;
$dst_y
= 0;
$src_x
= 0;
$src_y
= 0;
//原圖的寬度、高度
$src_w
= imagesx(
$src_image
);
//獲得圖像資源的寬度
$src_h
= imagesy(
$src_image
);
//獲得圖像資源的高度
if
(
$src_w
/
$area_w
<
$src_h
/
$area_h
){
$scale
=
$src_h
/
$area_h
;
}
if
(
$src_w
/
$area_w
>=
$src_h
/
$area_h
){
$scale
=
$src_w
/
$area_w
;
}
$dst_w
= (int)(
$src_w
/
$scale
);
$dst_h
= (int)(
$src_h
/
$scale
);
$dst_image
= imagecreatetruecolor(
$dst_w
,
$dst_h
);
$color
= imagecolorallocate(
$dst_image
,255,255,255);
//將白色設(shè)置為透明色
imagecolortransparent(
$dst_image
,
$color
);
imagefill(
$dst_image
,0,0,
$color
);
imagecopyresampled(
$dst_image
,
$src_image
,
$dst_x
,
$dst_y
,
$src_x
,
$src_y
,
$dst_w
,
$dst_h
,
$src_w
,
$src_h
);
//可以在瀏覽器直接顯示
//header("Content-Type:image/png");
//imagepng($dst_image);
//分目錄保存壓縮的圖像
$sub_path
=
date
(
'Ymd'
).
'/'
;
//規(guī)范:上傳的圖像保存到upload目錄,壓縮的圖像保存到thumb目錄
if
(!
is_dir
(
$this
-> _thumb_path .
$sub_path
)){
mkdir
(
$this
-> _thumb_path .
$sub_path
,0777,true);
}
$filename
=
$this
-> _thumb_path .
$sub_path
.
'thumb_'
.
$this
->_filename;
//也可以另存為一個(gè)新的圖像
imagepng(
$dst_image
,
$filename
);
return
$filename
;
}
}
$thumb
=
new
Thumb(
'upload.jpg'
);
$thumb
-> _thumb_path =
'static/thumb/'
;
$file
=
$thumb
-> makeThumb(100,50);
// var_dump($file);
以上就是PHP圖像處理繪圖、水印、驗(yàn)證碼、圖像壓縮技術(shù)實(shí)例總結(jié)介紹,希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
- PHP實(shí)現(xiàn)高清晰度無損圖片壓縮功能的代碼
- 用PHP處理png圖片白色背景色改為透明色的實(shí)例代碼
- 關(guān)于PHP往mysql數(shù)據(jù)庫中批量插入數(shù)據(jù)實(shí)例教程
- Php兩點(diǎn)地理坐標(biāo)距離的計(jì)算方法和具體代碼
- PHP獲取HTTP body內(nèi)容的方法
- PHP面向?qū)ο蟪绦蛟O(shè)計(jì)中獲取對象屬性的3種方法實(shí)例分析
- php5.5新增的yield關(guān)鍵字功能與相關(guān)使用技巧
- Windows7下IIS+php配置教程詳細(xì)介紹
- PHP序列化的四種實(shí)現(xiàn)方法與橫向?qū)Ρ冉坛?/a>
- PHP基于Redis消息隊(duì)列實(shí)現(xiàn)的消息推送的方法
分享到:
投訴收藏