<?php class GDImage { /** * 源图片信息 * * @var <array> */ private $_imgInFo; /** * 水印图片信息 * * @var <array> */ private $_waterImgInFo; /** * 1:添加水印图片 * * @var <int> */ private $_waterType = 1; /** * 水印文字 * * @var <string> */ private $_waterStr; /** * 水印图片路径 */ private $_waterPic; /** * 创建后水印图片路径 */ private $_newImgPath = 'new_img'; /** * 位置 X * * @var <int> */ private $_positionX = 300; /** * 位置 Y * * @var <int> */ private $_positionY = 100; public function __construct() { } public function setPositionXY ($x,$y) { $this->_positionX = $x; $this->_positionY = $y; } /** * 生成水印文字 * * @param <int> $type */ public function CreateWaterString($string,$fileImg) { $this->_waterType = 0; $this->_waterStr = $string; $this->createWaterImage($fileImg); } /** * 生成水印图片 * * @param <string> $path */ public function CreateWaterPic($waterPic,$fileImg) { $this->_waterType = 1; $this->_waterPic = $waterPic; $this->createWaterImage($fileImg); } /** * 设置创建完成后图片保存的路径 * * @param <string> $path */ public function setNewImgPath($newSavePath) { $this->_newImgPath = $newSavePath; } /** * 创建水印图片 * * @return <type> */ protected function createWaterImage($fileImg) { $source_img = $this->createImgFromFile($fileImg); if(empty ($source_img)) { return false; } $file_name = $this->getFileName($fileImg); if($this->_waterType === 0) { $bg = imagecolorallocate($source_img, 206, 123, 0); imagestring($source_img, 32, $this->_imgInFo[0] - $this->_positionX, $this->_imgInFo[1] - $this->_positionY, $this->_waterStr, $bg); if($this->createImageFactory($source_img,$file_name) === false) return false; }else { $water_img = $this->createImgFromFile($this->_waterPic, true); imagecopy($source_img, $water_img, $this->_imgInFo[0] - $this->_positionX, $this->_imgInFo[1] - $this->_positionY, 0, 0, $this->_waterImgInFo[0], $this->_waterImgInFo[1]); imagedestroy($water_img); if($this->createImageFactory($source_img,$file_name) === false) return false; } } /** * 创建GD图像 * * $bl false时 处理源图片 true 处理水印图片 * * @param string file_img * @param boll bl * @return resource */ protected function createImgFromFile($file_img , $bl = false) { if(!file_exists($file_img) || !is_readable($file_img)) { return null; } if($bl === false) { $this->_imgInFo = getimagesize($file_img); }else{ $this->_waterImgInFo = getimagesize($file_img); } switch ($this->_imgInFo[2]) { case 1: return imagecreatefromgif($file_img); case 2: return imagecreatefromjpeg($file_img); case 3: return imagecreatefrompng($file_img); case 6: return imagecreatefrombmp($file_img); } return null; } /** * 生成图片 * * @param <type> $source_img * @param <type> $file_name * @return <resource> * */ protected function createImageFactory($source_img,$file_name) { switch ($this->_imgInFo[2]) { case 1: return imagegif($source_img,$this->_newImgPath.DIRECTORY_SEPARATOR.$file_name.'.gif'); case 2: return imagejpeg($source_img,$this->_newImgPath.DIRECTORY_SEPARATOR.$file_name.'.jpg'); case 3: return imagepng($source_img,$this->_newImgPath.DIRECTORY_SEPARATOR.$file_name.'.png'); case 6: return imagebmp($source_img,$this->_newImgPath.DIRECTORY_SEPARATOR.$file_name.'.bmp'); } return false; } /** * 获取文件名 * * @param <type> $file * @return <type> * */ protected static function getFileName($file) { $arry = array(); if(strpos($file, '/')) { $arry = explode('.', substr($file, strrpos($file,'/')+1,strlen($file))); return $arry[0]; } $arry = explode('.',$file); return $arry[0]; } } ?> 写的不是很完美,但是希望能给需要的朋友一点帮助和学习!