php產生浮水印,包括位置和透明度_PHP教程

來源:互聯網
上載者:User

/*

給大家推薦一下!主要包括圖片的裁剪,縮放,加浮水印等。下面是一些簡單的使用方法。

include("imageclass.php"); //這裡注意imageclass.php的路徑

$imgs=new image();

$imgs->param($tempFile);

if(!$imgs->thumb($targetFile,600,500))die('--映像上傳失敗!'); //對圖片進行縮放

$imgs->water($targetFile,$water,$pos=0,$pct=50);主要包括兩個參數,$targetFile為已經上傳到伺服器的檔案路徑,$water為浮水印圖片,$pos為浮水印圖片位置,$pct為透明度;

$pos位置說明:

0://隨機

1://1為頂端居左

2://2為頂端置中

3://3為頂端居右

4://4為中部居左

5://5為中部置中

6://6為中部居右

7://7為底端居左

8://8為底端置中

9://9為底端居右

將//裡邊的內容儲存在imageclass.php裡邊,就可以調用了。

///////////////////////////////////////////////////////////////

*/

?>

/* +-------------------------------------------------------------+

* | Copyright (c) 2008-2009 Diqiye.Com All rights reserved.

* +-------------------------------------------------------------+

* | Info : 影像處理類

* +-------------------------------------------------------------+

*/

class image {

// 當前圖片

protected $img;

// 映像types 對應表

protected $types = array(

1 => 'gif',

2 => 'jpg',

3 => 'png',

6 => 'bmp'

);

// image

public function __construct($img=''){

!$img && $this->param($img);

}

// Info

public function param($img){

$this->img = $img;

return $this;

}

// imageInfo

public function getImageInfo($img){

$info = @getimagesize($img);

if(isset($this->types[$info[2]])){

$info['ext'] = $info['type'] = $this->types[$info[2]];

} else{

$info['ext'] = $info['type'] = 'jpg';

}

$info['type'] == 'jpg' && $info['type'] = 'jpeg';

$info['size'] = @filesize($img);

return $info;

}

// thumb(新圖地址, 寬, 高, 裁剪, 允許放大)

public function thumb($filename,$new_w=160,$new_h=120,$cut=0,$big=0){

// 擷取原圖資訊

$info = $this->getImageInfo($this->img);

if(!empty($info[0])) {

$old_w = $info[0];

$old_h = $info[1];

$type = $info['type'];

$ext = $info['ext'];

unset($info);

// 如果原圖比縮圖小 並且不允許放大

if($old_w < $new_h && $old_h < $new_w && !$big){

return false;

}

// 裁剪圖片

if($cut == 0){ // 等比列

$scale = min($new_w/$old_w, $new_h/$old_h); // 計算縮放比例

$width = (int)($old_w*$scale); // 縮圖尺寸

$height = (int)($old_h*$scale);

$start_w = $start_h = 0;

$end_w = $old_w;

$end_h = $old_h;

} elseif($cut == 1){ // center center 裁剪

$scale1 = round($new_w/$new_h,2);

$scale2 = round($old_w/$old_h,2);

if($scale1 > $scale2){

$end_h = round($old_w/$scale1,2);

$start_h = ($old_h-$end_h)/2;

$start_w = 0;

$end_w = $old_w;

} else{

$end_w = round($old_h*$scale1,2);

$start_w = ($old_w-$end_w)/2;

$start_h = 0;

$end_h = $old_h;

}

$width = $new_w;

$height= $new_h;

} elseif($cut == 2){ // left top 裁剪

$scale1 = round($new_w/$new_h,2);

$scale2 = round($old_w/$old_h,2);

if($scale1 > $scale2){

$end_h = round($old_w/$scale1,2);

$end_w = $old_w;

} else{

$end_w = round($old_h*$scale1,2);

$end_h = $old_h;

}

$start_w = 0;

$start_h = 0;

$width = $new_w;

$height= $new_h;

}

// 載入原圖

$createFun = 'ImageCreateFrom'.$type;

$oldimg = $createFun($this->img);

// 建立縮圖

if($type!='gif' && function_exists('imagecreatetruecolor')){

$newimg = imagecreatetruecolor($width, $height);

} else{

$newimg = imagecreate($width, $height);

}

// 複製圖片

if(function_exists("ImageCopyResampled")){

ImageCopyResampled($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w,$end_h);

} else{

ImageCopyResized($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w,$end_h);

}

// 對jpeg圖形設定隔行掃描

$type == 'jpeg' && imageinterlace($newimg,1);

// 產生圖片

$imageFun = 'image'.$type;

!@$imageFun($newimg,$filename,100) && die('儲存失敗!檢查目錄是否存在並且可寫?');

ImageDestroy($newimg);

ImageDestroy($oldimg);

return $filename;

}

return false;

}

// water(儲存地址,浮水印圖片,浮水印位置,透明度)

public function water($filename,$water,$pos=0,$pct=50){

// 載入浮水印圖片

$info = $this->getImageInfo($water);

if(!empty($info[0])){

$water_w = $info[0];

$water_h = $info[1];

$type = $info['type'];

$fun = 'imagecreatefrom'.$type;

$waterimg = $fun($water);

} else{

return false;

}

// 載入背景圖片

$info = $this->getImageInfo($this->img);

if(!empty($info[0])){

$old_w = $info[0];

$old_h = $info[1];

$type = $info['type'];

$fun = 'imagecreatefrom'.$type;

$oldimg = $fun($this->img);

} else{

return false;

}

// 剪下浮水印

$water_w >$old_w && $water_w = $old_w;

$water_h >$old_h && $water_h = $old_h;

// 浮水印位置

switch($pos){

case 0://隨機

$posX = rand(0,($old_w - $water_w));

$posY = rand(0,($old_h - $water_h));

break;

case 1://1為頂端居左

$posX = 0;

$posY = 0;

break;

case 2://2為頂端置中

$posX = ($old_w - $water_w) / 2;

$posY = 0;

break;

case 3://3為頂端居右

$posX = $old_w - $water_w;

$posY = 0;

break;

case 4://4為中部居左

$posX = 0;

$posY = ($old_h - $water_h) / 2;

break;

case 5://5為中部置中

$posX = ($old_w - $water_w) / 2;

$posY = ($old_h - $water_h) / 2;

break;

case 6://6為中部居右

$posX = $old_w - $water_w;

$posY = ($old_h - $water_h) / 2;

break;

case 7://7為底端居左

$posX = 0;

$posY = $old_h - $water_h;

break;

case 8://8為底端置中

$posX = ($old_w - $water_w) / 2;

$posY = $old_h - $water_h;

break;

case 9://9為底端居右

$posX = $old_w - $water_w;

$posY = $old_h - $water_h;

break;

default: //隨機

$posX = rand(0,($old_w - $water_w));

$posY = rand(0,($old_h - $water_h));

break;

}

// 設定映像的混色模式

imagealphablending($oldimg, true);

// 添加浮水印

imagecopymerge($oldimg, $waterimg, $posX, $posY, 0, 0, $water_w,$water_h,$pct);

$fun = 'image'.$type;

!@$fun($oldimg, $filename,100) && die('儲存失敗!檢查目錄是否存在並且可寫?');

imagedestroy($oldimg);

imagedestroy($waterimg);

return $filename;

}

}

$imgs=new image();

$tempFile = "./images/hdb.jpg";

$water = "./images/bamiluo.jpg";

$imgs->param($tempFile);

//if(!$imgs->thumb($targetFile,600,500))die('fail');

$imgs->water($targetFile,$water,0,10);

echo "aaa";

?>

摘自:ws07_byyy的專欄

http://www.bkjia.com/PHPjc/478568.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478568.htmlTechArticle?php /* 給大家推薦一下!主要包括圖片的裁剪,縮放,加浮水印等。下面是一些簡單的使用方法。 include(imageclass.php); //這裡注意imageclass.php的...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.