国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

Php圖像處理類代碼分享

2024-05-04 23:15:50
字體:
供稿:網(wǎng)友
目前只實(shí)現(xiàn)了三個(gè)功能:1:圖片縮放,2:圖片裁剪,3:加圖片水印
在實(shí)例化中,通過給第二個(gè)參數(shù)傳不同的值,從而實(shí)現(xiàn)不同的功能

復(fù)制代碼 代碼如下:


<?php
include "image.class.php";
$image=new image("2.png", 1, "300", "500", "5.png"); //使用圖片縮放功能
$image=new image("2.png", 2, "0,0", "50,50", "5.png"); //使用圖片裁剪功能
$image=new image("2.png", 3, "1.png", "0", "5.png"); //使用加圖片水印功能
$image->outimage();
?>


PHP代碼

復(fù)制代碼 代碼如下:


<?php
/*已知問題:1.在圖片縮放功能中,使用imagecreatetruecolor函數(shù)創(chuàng)建畫布,并使用透明處理算法,但PNG格式的圖片無法透明。用imagecreate函數(shù)創(chuàng)建畫布可以解決這個(gè)問題,但是縮放出來的圖片色數(shù)太少了
*
*
*type值:
* (1):代表使用圖片縮放功能,此時(shí),$value1代表縮放后圖片的寬度,$value2代表縮放后圖片的高度
* (2):代表使用圖片裁剪功能,此時(shí),$value1代表裁剪開始點(diǎn)的坐標(biāo),例:從原點(diǎn)開始即是“0,0”前面是x軸后面是y軸,中間用,分隔,$value2代表裁剪的寬度和高度,同樣也是“20,20”的形式使用
* (3):代表使用加圖片水印功能,此時(shí),$value1代表水印圖片的文件名,$value2代表水印在圖片中的位置,有10值個(gè)可以選,1代表左上,2代表左中,3代表左右,4代表中左,5代表中中,6代表中右,7代表下做,8代表下中,9代表下右,0代表隨機(jī)位置
*
*/
class image{
private $types; //使用的功能編號(hào),1為圖片縮放功能 2為圖片裁剪功能 3,為圖片加圖片水印功能
private $imgtype;//圖片的格式
private $image; //圖片資源
private $width;//圖片寬度
private $height;//圖片高度
private $value1;//根據(jù)所傳type值的不同,$value1分別代表不同的值
private $value2;//根據(jù)所傳type值的不同,$value2分別代表不同的值
private $endaddress;//輸出后的地址+文件名
function __construct($imageaddress, $types, $value1="", $value2="", $endaddress){
$this->types=$types;
$this->image=$this->imagesources($imageaddress);
$this->width=$this->imagesizex();
$this->height=$this->imagesizey();
$this->value1=$value1;
$this->value2=$value2;
$this->endaddress=$endaddress;
}
function outimage(){ //根據(jù)傳入type值的不同,輸出不同的功能
switch($this->types){
case 1:
$this->scaling();
break;
case 2:
$this->clipping();
break;
case 3:
$this->imagewater();
break;
default:
return false;
}
}
private function imagewater(){ //加圖片水印功能
//用函數(shù)獲取水印文件的長和寬
$imagearrs=$this->getimagearr($this->value1);
//調(diào)用函數(shù)計(jì)算出水印加載的位置
$positionarr=$this->position($this->value2, $imagearrs[0], $imagearrs[1]);
//加水印
imagecopy($this->image, $this->imagesources($this->value1), $positionarr[0], $positionarr[1], 0, 0, $imagearrs[0], $imagearrs[1]);
//調(diào)用輸出方法保存
$this->output($this->image);
}
private function clipping(){ //圖片裁剪功能
//將傳進(jìn)來的值分別賦給變量
list($src_x, $src_y)=explode(",", $this->value1);
list($dst_w, $dst_h)=explode(",", $this->value2);
if($this->width < $src_x+$dst_w || $this->height < $src_y+$dst_h){ //這個(gè)判斷就是限制不能截取到圖片外面去
return false;
}
//創(chuàng)建新的畫布資源
$newimg=imagecreatetruecolor($dst_w, $dst_h);
//進(jìn)行裁剪
imagecopyresampled($newimg, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h);
//調(diào)用輸出方法保存
$this->output($newimg);
}
private function scaling(){ //圖片縮放功能
//獲取等比縮放的寬和高
$this-> proimagesize();
//根據(jù)參數(shù)進(jìn)行縮放,并調(diào)用輸出函數(shù)保存處理后的文件
$this->output($this->imagescaling());
}
private function imagesources($imgad){ //獲取圖片類型并打開圖像資源
$imagearray=$this->getimagearr($imgad);
switch($imagearray[2]){
case 1://gif
$this->imgtype=1;
$img=imagecreatefromgif($imgad);
break;
case 2://jpeg
$this->imgtype=2;
$img=imagecreatefromjpeg($imgad);
break;
case 3://png
$this->imgtype=3;
$img=imagecreatefrompng($imgad);
break;
default:
return false;
}
return $img;
}
private function imagesizex(){ //獲得圖片寬度
return imagesx($this->image);
}
private function imagesizey(){ //獲取圖片高度
return imagesy($this->image);
}
private function proimagesize(){ //計(jì)算等比縮放的圖片的寬和高
if($this->value1 && ($this->width < $this->height)) { //等比縮放算法
$this->value1=round(($this->value2/ $this->height)*$this->width);
}else{
$this->value2=round(($this->value1/ $this->width) * $this->height);
}
}
private function imagescaling(){//圖像縮放功能,返回處理后的圖像資源
$newimg=imagecreatetruecolor($this->value1, $this->value2);
$tran=imagecolortransparent($this->image);//處理透明算法
if($tran >= 0 && $tran < imagecolorstotal($this->image)){
$tranarr=imagecolorsforindex($this->image, $tran);
$newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']);
imagefill($newimg, 0, 0, $newcolor);
imagecolortransparent($newimg, $newcolor);
}
imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this->width, $this->height);
return $newimg;
}
private function output($image){//輸出圖像
switch($this->imgtype){
case 1:
imagegif($image, $this->endaddress);
break;
case 2:
imagejpeg($image, $this->endaddress);
break;
case 3:
imagepng($image, $this->endaddress);
break;
default:
return false;
}
}
private function getimagearr($imagesou){//返回圖像屬性數(shù)組方法
return getimagesize($imagesou);
}
private function position($num, $width, $height){//根據(jù)傳入的數(shù)字返回一個(gè)位置的坐標(biāo),$width和$height分別代表插入圖像的寬和高
switch($num){
case 1:
$positionarr[0]=0;
$positionarr[1]=0;
break;
case 2:
$positionarr[0]=($this->width-$width)/2;
$positionarr[1]=0;
break;
case 3:
$positionarr[0]=$this->width-$width;
$positionarr[1]=0;
break;
case 4:
$positionarr[0]=0;
$positionarr[1]=($this->height-$height)/2;
break;
case 5:
$positionarr[0]=($this->width-$width)/2;
$positionarr[1]=($this->height-$height)/2;
break;
case 6:
$positionarr[0]=$this->width-$width;
$positionarr[1]=($this->height-$height)/2;
break;
case 7:
$positionarr[0]=0;
$positionarr[1]=$this->height-$height;
break;
case 8:
$positionarr[0]=($this->width-$width)/2;
$positionarr[1]=$this->height-$height;
break;
case 9:
$positionarr[0]=$this->width-$width;
$positionarr[1]=$this->height-$height;
break;
case 0:
$positionarr[0]=rand(0, $this->width-$width);
$positionarr[1]=rand(0, $this->height-$height);
break;
}
return $positionarr;
}
function __destruct(){
imagedestroy($this->image);
}
}
?>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 姚安县| 西青区| 南通市| 翼城县| 寿阳县| 同心县| 铜山县| 雷波县| 威信县| 哈尔滨市| 蕉岭县| 丽江市| 光山县| 安西县| 阿合奇县| 霍州市| 大宁县| 五指山市| 赤水市| 响水县| 乐山市| 桦甸市| 焉耆| 万安县| 泉州市| 济阳县| 固原市| 新晃| 湾仔区| 徐汇区| 阿尔山市| 茌平县| 万盛区| 稻城县| 安阳县| 溧水县| 宁化县| 霍邱县| 曲水县| 沙湾县| 亳州市|