登录  /  注册
首页 > php教程 > php手册 > 正文

php水印图类

php中文网
发布: 2016-06-06 19:31:45
原创
1325人浏览过

图片加上标签 无 ?phprequire( './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 pankai53091

图片加上标签
<?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<530911044@qq.com>
 * @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;	
	}
}
?>
登录后复制
智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号