PHP FFMPEG Script to Compress Size of GIF Animation Files in Browser Using HTML5 Form
Dec 21, 2025
Buy the full source code of the application here:
https://procodestore.com/index.php/product/php-ffmpeg-script-to-compress-size-of-gif-animation-files-in-browser-using-html5-form/
Show More Show Less View Video Transcript
0:05
Uh hello friends. Today in this tutorial
0:07
I will basically show you a PHP script
0:10
which will allow us to compress uh GIF
0:13
animations. GIF is basically a type of a
0:16
image which repeats for a sequence of
0:19
time and you can see uh we will have
0:22
original GIF size. We will compress it.
0:25
So it's a 13 MB file. We compress it to
0:28
5 MB. So let me show you the
0:30
application. So this is a application
0:33
here. We are using Bootstrap here also.
0:35
If we can choose the GIF file guys and
0:38
you will see it's 13 mgabytes. The
0:41
original size of the GIF file is 13 MB.
0:44
And if I select this GIF file and click
0:47
uh click this button which is compress
0:50
GIF. So there will be a ffmpg command
0:53
which will be running in the background
0:54
once I click this button. And now it
0:57
will basically compress this uh GIF.
1:00
Original size is 13 MB and it is
1:03
compressed to 5 MGB and the compression
1:06
ratio is 62%. So it will automatically
1:09
calculate for every GIF file that you
1:12
submit inside this application. So the
1:15
best quality will be preserved. The
1:17
quality of the GIF file will be
1:18
preserved alongside with compressing the
1:20
size of the GIF file. So now you can
1:23
open this and now you can see that the
1:25
quality is not preserved here and there
1:29
is a download button. If you click the
1:31
download button your file will be
1:33
downloaded 5 M MB you will see that. So
1:36
you can play your file
1:40
the original file. If I show you this
1:43
was the original file 13 mgabytes
1:48
and you can see the quality of the GIF
1:51
animation is preserved
1:55
and similarly you can take any more
1:57
example. Let me take second file here.
1:59
Let's suppose I take uh
2:02
this file. So this is 9 mgabytes the
2:06
original size and it is compressed to 4
2:08
MB. So the compression ratio is 51%.
2:12
And now you can see this file as well.
2:14
You'll see that it is compressed to
2:17
almost 50%.
2:19
So I will show you guys how you can make
2:22
this PHP script. You can basically
2:24
deploy this tool as a GIF compressor in
2:27
your tool website. You can monetize with
2:29
Google AdSense or earn a lot of money.
2:32
So the link is given in the description.
2:34
If you want to basically get the PHP
2:36
script, you can purchase it from
2:38
stripe.com and you will get this. So I
2:42
will write this in step by step
2:44
explaining you each. So just basically
2:50
create a index.php file here and we will
2:52
start from scratch. So here first of all
2:55
guys what we will do we will have a
2:58
simple
3:00
form here.
3:02
Let me write the form in its separate
3:04
file. Let's suppose I create a file
3:06
index html and I rename this file to
3:10
process.php.
3:12
So inside our form guys basically we
3:16
will include the bootstrap CDN. So which
3:19
is required. So just after the title
3:22
just paste the CDN for bootstrap and we
3:24
will now be having container class
3:27
margin top five and we will have a
3:31
heading which will say gif compressor
3:37
and after that guys we will have a form
3:39
here and the method here will be post
3:42
encoding type multiart form data. So
3:45
action will go to process dot php. So we
3:48
will write this PHP script. So form
3:51
group class is a bootstrap class and
3:53
here we will allow the user to select a
3:55
GIF file that they want to compress. So
3:58
we will simply say select a GIF and then
4:02
the user will select a GIF file. So
4:04
input type is file and we will say name
4:08
is equal to file. We will also be giving
4:10
a bootstrap class to it of form control
4:12
and this is required. We will only be
4:15
accepting give file here like this.
4:21
And then guys we will have
4:25
a button which will submit the form. We
4:27
will say button
4:29
which will type is submit. So button
4:33
type submit and we will give it a class
4:36
of btn btn primary and then we will say
4:40
compress gif. So if you just refresh
4:43
your application guys, so you will have
4:45
a simple form here. And uh now what we
4:49
need to do here, we now need to write
4:52
our uh
4:56
PHP code here. So inside the PHP code
4:59
guys, we will just write like this.
5:04
And uh we can have a if condition here.
5:07
And inside if condition
5:12
we will basically check the request
5:16
method request method
5:19
and uh
5:24
if the method is post in that case we
5:27
will now need to create dynamic
5:28
directories. So first of all we will
5:30
create the first directory where all
5:32
your files will be stored and then the
5:34
output directory as well. So output
5:36
directory. So we have put the address
5:38
here uploads and output. You don't need
5:41
to create these directories. It will
5:42
automatically create. So how we can
5:44
create this? We have a function in PHP
5:47
which we will we will first of all say
5:50
is directory.
5:52
We will basically check that if this
5:54
uploads directory exist or not.
6:00
Uploads directory. If it doesn't exist
6:02
in that case it will return false. And
6:04
here we will use this function make
6:06
directory. It will make this directory
6:08
here in the root directory. We will pass
6:10
this code 0777.
6:13
This is permission code.
6:16
And in the sim similar way guys we will
6:19
again check for the output directory as
6:22
well.
6:29
output directory. If it doesn't exist
6:32
then in that case we will create the
6:34
output directory
6:38
like this. So once you basically run
6:41
this code guys so what should happen if
6:43
you check right here in the uh left hand
6:46
side no directory is there. So if I run
6:48
this code if I just select a file click
6:52
nothing happens here but the directories
6:54
have been created. So for the very first
6:57
time it will create them and after that
7:00
this statement will return
7:04
uh
7:05
true. So in that case this line will not
7:08
be executed. So right here guys we will
7:11
now be after creating the directories
7:13
you will now say where to store the
7:18
files. So we will be storing them in the
7:21
root uh basically uploads directory.
7:24
Here we need to provide the name
7:26
parameter. So whatever name parameter
7:28
you have given. So we have given this
7:30
name parameter which is
7:33
file. So this needs to be file here. So
7:36
file and then followed by the name. So
7:40
this will basically get the file name.
7:43
So after that we will say output gif
7:45
path. So here we will store it in the
7:49
output directory and this will be
7:52
compressed
7:56
just concatenating it and we'll
7:58
concatenate dollar files give the name
8:03
of the file.
8:07
So make sure that you put semicolon
8:09
after each line in PHP because it will
8:11
create error if you don't do it. And now
8:14
we need to move this file guys. So we
8:16
have a function in PHP which is move
8:18
uploaded file. So this will actually
8:20
upload the file.
8:24
We have the temporary location from
8:26
which we can move it to the
8:29
location that we specified which is
8:32
path. If this return true in that case
8:34
our file is uploaded and then after that
8:37
we will get the original size of the
8:39
file. We will say original size. So
8:42
there is a function in PHP which allows
8:45
you to get the size of the file. So we
8:47
will pass the path of the file to get
8:49
the original size. And here we will
8:52
write ffmpg command guys. So if you
8:54
don't know about ffmpg it's basically a
8:57
open-source tool for interacting with
8:59
audio and video.
9:01
They have their own official website. It
9:03
is used by many companies out there. So
9:06
just it's a command line tool and uh
9:09
after installation just check
9:12
write here ffmppg the command is
9:16
accessible here. So we will try to
9:18
execute the command using PHP. So we can
9:22
basically write our command here which
9:24
we need to execute.
9:27
So we will write this command ffmpg- i
9:30
for the input. We will provide the input
9:32
file here and then we will provide a
9:35
video filter -ashvf. This stands for
9:38
video filter
9:40
slash fps. So here we can manipulate the
9:44
fps of the video. Make it to 10. You can
9:46
just uh play with this command as long
9:49
as you want. And then we will say
9:53
encoder value. We will change it to gif.
9:56
Output
9:57
gif path. That's all. This is all that
10:00
you need to do right and we have a
10:02
execute method guys which allows you to
10:05
execute any command
10:07
in command line using PHP. So this
10:10
execute method allows you to do that
10:13
thing
10:15
if you see clearly
10:23
this is a function which allows you to
10:26
do that. So,
10:31
so after doing this guys, after
10:33
executing it, we just need to write here
10:36
make a compressed size.
10:40
So, we will calculate the file size of
10:43
the compressed file as well. We will
10:45
provide the path here output give path.
10:48
So, all these things we are storing it
10:49
inside these variables. So, later on we
10:52
can store display them inside the HTML.
10:54
So now we can have also a L statement as
10:58
well. So if the file is not uploaded we
11:01
can say error in moving file. That's
11:05
all.
11:08
This is the PHP script guys. So now what
11:11
we need to do we need to move to our
11:13
index html.
11:15
So if you just uh do like this so what
11:17
should happen basically the application
11:20
will work fine. If you submit the input
11:23
file, click compress. So it is saying
11:26
undefined array key gif on line number
11:29
16.
11:31
Let me check.
11:35
Sorry, this needs to be file.
11:38
And here also file
11:47
again submit.
11:52
So it is saying undefined variable
11:55
on line number 18. So when you live code
11:59
guys some mistake can happen. You can
12:01
see this needs to be capital files.
12:06
Again
12:09
select your file click compress. So
12:12
nothing here will where happen but our
12:14
file is generated if you see in the
12:17
output directory. So this is our
12:18
compressed file guys. You will see that
12:20
this is the actual upload that we did
12:23
and this is the actual compressed file.
12:25
Now we need to display this file in the
12:27
index html. How we can do that? It's
12:29
very simple. So we need to write some
12:31
code here. HTML code. So we can need to
12:35
after the form
12:37
we can here put so what I can do is that
12:40
I can move this code in in the PHP file
12:43
that we write this file
12:47
like here and uh I can delete this file
12:52
and rename this file to index php. So
12:56
this is a clearer way of doing things in
12:59
a single file
13:02
and then we can put the PHP tag here to
13:05
embed PHP code in HTML. So right here
13:08
you can check if is set original size
13:14
and also is set
13:19
is set function basically uh does the
13:22
thing guys which it it checks that uh if
13:25
these two variables exist or not. We
13:28
have created these two variables at the
13:29
top guys. You will see compressed size
13:31
and original size. So if these two
13:34
variables exist
13:36
in that case we need to display the size
13:40
the first original size and the
13:42
compressed size. So here
13:47
after [clears throat] that what we need
13:49
to do
13:51
compress size
13:55
just do like this.
14:01
Uh this is slightly complicated code
14:03
guys. So what I will do I will simply
14:05
copy paste it there. It also contains a
14:08
function as well which automatically
14:10
calculates the size and display it. So I
14:14
will simply paste it here and explain
14:16
you what is happening right here and
14:18
this also contains this function. So if
14:20
you want this full script guys I will
14:23
definitely say just the link is given
14:26
you can purchase it. So basically here
14:28
we are using that function. We are
14:30
displaying the original size and the
14:32
compress size and then we are displaying
14:34
the compression ratio. This is all about
14:37
user engagement guys and also we are
14:39
displaying a download compress give
14:42
button as well. So if you want to rank
14:44
your tool on the Google first page then
14:46
this customization is very much
14:48
important. And if you search compress
14:54
one thing I forgot guys we need to
14:56
change the action parameter to nothing.
14:59
So this will only
15:02
if you now do it and select. So now guys
15:06
you will see that basically original GIF
15:08
size 13 MB it was compressed to 5 MB. So
15:12
the user will clearly see on the page
15:15
how your compressor has done the thing
15:18
how much space it has saved for the
15:21
user. This is a great information that
15:23
you can show as a tool and you can
15:27
basically go to the description guys.
15:29
You can pay the money at stripe and you
15:31
can get this script and you can rank
15:33
your tool website. So thank you very
15:36
much guys for watching this video. If
15:38
you like this video please hit that like
15:39
button, subscribe the channel and I will
15:42
be seeing you in the next live stream.
