Crop And Resize Animated GIF without Losing The Animation in PHP Website

Uploading an image on a PHP website isn’t a hard job, but uploading an animated GIF image file without losing the animation is no novice cup of tea. So in today’s PHP tutorial for beginners, we’re going to make a demo on how to resize Animated GIF and how to crop a gif in PHP website.

In this PHP tutorial, we’re going to add a button to allow user for uploading any size of GIF images in PHP webpage.

Let’s Get Started

First, create a form with input type = “file” and submit button.

<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="image" value="select..">
<input type="submit" value="Upload" name="upload">

Once you create the form, check folder path whether it exists or not. If it doesn’t exist, then create a path to upload your image.

chmod($uploadPath, 0777);

Next, check the file extension that you’ll allow to upload. Since we’re developing for GIF images, we’ll only allow image (GIF) extension. So if the image is of GIF cropper extension then it will allow uploading, otherwise, it will fire an exception.

$types = array('jpeg', 'gif','png');
$filename    = $_FILES["image"]["name"];
$extension   = end(explode(".", $filename));

echo "Sorry your file was not uploaded. It may be the wrong filetype. We only allow JPG, GIF, and PNG filetypes. <br/><br/>";

Now it’s time to upload an image and unlink or delete old images. Here we’ll scan the directory and delete old images and upload an image using “move_uploaded_file”.

$newfilename = "image.".$extension;
//read images and delete last
$images = glob($dirname."*");
foreach($images as $image) {
//upload new image
chmod($uploadPath.$newfilename, 0777);
echo "Something went wrong please try again. <br/><br/>";

Want to Create a Web Application?

Validate your app idea and get a free quote.

After completing this code, open your browser and go to http://localhost/image-upload (your PHP file name here)/
Once you open it, it’ll look like specified in the below screenshot. Now you can upload a GIF by clicking on the ‘browse’ button.


After clicking on ‘browse’ button, select any GIF image from your local system.


After selecting a GIF image, you need to click on upload button. It will show your image and give you output shown as below:


That’s it!

We also shared this code on GitHub.

You can directly download the entire source code of resizing animated GIF.

By following the above steps, you can resize or crop an animated gif file without losing the animation quality. If you face any problem implementing this feature, contact our PHP developers by filling the form below.

Do you know most big brands like Facebook, Tumblr, and Wiki are built on PHP only? And, building a website in PHP has many benefits like secured, independent, and cost-efficient. So, if you are looking for web app making company or want to hire PHP developer for building your website, go ahead. Contact us now.

Author Bio
Jigar Mistry
Jigar Mistry
Designation: Vice President (Technologies)

Jigar Mistry is Vice President (Technologies) at Space-O Technologies. He has 14+ years of experience in the web and mobile app development industry. He has expertise in different mobile app categories like health and fitness, eCommerce, and on-demand.