我們對圖片的處理主要是添加水印和等比縮放,在PHP中,封裝一個類來實現(xiàn)兩個功能。

專注于為中小企業(yè)提供網(wǎng)站設計制作、網(wǎng)站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)玉州免費做網(wǎng)站提供優(yōu)質的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉變。
源代碼如下:
<?php
/**
*圖片處理
*/
class Image
{
//路徑
private $path = './upload/';
//隨機文件名
private $isRandName;
//初始化成員方法
public function __construct($path = null , $r = true)
{
if (!is_null($path)) {
$this->path = rtrim($path,'/').'/';
}
$this->isRandName = $r;
}
//water水印的方法
//源(圖片 $dst) 目標(水印 $src) 位置(9宮格) 前綴($prefix) 透明度($tmd )
public function water($dst,$src,$pos = 9,$prefix = 'wa_', $tmd = 100)
{
//判斷文件路徑是否存在
$src = $this->path . $src;
if (!file_exists($dst) || !file_exists($src)) {
exit('圖片或者水印不存在');
}
//獲取圖像(圖片和水印)的相關信息
$dstInfo = self::getImageInfo($dst);
$srcInfo = self::getImageInfo($src);
//var_dump($dstInfo);
//判斷寬高是否超過了目標圖片的寬高
if (!$this->_checkSize($dstInfo,$srcInfo)) {
exit('水印圖片的寬、高不合法');
}
//擺放位置 1 2 3 4 5 6 7 8 9 九宮格(3行3列)
$postion = self::getPostion($dstInfo,$srcInfo,$pos);
//打開圖片
$dstRes = self::openImage($dst,$dstInfo);
$srcRes = self::openImage($src,$srcInfo);
//將兩個圖片合并在一起 通過兩張圖片信息將圖片合并在一起 需要自定義一個方法
$newRes = $this->_mergeImage($dstRes,$srcRes,$postion,$dstInfo,$srcInfo,$tmd);
//判斷是否允許隨機命名【保存之前】
if ($this->isRandName) {
//路徑 前綴 產生id . 后綴
//uniqid() 獲取一個帶前綴、基于當前時間微秒數(shù)的唯一ID
$path = $this->path.$prefix . uniqid(). '.' .$dstInfo['subfix'];
} else {
//路徑 前綴 文件原名
$path = $this->path.$prefix . $dstInfo['basename'];
}
//保存圖片
self::saveImage($newRes,$path,$dstInfo);
//銷毀資源
p_w_picpathdestroy($dstRes);
p_w_picpathdestroy($srcRes);
//返回路徑
}
//等比縮放
//源圖片 寬 高 前綴
public function thump($dst,$width,$height,$prefix = 'thump_')
{
//判斷文件是否存在
if (!file_exists($dst)) {
exit('文件路徑不存在');
}
//獲取圖像的信息 沒有信息就退出
$info = self::getImageInfo($dst);
//得到一個新的尺寸
$newSize = self::getNewSize($width,$height,$info);
//打開資源
$res = self::openImage($dst,$info);
//等比縮放這個資源 處理gif背景變黑的問題
$newRes = self::kidOfImage($res,$newSize,$info);
//保存
$path = $this->path.$prefix.$info['basename'];
self::saveImage($newRes,$path,$info);
//銷毀資源
p_w_picpathdestroy($newRes);
//返回路徑
return $path;
}
//等比縮放處理
private static function kidOfImage($srcImg, $size, $imgInfo)
{
$newImg = p_w_picpathcreatetruecolor($size["width"], $size["height"]);
$otsc = p_w_picpathcolortransparent($srcImg);
if ( $otsc >= 0 && $otsc < p_w_picpathcolorstotal($srcImg)) {
$transparentcolor = p_w_picpathcolorsforindex( $srcImg, $otsc );
$newtransparentcolor = p_w_picpathcolorallocate(
$newImg,
$transparentcolor['red'],
$transparentcolor['green'],
$transparentcolor['blue']
);
p_w_picpathfill( $newImg, 0, 0, $newtransparentcolor );
p_w_picpathcolortransparent( $newImg, $newtransparentcolor );
}
p_w_picpathcopyresized( $newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"] );
p_w_picpathdestroy($srcImg);
return $newImg;
}
//得到一個新的尺寸
private static function getNewSize($width, $height, $imgInfo)
{
$size["width"] = $imgInfo["width"]; //將原圖片的寬度給數(shù)組中的$size["width"]
$size["height"] = $imgInfo["height"]; //將原圖片的高度給數(shù)組中的$size["height"]
if($width < $imgInfo["width"]) {
$size["width"] = $width; //縮放的寬度如果比原圖小才重新設置寬度
}
if ($width < $imgInfo["height"]) {
$size["height"] = $height; //縮放的高度如果比原圖小才重新設置高度
}
if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]) {
$size["height"] = round($imgInfo["height"] * $size["width"] / $imgInfo["width"]);
} else {
$size["width"] = round($imgInfo["width"] * $size["height"] / $imgInfo["height"]);
}
return $size;
}
//獲取圖片的相關信息
public static function getImageInfo($path)
{
$data = [];
//獲取圖片大小
$info = getp_w_picpathsize($path);
//var_dump($info);
//根據(jù)打印出來的信息 將鍵所對應的值(文件的大小)賦值給data的數(shù)組中
$data['width'] = $info[0];
$data['height'] = $info[1];
$data['mime'] = $info['mime'];
//獲取路徑 后綴 文件名信息
$path = pathinfo($path);
//var_dump($path);die;
//根據(jù)打印出來的信息 將將鍵所對應的值(路徑和文件名)賦值給data的數(shù)組中
$data['basename'] = $path['basename'];
$data['subfix'] = $path['extension'];
return $data;
}
//檢查圖片和水印的寬高
//將圖片的寬高和水印的寬高進行比較
private function _checkSize($dstInfo,$srcInfo)
{
//水印的寬應該小于圖片的寬度或者水印的高度應該小于圖片的高度 ,只要其中一個不滿足就不能繼續(xù)
if ($dstInfo['width'] < $srcInfo['width'] || $dstInfo['height'] < $srcInfo['height']) {
return false;
}
return true;
}
//位置處理
public static function getPostion($dstInfo,$srcInfo,$pos)
{
switch ($pos) {
case 1:
$x = 0;
$y = 0;
break;
case 2:
$x = ceil(($dstInfo['width'] - $srcInfo['width']) / 2 );
$y = 0;
break;
case 3:
$x = $dstInfo['width'] - $srcInfo['width'];
$y = 0;
break;
case 4:
$x = 0;
$y = ceil(($dstInfo['height'] - $srcInfo['height']) / 2 );
break;
case 5:
$x = ceil(($dstInfo['width'] - $srcInfo['width']) / 2 );
$y = ceil(($dstInfo['height'] - $srcInfo['height']) / 2 );
break;
case 6:
$x = $dstInfo['width'] - $srcInfo['width'];
$y = ceil(($dstInfo['height'] - $srcInfo['height']) / 2 );
break;
case 7:
$x = 0;
$y = $dstInfo['height'] - $srcInfo['height'];
break;
case 8:
$x = ceil(($dstInfo['width'] - $srcInfo['width']) / 2 );
$y = $dstInfo['height'] - $srcInfo['height'];
break;
case 9:
$x = $dstInfo['width'] - $srcInfo['width'];
$y = $dstInfo['height'] - $srcInfo['height'];
break;
}
return ['x' => $x ,'y' =>$y];
}
//打開圖片
//根據(jù)圖片的類型打開相應的圖片資源
private function openImage($path,$info)
{
switch ($info['mime']) {
case 'p_w_picpath/png':
case 'p_w_picpath/x-png':
$res = p_w_picpathcreatefrompng($path);
break;
case 'p_w_picpath/jpeg':
case 'p_w_picpath/jpg':
case 'p_w_picpath/pjpeg':
$res = p_w_picpathcreatefromjpeg($path);
break;
case 'p_w_picpath/gif':
$res = p_w_picpathcreatefromgif($path);
break;
case 'p_w_picpath/wbmp':
case 'p_w_picpath/bmp':
$res = p_w_picpathcreatefromwbmp($path);
break;
}
//var_dump($res);die;
return $res;
}
//合并圖片 p_w_picpathcopymerge(圖片,水印,圖片坐標x,圖片坐標y,水印坐標x,水印坐標y,透明度)
private function _mergeImage($dstRes,$srcRes,$postion,$dstInfo,$srcInfo,$tmd)
{
p_w_picpathcopymerge($dstRes,$srcRes,$postion['x'],$postion['y'],0,0,$srcInfo['width'],$srcInfo['height'],$tmd);
return $dstRes;
}
//保存圖片處理方法
//參數(shù):需要保存的圖片資源,保存的路徑,保存的信息
public static function saveImage($res,$path,$info)
{
//根據(jù)不同的圖片類型選擇不同的函數(shù)進行保存
switch ($info['mime']) {
case 'p_w_picpath/png':
case 'p_w_picpath/x-png':
p_w_picpathpng($res,$path);
break;
case 'p_w_picpath/jpeg':
case 'p_w_picpath/jpg':
case 'p_w_picpath/pjpeg':
p_w_picpathjpeg($res,$path);
break;
case 'p_w_picpath/gif':
p_w_picpathgif($res,$path);
break;
case 'p_w_picpath/wbmp':
case 'p_w_picpath/bmp':
p_w_picpathwbmp($res,$path);
break;
}
}
}測試代碼:
$img = new Image();
/*
$img->water('ly.png','logo.gif',3);
$img->water('ly.png','logo.gif',4);*/
$img->thump('ly.png',100,100,'l1_');
本文題目:PHP中面向對象的圖片處理類
網(wǎng)站鏈接:http://chinadenli.net/article20/jiijjo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、自適應網(wǎng)站、關鍵詞優(yōu)化、域名注冊、網(wǎng)頁設計公司、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)