📜 ⬆️ ⬇️

Resize animated GIF images using PHP + GD

Good day.

First, a small entry.
During the development of my own project, I was faced with the task of resizing GIF files using only PHP tools without using the ImageMagick software package (I think you guessed why). If there is a task to mean it must be solved, without giving it much attention and complexity, I went to the Internet to search for the implemented script as a function or class. And to my surprise and chagrin (as the resize must be unequivocal) I found only two PHP scripts, the authors of which made an attempt to realize this non-trivial task (I generally keep silent about only alternative ImageMagick there for other languages), although I rummaged the Internet up and down.

- first script: trying two classes GIFDecoder.class.php and GIFEncoder.class.php, which were used to generate GIF animation, teach resize GIF images; as a result, the end user has to write almost a third class to work with data, and judging by the reviews, only half succeed; but this is not the main thing, but the main thing is that the resizing of many files is incorrect (problems with transparency, interlacing, frame processing, etc.), there are quite a lot of these files and therefore not suitable for the task, link to the author’s page

- second script: the gifresizer.php class is already more complete, specialized and easier to use, and is considered the best on the Internet today, but my experiments with it showed that this class has problems with processing GIF files that have graphics data optimization, as well as incorrect work with the palette, and the worst thing is that from some GIF files the script goes into an infinite loop, about which the truth is honestly written by the author himself link to the class page
')
Of the above reasons, none of the classes naturally came up. What to do?!!! Having a little thought, having rolled up our sleeves and having postponed for a while the main project, I decided to implement an unimplemented class, namely, for a full-fledged and most importantly correct resizing of GIF files. For a long time I will not say only one thing, the task was really non-trivial complexity, and all due to the fact that the tricky structure of the GIF file, although a standardized standard, few people follow, more precisely, the standard does not conform to the standard.

During the development process, the code was rewritten three times (sometimes there were thoughts to throw this thankless task), and as a result a class appeared for resizing animated GIF files with GIF_eXG transparency support . A distinctive feature of the class is: fast, stable and correct work, as well as ease of use. Moreover, the class goes through (read: corrects errors) the source file so that it at least approximately corresponds to the standard, as a result in Windows where there were problems with animation playback (in this OS) after the resize animation plays correctly.

I performed the analysis of the structure and analysis of each bit manually and did not resort to the functions of analyzing the graphic file of the GD library, which in its nature is rather buggy. It (the GD library) was connected only to resize a single frame. With the use of the GD library for resizing a single frame, one glitch is associated (usually 1 to 50-70 resizes), which is a small graphic artifact in the form of scattered pixels.

Class usage example:

require_once "gif_exg.php";

$ nGif = new GIF_eXG ("../ image / src.gif", 1);
$ nGif-> resize ("../ image / dst1.gif", 180,180,1,0);
$ nGif-> resize ("../ image / dst2.gif", 150,150,0,0);

I think everything is clear, only small comments on the parameters passed:
- in the constructor, the second parameter is responsible for the optimization of the structure, if (1) the output file is more compact in size, if (0) the entire original structure is preserved;
- in the only open function resize, the fourth parameter indicates whether to observe symmetry (1), or not to comply with (0);
- the fifth parameter is experimental: (1) try to interpolate pixels, (0) without interpolation (recommended);

Place of application of the class: the creation of animated avatars, previews, image galleries and so on.

The file contains instructions in broken English (sorry for it in advance, problems with foreign languages) for the international community, I think you will understand.

Links to the class ( current version 1.07 ):

class GIF_eXG (PhpClasses)
class GIF_eXG (GitHub)

Source: https://habr.com/ru/post/186084/


All Articles