1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

create animated gifs with php

Discussion in 'PHP' started by sivainternet, Jan 10, 2006.

  1. #1
    Is it possible to create animated gifs using php?

    Can anyone point me to any tutorial or script or resources for doing this?
     
    sivainternet, Jan 10, 2006 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I suspect feedburner are doing it with images like this one

    [​IMG]

    However I'm not sure how they're doing it...
     
    sarahk, Jan 10, 2006 IP
  3. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can use the gd library to create an image by merging a new image with an existing background image. Maybe if the background image was an animated gif you could then overlay some new content.

    Or try http://www.imagemagick.org/script/index.php
     
    dave487, Jan 10, 2006 IP
  4. TotalBrutal

    TotalBrutal Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It is possible. I have seen methods to create an animated gif from different gif files.
    You can create GIF files by using the GD functions, thus it must be possible...
    I am working on this stuff at the moment...
    First i d like to create GIF files and write them into files, after that i can use the another class to create an animated gif from 'em...
    But to tell the truth this isnt what i d like to do, i d liek to skip somehow the file usage part.... Any ideas ? In my oppinion i should modify the merging class... but it is so complex :S
     
    TotalBrutal, Jan 15, 2009 IP
  5. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #5
    It's possible, imagick isn't required.
    Here's the article : animated captcha
    The basic idea is you have to create every single frame and merge them into an animated one.
     
    xrvel, Jan 15, 2009 IP
  6. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #6
    I think the capacity to generate static/animated GIF is not in-built in PHP because of some licensing issues with Compuserve, the entity behind GIFs!
     
    rohan_shenoy, Jan 15, 2009 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #7
    Wow, this is an old thread that got bumped.

    They're actually easy to do... here's one I did, check out the gifmerge class at phpclasses.org

    [​IMG]
     
    sarahk, Jan 24, 2009 IP
  8. b0bleponge

    b0bleponge Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Excuse the monstrous bump, but this topic is a very popular Google search result and I see you, sarahk, is online.
    Well, I have spent several hours trying to generate animated GIFs, but I don't get this "gifmerge" to work.

    If it's so easy, sarahk, could you please explain me how to do this? This will certainly help many others.

    Thanks.
     
    b0bleponge, Aug 17, 2009 IP
  9. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #9
    Vooler, Aug 18, 2009 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #10
    you must have just missed me, sorry. It's late but I'll run through a tutorial in the morning :)
     
    sarahk, Aug 18, 2009 IP
  11. b0bleponge

    b0bleponge Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thank you very much sarahk, I'll be waiting for this!
    The example included with the class is just incomprehensible for beginners and there's no other documentation at all.
     
    b0bleponge, Aug 18, 2009 IP
  12. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #12
    premiumscripts, Aug 18, 2009 IP
  13. sivainternet

    sivainternet Peon

    Messages:
    521
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Wow ! What a bump !! :)

    I had actually given up on this project. Have to start again
     
    sivainternet, Aug 18, 2009 IP
  14. silotka

    silotka Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    You also can try create gifs with ffmpeg!
     
    silotka, Aug 18, 2009 IP
  15. b0bleponge

    b0bleponge Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Guys, I copied and pasted the animated gif example for Imagick; and it doesn't work.
    I asked my web hoster if Imagick was installed, and it is. What's wrong? The image is just not generated.

    He said: "Are you sure you're not missing the proper path in your script's configuration file?", what does that mean?

    I am c-o-n-f-u-s-e-d.
     
    Last edited: Aug 19, 2009
    b0bleponge, Aug 19, 2009 IP
  16. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #16
    The examples in the tutorial require php extension not module installed actually. Ask them to install this textension. On the other hand if imagick binaries are installed, then these examples wont work, you will be needing to call something like:

    exec('<IMAGICK_PATH>/convert myimage.bmp myimge.gif'); etc

    regards
     
    Vooler, Aug 19, 2009 IP
  17. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #17
    Ok, I've pulled up my code and lets see if I can explain it.

    I've been around awhile and when you've had to revisit code that's really old the last thing you need is code that's too clever for it's own good and unreadable. Better to be less intense and more readable/maintainable.

    So, I start with a simple switch to allow me to quickly turn my code from a gif to a regular script. If debug is false then the headers go out telling the browser to expect an image.
    $debug = false;
    
    if ($debug) error_reporting(E_ALL);
    else header("Content-type: image/gif");
    Code (markup):
    next I set up my paths

    You will need a cache folder in the MYPATH location

    define('MYPATH', '/home/mysite/domains/mysite.com/public_html/');
    
    require('GifMerge.class.php');
    require MYPATH .'muck/simplepie/simplepie.inc';
    Code (markup):
    And now I get the text for my images using SimplePie - bear in mind this is old code so the syntax for simplepie may well have changed.
    $feedchoice = 'http://www.mysite.com/feed/'
    $bg =   1;
    
    $backgrounds = array(
    	'1' => array('img'=>'mysite.gif', 'max'=>35, 'x'=>180), 
    	);
    
    //first up, create the single images
    $feed = new SimplePie();
    $feed->cache_location(MYPATH . 'cache');
    $feed->feed_url($feedchoice);
    $feed->init();
    
    Code (markup):
    These arrays are used to hold information about where to place the images. We don't handle them directly.
    
    
    $__i = array();  // items
    $__d = array();  // delay
    $__x = array();  // x location
    $__y = array();  // y location
    
    if ($feed->data)
    {
    	$these = $feed->get_items(); 
    	$these = array_splice($these, 0, 10);
    	foreach($these as $row)
    	{
    		if (is_array($row)) $title = $row['title'];
    		else $title = $row->get_title();
    
    		//this is clumsy but it's the bare minimum to get rid of dodgy characters in the image
    		$title = str_replace('’', "'", $title);
    		$title = str_replace('…', "!", $title);
    		$title = str_replace('“', '"', $title);
    		$title = str_replace('’', "'", $title);
    		$title = str_replace('”', '"', $title);
    		
    		if ($debug) echo "<li>{$title}</li>\n";
    		$__i[] = makeSingle($title, $backgrounds[$bg]);
    		$__d[] = 200;
    		$__x[] = 0;
    		$__y[] = 0;
    	}
    }
    if ($debug) exit;
    
    
    /*
    	Params from left to right:
    	1 		<== Array of gif frames
    	2,3,4	<== Transparent colors (red, green, blue)
    	5		<== Frames loop (-10 infinity, 5 : five loop, 9 : nine loop, etc...)
    	6		<== Array of delay between frames in millisecond
    	7		<== Disposal
    	8,9		<== Array of X-Y distances from top & left
    	*/
    //now put it all together
    $gifmerge	= new GifMerge($__i, 1, 1, 1, -10, $__d, 2, $__x, $__y, 'C_FILE');  //black
    echo 		$gifmerge -> getAnimation();
    
    exit;
    //=================
    
    Code (markup):
    makeSingle creates the individual panes in the animated gif
    
    function makeSingle($text, $bg)
    {
    	$im = imagecreatefromgif ($bg['img']);
    	// Create some colors
    	$white = imagecolorallocate($im, 255, 255, 255);
    	$black = imagecolorallocate($im, 0, 0, 0);
    
    	$loadedfont = 4;
    	$bits = explode(' ', $text);
    	$lines = array('');
    	$ptr = 0;
    	$max = $bg['max'];	
    	for ($i = 0; $i < count($bits); $i++)
    	{
    		if (strlen($lines[$ptr].$bits[$i]) < $max) $lines[$ptr] .= $bits[$i].' ';
    		else 
    		{
    			$ptr++;
    			$lines[$ptr] = $bits[$i].' ';
    		}
    	}
    	if (count($lines) == 1) imagestring($im, $loadedfont, $bg['x'], 20, $lines[0], $black);
    	else
    	{
    		imagestring($im, $loadedfont, $bg['x'], 10, $lines[0], $black);
    		imagestring($im, $loadedfont, $bg['x'], 30, $lines[1], $black);
    	}
    	// Using imagepng() results in clearer text compared with
    	$fname = MYPATH .'cache/'.md5($text).'.gif';
    	imagegif($im, $fname);
    	imagedestroy($im);
    	return $fname;
    }//makeSingle
    ?>
    Code (markup):
     
    sarahk, Aug 22, 2009 IP