#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,470
    Rep Power
    221

    Generating Gif With PHP


    Hey guys;

    The previous thread died. I post this since I really need the answer

    I use the code below to generate custom timers for clients.

    I am using this library https://seanja.com/secret/countdown/

    Here is an example https://webmoosh.com/e/26

    My question is, could this hurt the server? I mean it is working well but I don't understand where the image is. Is it getting stored somewhere for good?

    If I want to do this for evergreen launches with +100k prospects, I have to change the php file that generates the image to custom URL with prospect id something like https://site.com/countdown-timer/2/145276

    Does this mean it generates 100k images? Is these images are being saved somewhere, that could suck since each gif is almost 1 MB.

    I have a feeling since it generates the images on the go, it's not stored anywhere permanently but I am not sure even a bit.
    Thank you

    PHP Code:
    <?php 
    class Email_countdown_generator extends CI_Controller 

        public function 
    index($launch_id
            { 
                
    $sql "SELECT timezone, offer_close_date FROM launch_launches 
                    INNER JOIN launch_owners ON  
                        launch_launches.user_id = launch_owners.id 
                    WHERE launch_launches.id = ?"


                
    $query $this->db->query($sql, array($launch_id));     
                
    date_default_timezone_set("".$query->result_array()[0]['timezone']."");
                include 
    'AnimatedGif.php'
                
    $time "".$query->result_array()[0]['offer_close_date'].""
                
    $future_date = new DateTime(date('r',strtotime($time))); 
                
    $time_now time(); 
                
    $now = new DateTime(date('r'$time_now)); 


                
    $frames = array(); 
                
    $delays = array(); 


                
    $image imagecreatefrompng(base_url('countdown.png')); 
                
    //$image = imagecreatefrompng(base_url('countdown-white.png')); 
                
    $delay 100// milliseconds 
                
    $font = array( 
                    
    'size'=>40
                    
    'angle'=>0
                    
    'x-offset'=>10
                    
    'y-offset'=>70
                    
    'file'=>'digitaldream.ttf'
                    
    'color'=>imagecolorallocate($image255255255), 
                ); 
                for(
    $i 0$i <= 60$i++){ 
                    
    $interval date_diff($future_date$now); 
                    if(
    $future_date $now){ 
                        
    // Open the first source image and add the text. 
                        
    $image imagecreatefrompng(base_url('countdown.png')); 
                        
    $text $interval->format('00:00:00:00'); 
                        
    imagettftext ($image $font['size'] , $font['angle'] , $font['x-offset'] , $font['y-offset'] , $font['color'] , $font['file'], $text ); 
                        
    ob_start(); 
                        
    imagegif($image); 
                        
    $frames[]=ob_get_contents(); 
                        
    $delays[]=$delay
                                
    $loops 1
                        
    ob_end_clean(); 
                        break; 
                    } else { 
                        
    // Open the first source image and add the text. 
                        
    $image imagecreatefrompng(base_url('countdown.png')); 
                        
    $text $interval->format('%a:%H:%I:%S'); 
                        
    // %a is weird in that it doesn’t give you a two digit number 
                        // check if it starts with a single digit 0-9 
                        // and prepend a 0 if it does 
                        
    if(preg_match('/^[0-9]\:/'$text)){ 
                            
    $text '0'.$text
                        } 
                        
    imagettftext ($image $font['size'] , $font['angle'] , $font['x-offset'] , $font['y-offset'] , $font['color'] , $font['file'], $text ); 
                        
    ob_start(); 
                        
    imagegif($image); 
                        
    $frames[]=ob_get_contents(); 
                        
    $delays[]=$delay
                                
    $loops 0
                        
    ob_end_clean(); 
                    } 
                    
    $now->modify('+1 second'); 
                } 
                
    //expire this image instantly 
                
    header'Expires: Sat, 26 Jul 1997 05:00:00 GMT' ); 
                
    header'Last-Modified: ' gmdate'D, d M Y H:i:s' ) . ' GMT' ); 
                
    header'Cache-Control: no-store, no-cache, must-revalidate' ); 
                
    header'Cache-Control: post-check=0, pre-check=0'false ); 
                
    header'Pragma: no-cache' );  
                
    $gif = new AnimatedGif($frames,$delays,$loops); 
                
    $gif->display(); 
            } 

    }
    ?>
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,395
    Rep Power
    2105
    The code says it all. The GIF is generated on the fly each time it's called. Is that great for the server? Not really as it's a fir bit of processing time vs. sending one static image. BUT... If you need the countdown to start at a variable time, that's the best you're going to do without using JavaScript.

    As far as server resources, just monitor the server when people start to visit, You'll soon see if the server can handle it or not. It's not that big a program, but image processing can be resource-intensive.
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,470
    Rep Power
    221
    Originally Posted by Catacaustic
    The code says it all. The GIF is generated on the fly each time it's called. Is that great for the server? Not really as it's a fir bit of processing time vs. sending one static image. BUT... If you need the countdown to start at a variable time, that's the best you're going to do without using JavaScript.

    As far as server resources, just monitor the server when people start to visit, You'll soon see if the server can handle it or not. It's not that big a program, but image processing can be resource-intensive.
    Hi;

    Ok.

    I finally made it to brisbane. Will be out of here soon. If you're still here pm me I like to meet you.

    Great

IMN logo majestic logo threadwatch logo seochat tools logo