-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatermark.php
60 lines (42 loc) · 1.79 KB
/
watermark.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
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
session_start();
header('Content-Type: image/png');
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 20 day")));
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
// if the browser has a cached version of this image, send 304
header('Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],true,304);
exit;
} else {
// no cached image, generate image with watermark
include("functions.php");
include("config.php");
$im_path = $_GET[img];
$im = ImageCreateFromPNG ($im_path);
// basic settings for watermark --> maybe move to config file
$rgb = hex2rgb($font_color);
$textcolor = ImageColorAllocate ($im, $rgb['r'], $rgb['g'], $rgb['b']);
// font size as 2% of image width
$size = imagesx ($im)*0.02;
$angle = 0;
$fontfile = "./".$font_file;
$text = "© ".strftime("%Y").", ".$author;
// compute dimensions of watermark
$dimensions = imagettfbbox($size, $angle, $fontfile, $text);
// compute width
$textWidth = abs($dimensions[4] - $dimensions[6]);
$x = imagesx($im) - $textWidth;
// compute heigth
$textHeight = abs($dimensions[5] - $dimensions[3]);
$y = imagesy($im) - $textHeight;
// create watermark
ImageTTFText ($im, $size, 0, $x-$size, $y+$size/2, $textcolor, $fontfile, $text);
header('Content-type: image/png');
header("Content-disposition: inline; filename=".str_replace(" ", "_", basename($im_path)));
// image output
ImagePNG ($im);
// cleanup
ImageDestroy ($im);
}
?>