PHP 8 ImageMagick Library Example to Add Watermark to Image Using Imagick Extension Full Example
Jan 9, 2025
Get the full source code of application here: https://gist.github.com/gauti123456/aaef632aa603605dbb574234a4217e11
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
we'll be looking at one more library of
0:05
PHP which is built in it is similar to
0:08
GD library that we discussed in the last
0:09
video it is image
0:11
magic it basically is a extension in PHP
0:15
that you need to enable it activate it
0:18
it is built in whenever you install
0:21
PHP it comes within the core library of
0:25
PHP it is called as image magic
0:27
extension so image magic if you don't
0:30
it's open source Library if you just
0:33
type on Google this is their official
0:37
website either you can download image
0:39
magic as a command line tool or you can
0:42
even you can you can see there is a
0:45
download
0:46
option it's a program it's a binary
0:50
release for Windows it's also available
0:53
for Linux Mac as well for Windows is
0:55
really easy you need to Simply install
0:57
the program and once you install it in
0:59
the command line you can run this
1:01
program by executing magic you can see I
1:05
already installed it so for activating
1:08
inside PHP I'm using uh exam control
1:11
panel for having this Apache PHP server
1:14
running you would see
1:15
that exam if you don't know is a
1:19
software if you want to run PHP
1:21
application locally you can download
1:23
this software it's a crossplatform
1:25
software Windows Ling Mac so now to
1:29
activate this image magic
1:31
extension uh you need to go to your exam
1:34
directory which is located in the C
1:36
directory just if you see there is a
1:38
exam folder just need to go to the PHP
1:43
directory this
1:45
one and then there is an extension
1:47
directory EXT this is will be a
1:49
directory right here simply go to it and
1:52
here you will need to paste the PHP
1:55
image magic D dll file that you will
1:57
need to download from the
2:00
internet based upon your PHP version and
2:03
the image magic that you're using so in
2:06
my case I'm using 8.2 which is the
2:08
latest version of PHP at the time of
2:11
recording this video so just you need to
2:13
Simply download this
2:17
PHP image magic dll
2:22
file you can
2:25
basically go to this
2:28
website and uh it is having all the
2:32
versions depending upon which version
2:33
you are using it has all the files so
2:37
just download the file and copy in
2:39
inside that folder and then what you
2:42
need to do you need to also reference
2:44
that inside your PHP configuration so
2:47
now in the same PHP folder there is this
2:51
PHP configuration file which is
2:56
available this is a PHP configuration
2:59
file you need to open it
3:01
inside notepad or any other text editor
3:05
just search for
3:09
image so you need to first of all enable
3:12
this extension uh just type extension is
3:14
equal to image magic so just add this
3:18
right
3:23
here
3:25
so that's all that you need to do
3:30
so once you do this once you add this
3:32
extension you can
3:35
be now use the it inside the PHP script
3:38
we'll take a very simple example we have
3:41
with this logo which we need to add it
3:44
on top of the image
3:46
YouTube
3:49
logo so we we have this input image if
3:53
you
3:54
see we have this jpg image we need to
3:57
add this logo which YouTube logo we have
4:00
this PHP script if I run this now you
4:02
will see it will create this PNG file
4:05
and the logo will appear on the center
4:09
of the image you can even change the
4:12
position if I change it to top
4:15
left again run this so now if you see
4:19
the logo will appear on the top left
4:21
position so the source code that I'm
4:23
using guys is given in the description
4:25
you can get the source code and now to
4:28
get started uh I will simply write the
4:31
script step by step so I will also
4:34
explain you guys as
4:37
well so what I will
4:44
do so now the very first thing
4:50
guys we need to
4:54
actually tell where the image is
4:57
present the input image
5:00
so right in the PHP script first of all
5:02
we'll be telling uh the input
5:06
image so it is present in the same
5:09
directory the name of the file is 1.
5:12
jpg and where is the
5:15
watermark
5:16
image so it is present in the same
5:19
directory it is called
5:22
logo.png and after this you will have a
5:25
variable for output path and this will
5:28
be result do
5:32
PNG it will get
5:37
created and then we will have a variable
5:40
for storing The Watermark position by
5:42
default it will get added in the center
5:44
position so I will simply make it in
5:48
center and then uh we need to initialize
5:51
the image magic extension so we just use
5:55
the new image magic
6:01
like this and we provide the path of the
6:07
image which is the input
6:11
image so after this we also need to load
6:14
the watermark as well so we simply again
6:18
use image
6:21
magic this time we load the watermark
6:25
image
6:30
so after adding after loading all these
6:33
input image and the water mark image now
6:36
we need to actually get the dimensions
6:39
of both the watermark and the input
6:41
image so we can do this using uh a
6:44
function which is available inside image
6:48
magic so this
6:50
image get image width this is the
6:54
function which will get the width of the
6:57
image similarly to get the height we use
7:01
uh get image
7:06
height and same we will do this for
7:09
getting the watermark image width and
7:11
height so
7:13
Watermark
7:18
width image get
7:22
Watermark sorry get image WID once again
7:24
we'll use the same function
7:38
image
7:39
get image
7:47
height so after getting the dimensions
7:50
such such as the width and the height of
7:52
both input image and the water mark now
7:55
we need to Simply calculate the position
7:57
we will use the switch statement for
7:59
this so This variable of position can
8:02
have multiple values first case will be
8:04
for top left
8:06
position for that the x coordinate and
8:09
the y coordinate will
8:11
be
8:12
0 0 0
8:18
respectively then we will break
8:21
it so we will have all these cases top
8:25
left
8:26
Center bottom left bottom right
8:30
so we we have used some ma mathematics
8:33
for
8:43
this to calculate the x coordinate on
8:45
the y coordinate
9:05
so you can see
9:07
that so we calculating the x coordinate
9:10
and the y coordinate for both the all
9:11
the cases here right here we using some
9:14
mathematical formula we are using the
9:16
image width Watermark width image height
9:19
and Watermark height so you can just see
9:22
you can get all the sorts coding the
9:24
description after we calculate all these
9:26
positions based upon the variable of the
9:30
position now image magic has a function
9:33
which actually adds this Watermark to
9:35
the image this function is composite
9:39
image so it basically overlays the water
9:42
mark on top of the image and here it
9:45
takes some variables Watermark the
9:47
actual Watermark the second argument it
9:50
it takes is image
9:53
magic colon colon and here we are using
9:56
composite over
10:00
and then the x
10:02
coordinate and the y
10:04
coordinate so these are the four
10:06
arguments is take to actually draw the
10:10
watermark on top of the image and lastly
10:12
we need to Simply Save this image we
10:15
need to set the image format first of
10:17
all this we can do this convert this jpg
10:20
image to PNG webp so we need to convert
10:24
this into PNG so you will simply say PNG
10:27
and then we need to save this image so
10:29
use the Right image
10:32
function so here you'll providing the
10:35
output
10:36
path will get created and simply then we
10:39
can
10:40
simp add a echo statement just telling
10:44
the user that your output image has been
10:49
saved that's all so now if you run this
10:52
now uh you'll actually
10:55
see it is saying
11:00
undefined constant position
11:03
in line number 23 so let me
11:06
check okay this needs to be dollar
11:10
because it's a
11:11
variable so if you now run this code you
11:14
will see in the left hand side
11:16
result.png will get created and if you
11:18
see by default we are
11:20
using it is setting this watermark in
11:24
the top left
11:27
position but here the value is Center it
11:30
should appear on the
11:40
center uh let me
11:45
see I think guys I made some kind of typ
11:48
of
11:49
mistake you uh basically the full source
11:52
code is given in the description you can
11:55
cross
11:57
check if I run this once again again we
12:00
will now see if
12:13
I sorry just me let me paste
12:21
it put a semicolon
12:31
so by default it appears on the top left
12:33
position so here you can change the
12:35
position here to
12:37
Center again run
12:40
this now it is uh it will appear if you
12:44
see this is output
12:50
here it is appearing in the center
12:52
position we can change it to bottom left
13:02
so you can see
13:04
that so in this way you can use the
13:06
image magic extension in
13:09
PHP to actually add watermarks you can
13:12
convert images from one format to
13:14
another you can resize crop images as
13:17
well but in this example we look at how
13:20
to add water marks thank you very much
13:22
for watching this video and I will be
13:24
seeing you guys in the next one
#Other