php實(shí)現(xiàn)水印圖功能
來(lái)源:易賢網(wǎng) 閱讀:738 次 日期:2015-04-15 15:19:31
溫馨提示:易賢網(wǎng)小編為您整理了“php實(shí)現(xiàn)水印圖功能”,方便廣大網(wǎng)友查閱!

php實(shí)現(xiàn)水印圖功能.

測(cè)試水印的文件代碼:

<?php

require( './WaterMark.class.php' );

$water = new WaterMark( './images/psb.jpg', './images/200(1).jpg' );

$water->Exec( './images/w.jpg', 'center-center' );

?>

水印功能的文件代碼:

<?php

/**

* Add water stain to specified image

* @author pankai<>

* @date 2013-09-27

*/

class WaterMark {

/* Image source path */

private $_src_path = NULL;

private $_image_source = NULL;

/* Water-Stain image source path */

private $_water_path = NULL;

private $_water_source = NULL;

private $_extension = NULL;

public function __construct( $_src_path, $_water_path ) {

$this->_src_path = $_src_path;

$this->_water_path = $_water_path;

}

/**

* Get image source form image designated path

* @param $_img_path

* @return resource

*/

private function getImageSource( $_img_path ) {

$_path = explode( ".", strtolower( $_img_path ) );

$_bak = strtoupper( $_path[ sizeof( $_path ) - 1 ] );

$_img_source = NULL;

$this->_extension = $_bak;

switch( $_bak ) {

case "GIF":

$_img_source = @imagecreatefromgif( $_img_path );

break;

case "JPG":

case "JPEG":

$_img_source = @imagecreatefromjpeg( $_img_path );

break;

case "PNG":

$_img_source = @imagecreatefrompng( $_img_path );

break;

case "WBMP":

$_img_source = @imagecreatefromwbmp( $_img_path );

break;

}

return $_img_source;

}

/**

* Count coordinate

* @param $direction string

* @return array

*/

private function countArgs( &$_src, &$_water, $_direction ) {

$_args = NULL;

/**

* Count the image's width and image's height

* which are used to show Water-Stain image arbitrarily

*/

$src_x = imagesx( $_src );

$src_y = imagesy( $_src );

$water_x = imagesx( $_water );

$water_y = imagesy( $_water );

switch( strtolower( $_direction ) ) {

case 'top-left':

$_args = array( 0, 0 );

break;

case 'top-center':

$_args = array( floor( ( $src_x - $water_x ) / 2 ), 0 );

break;

case 'top-right':

$_args = array( $src_x - $water_x, 0 );

break;

case 'center-left':

$_args = array( 0, floor( ( $src_y - $water_y ) / 2 ) );

break;

case 'center-center':

$_args = array( floor( ( $src_x - $water_x ) / 2 ), floor( ( $src_y - $water_y ) / 2 ) );

break;

case 'center-right':

$_args = array( $src_x - $water_x, floor( ( $src_y - $water_y ) / 2 ) );

break;

case 'bottom-left':

$_args = array( 0, $src_y - $water_y );

break;

case 'bottom-center':

$_args = array( floor( ( $src_x - $water_x ) / 2 ), $src_y - $water_y );

break;

case 'bottom-right':

$_args = array( $src_x - $water_x, $src_y - $water_y );

break;

}

return $_args;

}

/**

* The main programmer: copy Water-Stain image source into specified image source,

* then save this into appointed path

* @param $dst_name

* @return bool

*/

public function Exec( $dst_name, $direction = 'bottom-right' ) {

$this->_image_source = &$this->getImageSource( $this->_src_path );

$this->_water_source = &$this->getImageSource( $this->_water_path );

$water_x = imagesx( $this->_water_source );

$water_y = imagesy( $this->_water_source );

$_args = $this->countArgs( $this->_image_source, $this->_water_source, $direction );

$_copy = imagecopy( $this->_image_source, $this->_water_source,

abs( $_args[ 0 ] ), abs( $_args[ 1 ] ), 0, 0, $water_x, $water_y );

$_out = true;

/* imagejpeg( $this->img_dst, $dst_name, 100 ); */

switch ( $this->_extension ) {

case 'GIF':

$_out = imagegif( $this->_image_source, $dst_name );

break;

case 'JPG':

case 'JPEG':

$_out = imagejpeg( $this->_image_source, $dst_name, 100 );

break;

case 'PNG':

$_out = imagepng( $this->_image_source, $dst_name );

break;

case 'WBMP':

$_out = imagewbmp( $this->_image_source, $dst_name );

}

return $_copy && $_out;

}

}

?>

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php實(shí)現(xiàn)水印圖功能
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)