Resize animated GIFs using PHP + GD
Good day.
First, a small introduction.
During the development of my own project, I was faced with the task of resizing GIF files exclusively using PHP means without using the ImageMagick software package (I think they guessed why). If there is a problem, it must be solved, without giving it much attention and complexity, I went to the Internet to look for a implemented script in the form of a function or class. And to my surprise and chagrin (since resize should be unambiguous), I found only two PHP scripts, the authors of which made an attempt to implement this non-trivial task (I generally keep silent about other languages only there is no alternative ImageMagick), although I rummaged the Internet far and wide.
- the first script: an attempt to teach two classes GIFDecoder.class.php and GIFEncoder.class.php, which were used to generate GIF animations, to teach how to resize GIF images; as a result, the end user has to write almost the 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 resize of many files is incorrect (problems with transparency, rotation, frame processing, etc.), there are a lot of these files, and therefore are not suitable for the task, a link to the author’s page
- the second script: the gifresizer.php class is already more integral, 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 processing GIF files that have graphic data optimization, as well as incorrect work with the palette, and the worst thing is that the script leaves an endless loop from some GIF files, as the author honestly writes the link to the class page
Of the above reasons, not a single class naturally came up. What to do?!!! After a little reflection, I rolled up my sleeves and postponed the main project for some time, I decided to implement an unrealized, namely, a class for the full and most importantly correct resize of GIF files. I won’t tell you for a long time only one thing, the task was really of non-trivial complexity, and all because of the fact that the tricky structure of the GIF file is standardized, but few people follow the standard, more precisely, the standard here does not comply with the standard.
In the process of development, the code was rewritten three times (sometimes there were thoughts of leaving this thankless task), and as a result, a class appeared for resizing animated GIF files with support for transparency GIF_eXG. A distinctive feature of the class is: fast, stable and correct operation, as well as ease of use. Moreover, the class enumerates (read: corrects errors) the source file so that it at least approximately conforms to the standard, as a result in Windows in a file where there were problems with the animation playing (in this OS) after resizing, the animation plays correctly.
I performed manual analysis of the structure and analysis of each bit and did not resort to the functions of analyzing the graphic file of the GD library, which is rather glitchy in nature. It (the GD library) was connected only for resizing a single frame. Using the GD library for resizing a single frame, one glitch is connected (usually 1 for 50-70 resizes), which is small graphic artifacts in the form of scattered pixels.
An example of using the class:
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 a few remarks on the parameters passed:
- in the constructor, the second parameter is responsible for optimizing the structure, if (1) then the output file is more compact in size, if (0) the whole original structure is saved;
- in the only open function resize, the fourth parameter indicates whether to observe symmetry (1) or not (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, etc.
The file contains instructions in broken English (I apologize in advance for it, problems with foreign languages) for the international community, I think you will figure it out.
Class references ( current version 1.07 ):
GIF_eXG class (PhpClasses)
GIF_eXG class (GitHub)
First, a small introduction.
During the development of my own project, I was faced with the task of resizing GIF files exclusively using PHP means without using the ImageMagick software package (I think they guessed why). If there is a problem, it must be solved, without giving it much attention and complexity, I went to the Internet to look for a implemented script in the form of a function or class. And to my surprise and chagrin (since resize should be unambiguous), I found only two PHP scripts, the authors of which made an attempt to implement this non-trivial task (I generally keep silent about other languages only there is no alternative ImageMagick), although I rummaged the Internet far and wide.
- the first script: an attempt to teach two classes GIFDecoder.class.php and GIFEncoder.class.php, which were used to generate GIF animations, to teach how to resize GIF images; as a result, the end user has to write almost the 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 resize of many files is incorrect (problems with transparency, rotation, frame processing, etc.), there are a lot of these files, and therefore are not suitable for the task, a link to the author’s page
- the second script: the gifresizer.php class is already more integral, 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 processing GIF files that have graphic data optimization, as well as incorrect work with the palette, and the worst thing is that the script leaves an endless loop from some GIF files, as the author honestly writes the link to the class page
Of the above reasons, not a single class naturally came up. What to do?!!! After a little reflection, I rolled up my sleeves and postponed the main project for some time, I decided to implement an unrealized, namely, a class for the full and most importantly correct resize of GIF files. I won’t tell you for a long time only one thing, the task was really of non-trivial complexity, and all because of the fact that the tricky structure of the GIF file is standardized, but few people follow the standard, more precisely, the standard here does not comply with the standard.
In the process of development, the code was rewritten three times (sometimes there were thoughts of leaving this thankless task), and as a result, a class appeared for resizing animated GIF files with support for transparency GIF_eXG. A distinctive feature of the class is: fast, stable and correct operation, as well as ease of use. Moreover, the class enumerates (read: corrects errors) the source file so that it at least approximately conforms to the standard, as a result in Windows in a file where there were problems with the animation playing (in this OS) after resizing, the animation plays correctly.
I performed manual analysis of the structure and analysis of each bit and did not resort to the functions of analyzing the graphic file of the GD library, which is rather glitchy in nature. It (the GD library) was connected only for resizing a single frame. Using the GD library for resizing a single frame, one glitch is connected (usually 1 for 50-70 resizes), which is small graphic artifacts in the form of scattered pixels.
An example of using the class:
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 a few remarks on the parameters passed:
- in the constructor, the second parameter is responsible for optimizing the structure, if (1) then the output file is more compact in size, if (0) the whole original structure is saved;
- in the only open function resize, the fourth parameter indicates whether to observe symmetry (1) or not (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, etc.
The file contains instructions in broken English (I apologize in advance for it, problems with foreign languages) for the international community, I think you will figure it out.
Class references ( current version 1.07 ):
GIF_eXG class (PhpClasses)
GIF_eXG class (GitHub)