Skip to content

Commit 80e1778

Browse files
committed
Finish water mark function
1 parent eb1a932 commit 80e1778

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

ImageResize.php

+50-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,55 @@ private function generate(){
6363

6464
}
6565

66+
public function addImageMark($path, $width = 50, $height = 50, $positionX = "right", $positionY = "bottom", $margin = 10){
67+
68+
$im = (new ImageResize($path, $width, $height))->getImageResource();
69+
70+
if (strtolower($positionX) == "right"){
71+
$dstX = $this->width - $width - $margin;
72+
} else {
73+
$dstX = $margin;
74+
}
75+
76+
if (strtolower($positionY) == "bottom"){
77+
$dstY = $this->height - $height - $margin;
78+
} else {
79+
$dstY = $margin;
80+
}
81+
82+
imagecopy($this->newResource, $im, $dstX, $dstY, 0, 0, $width, $height);
83+
84+
}
85+
86+
public function addTextMark($text, $fontSize = 5, $color = "#0000ff", $positionX = "right", $positionY = "bottom", $margin = 10){
87+
88+
$textWidth = imagefontwidth($fontSize) * strlen($text);
89+
$textHeight = imagefontheight($fontSize);
90+
91+
if (strtolower($positionX) == "right"){
92+
$dstX = $this->width - $textWidth - $margin;
93+
} else {
94+
$dstX = $margin;
95+
}
96+
97+
if (strtolower($positionY) == "bottom"){
98+
$dstY = $this->height - $textHeight - $margin;
99+
} else {
100+
$dstY = $margin;
101+
}
102+
103+
$textColor = imagecolorallocate($this->newResource, hexdec($color[1].$color[2]), hexdec($color[3].$color[4]), hexdec($color[5].$color[6]));
104+
105+
imagestring ( $this->newResource , $fontSize, $dstX , $dstY , $text , $textColor );
106+
}
107+
108+
public function getImageResource()
109+
{
110+
return $this->newResource;
111+
}
112+
113+
114+
66115
public function save($path, $format = null){
67116

68117
if (is_null($format)) {
@@ -104,7 +153,4 @@ public function render(){
104153
break;
105154
}
106155
}
107-
}
108-
109-
$a = new ImageResize('test.jpg',170,128);
110-
$a->save('a.png');
156+
}

index.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
require_once 'ImageResize.php';
3+
4+
$a = new ImageResize('Windows.jpg',800,600);
5+
$a->addTextMark("zzfly.net", 5, "#cccccc", "right", "bottom",10);
6+
$a->addImageMark('Windows.jpg',100,80,"right","bottom",50);
7+
$a->render();

0 commit comments

Comments
 (0)