Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create non loop GIF image #4

Open
indapublic opened this issue Apr 17, 2014 · 9 comments
Open

Unable to create non loop GIF image #4

indapublic opened this issue Apr 17, 2014 · 9 comments

Comments

@indapublic
Copy link

$gc->create($frames, $durations, LOOP);
define('LOOP', 0);

or

define('LOOP', FALSE);

But image still looped.

@robho
Copy link

robho commented Apr 23, 2014

If you only want the animation to show once, set loop to 1. The README file says that setting loop to zero will give you an infinite loop.

@indapublic
Copy link
Author

<?

error_reporting(0);
set_time_limit(0);

define('LEFT', 130);
define('WIDTH', 360);
define('HEIGHT', 110);
define('SPEED', 100);
define('LOOP', 1);  //  Not working yet
global $frames, $durations;

$files = array(
        'http://img.page2images.com/ccimages/28/2c/mGOIqfym1vkiYiSN.png',
        'http://img.page2images.com/ccimages/bf/25/htg5zcWBGFTzrgVY.png',
        'http://img.page2images.com/ccimages/38/3a/egKwE9Egqt7xg5Yx.png',
        'http://img.page2images.com/ccimages/c5/62/ntnzrgKKU1p5ZGmb.png',
        'http://img.page2images.com/ccimages/3c/39/nV0HB0uz8THOL0IZ.png'
);

foreach ($files as $file) {
        $img = imagecreatetruecolor(WIDTH, HEIGHT);
        $im = imagecreatefrompng($file);
        $ims = getimagesize($file);
        imagecopy($img, $im, 0, 0, LEFT, 0, WIDTH, HEIGHT);
        ob_start();
        imagegif($img);
        $frames[] = ob_get_contents();
        $durations[] = SPEED;
        ob_end_clean();
}

include_once 'GifCreator.php';

$gc = new GifCreator();
$gc->create($frames, $durations, LOOP);

header('Content-type: image/gif');
echo $gc->getGif();

@indapublic
Copy link
Author

Output you can see at http://indapublic.ru/gif/
Twice-looping Gif

@indapublic indapublic reopened this Apr 24, 2014
@robho
Copy link

robho commented Apr 24, 2014

Your gif plays only once in Firefox and Opera. Which browser do you use when the gif loops?

@indapublic
Copy link
Author

Opera 12.16 - Once
Firefox 28.0 - Once

Safari, MacOS Version 7.0.3 (9537.75.14) - Twice
Chrome 34.0.1847.116 - Twice
Opera 20.0.1387.91 - Twice

@robho
Copy link

robho commented Apr 24, 2014

I think the patch below will fix the problem with "animate only once" and seems to work in both Firefox and Chrome.

diff --git a/src/GifCreator/GifCreator.php b/src/GifCreator/GifCreator.php
index 2bc4a1f..9b128ac 100644
--- a/src/GifCreator/GifCreator.php
+++ b/src/GifCreator/GifCreator.php
@@ -193,7 +193,9 @@ class GifCreator

             $this->gif .= substr($this->frameSources[0], 6, 7);
             $this->gif .= substr($this->frameSources[0], 13, $cmap);
-            $this->gif .= "!\377\13NETSCAPE2.0\3\1".$this->encodeAsciiToChar($t
+            if ($this->loop != 1) {
+              $this->gif .= "!\377\13NETSCAPE2.0\3\1".$this->encodeAsciiToChar(
+            }
         }
     }

The main problem is that different browsers seem to have their own idea on how to interpret the loop counter so it's impossible to fix this in GifCreator. It seems possible to work well for loop values 0 (infinity) and 1 (play once), but for other values there will be differences between browsers.

GIF loop count Chrome Firefox
not set 1 play 1 play
0 infinite plays infinite plays
1 2 plays 1 play
2 3 plays 2 plays
... ... ...

@tientn
Copy link

tientn commented Sep 30, 2016

@robho

Thank you, it work

@jonaswebdev
Copy link

@robho good job, do you have an idea when the @Sybio will approve your PR? this fix it's very helpful :)

@jonaswebdev
Copy link

I found a solution, use a forked class with this fix:
https://github.com/lunakid/AnimGif / https://packagist.org/packages/lunakid/anim-gif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants