-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwatermark-image.php
executable file
·44 lines (32 loc) · 1.18 KB
/
watermark-image.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
#!/usr/bin/env php
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use Jcupitt\Vips;
#Vips\Config::setLogger(new Vips\DebugLogger());
if (count($argv) != 4) {
echo("usage: ./watermark.php input-image output-image watermark-image\n");
exit(1);
}
// we can stream the main image
$image = Vips\Image::newFromFile($argv[1], ['access' => 'sequential']);
// we'll read the watermark image many times, so we need random access for this
$watermark = Vips\Image::newFromFile($argv[3]);
// the watermark image needs to have an alpha channel
if (!$watermark->hasAlpha() || $watermark->bands != 4) {
echo("watermark image is not RGBA\n");
exit(1);
}
// make the watermark semi-transparent
$watermark = $watermark->multiply([1, 1, 1, 0.3])->cast("uchar");
// repeat the watermark to the size of the image
$watermark = $watermark->replicate(
1 + $image->width / $watermark->width,
1 + $image->height / $watermark->height
);
$watermark = $watermark->crop(0, 0, $image->width, $image->height);
// composite the watermark over the main image
$image = $image->composite2($watermark, 'over');
$image->writeToFile($argv[2]);
$image = null;
$watermark = null;
Vips\FFI::shutDown();