php给图片添加水印,并且按照一定的范围平铺。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public function watermark($imagePath){
//$imagePath是原图片路径。服务器绝对路径。
list($watermarkSrcWidth, $watermarkSrcHeight, $watermarkFileType, ) = getimagesize($imagePath);
$this->_fileType = $imgFileType;
$im = call_user_func($this->_getCallback(
'create',
$watermarkFileType,
'Unsupported watermark image format.'
), $imagePath);
//水印图片
$watermarkImage = Mage::getBaseDir() . DS . 'watermark-120x120.png';
// $newPath = Mage::getBaseDir('media') . DS .'new.jpg';

$watermark = imagecreatefrompng($watermarkImage);
// $im = imagecreatefromjpeg($imagePath);
imagesettile($im, $watermark);
// Make the image repeat
//在原图上画一个矩形,确定坐标后,平铺水印图片.
imagefilledrectangle($im, 0, 0, 480, 480, IMG_COLOR_TILED);
$functionParameters = array();
$functionParameters[] = $image;
$functionParameters[] = $newPath;
call_user_func_array($this->_getCallback('output'), $functionParameters);
imagedestroy($im);
imagedestroy($watermark);
}

private static $_callbacks = array(
IMAGETYPE_GIF => array('output' => 'imagegif', 'create' => 'imagecreatefromgif'),
IMAGETYPE_JPEG => array('output' => 'imagejpeg', 'create' => 'imagecreatefromjpeg'),
IMAGETYPE_PNG => array('output' => 'imagepng', 'create' => 'imagecreatefrompng'),
IMAGETYPE_XBM => array('output' => 'imagexbm', 'create' => 'imagecreatefromxbm'),
IMAGETYPE_WBMP => array('output' => 'imagewbmp', 'create' => 'imagecreatefromxbm'),
);
private function _getCallback($callbackType, $fileType = null, $unsupportedText = 'Unsupported image format.')
{
if (null === $fileType) {
$fileType = $this->_fileType;
}
if (empty(self::$_callbacks[$fileType])) {
throw new Exception($unsupportedText);
}
if (empty(self::$_callbacks[$fileType][$callbackType])) {
throw new Exception('Callback not found.');
}
return self::$_callbacks[$fileType][$callbackType];
}

类似这种效果

1457490851760692

坚持原创技术分享,您的支持将鼓励我继续创作!