2011-09-07 29 views
6

Se ho un sacco di risorse di immagine che ho creato usando la libreria GD in PHP, o comunque un mucchio di fotogrammi, come posso combinarli in una gif animata? Ho una serie di immagini e un frame al secondo che vorrei aggiungere ..Creare gif animate usando la libreria GD

Non riesco a utilizzare ImageMagick o FFMPEG, e preferirei semplicemente utilizzare la libreria GD se possibile.

Apparentemente "Support for creating GIF animations is also available.", ma non riesco a trovare la documentazione su come accedere a questo da PHP?

+1

possibile duplicato di [PHP: creare GIF animate semplici da due immagini JPEG?] (Http://stackoverflow.com/questions/2191367/php-create-simple-animated-gif-from-two-jpeg-images) – Mat

risposta

2

Una buona ricerca su Google ha rivelato GIFEncoder.class.php trovato in http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html
Questo collegamento richiede la registrazione.

Così ho cercato un po 'ed è incluso nel phpvideotoolkit su code.google.com e può essere scaricato qui:
http://phpvideotoolkit.googlecode.com/svn-history/r6/trunk/phpvideotoolkit/adapters/ffmpeg-php/gifencoder/GIFEncoder.class.php

esiste anche una versione bugfixed basta cambiare il nome del file per GIFEncoder.class. phpvideotoolkit.php nel link sopra.

Non ho provato da solo ma forse può aiutarti.

nella directory principale del file php su code.google.com è anche un esempio

buona fortuna

0

AFAIK gd lib in bundle con PHP non supporta la creazione di GIF animate. Potresti usare lo gifsicle.

+0

In qualsiasi modo senza l'accesso alla linea di comando? – Cyclone

+0

Puoi usare la funzione exec() di PHP - conta? :) O potresti usare le funzioni PHP di ImageMagick, ma non l'ho mai usato. YMMV. – johndodo

4

Questo non può essere fatto con GD, ma ho trovato una grande biblioteca per esso. È un po 'complicato, quindi ecco un link alla libreria che crea gif animate con php. Spiega come usarlo a fondo. http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html

Potete guardare il mio esempio utilizzando questa libreria:

Basta selezionare 2 immagini e scrittura 100 per velocità di 900 per la larghezza e l'altezza. Li metterà in uno slideshow gif animato.

Ecco il codice per lo script:

<?php 
if(isset($_POST['speed'])) 
{ 
    header('Content-type: image/gif'); 
    if(isset($_POST['download'])){ 
    header('Content-Disposition: attachment; filename="animated.gif"'); 
    } 
    include('GIFEncoder.class.php'); 
    function frame($image){ 
     ob_start(); 
     imagegif($image); 
     global $frames, $framed; 
     $frames[]=ob_get_contents(); 
     $framed[]=$_POST['speed']; 
     ob_end_clean(); 
    } 
    foreach ($_FILES["images"]["error"] as $key => $error) 
    { 
     if ($error == UPLOAD_ERR_OK) 
     { 
      $tmp_name = $_FILES["images"]["tmp_name"][$key]; 
      $im = imagecreatefromstring(file_get_contents($tmp_name)); 
      $resized = imagecreatetruecolor($_POST['width'],$_POST['height']); 
      imagecopyresized($resized, $im, 0, 0, 0, 0, $_POST['width'], $_POST['height'], imagesx($im), imagesy($im)); 
      frame($resized); 
     } 
    } 
    $gif = new GIFEncoder($frames,$framed,0,2,0,0,0,'bin'); 
    echo $gif->GetAnimation(); 
} 
?> 
<form action="" method="post" enctype="multipart/form-data"> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script src="jquery.MultiFile.js"></script> 
<script src="jquery.placeholder.js"></script> 
<input type="file" name="images[]" class="multi" /> 
<script> 
    $(function(){ 
     $('input[placeholder], textarea[placeholder]').placeholder(); 
    }); 
</script> 
    <SCRIPT language=Javascript> 
     <!-- 
     function isNumberKey(evt) 
     { 
     var charCode = (evt.which) ? evt.which : event.keyCode 
     if (charCode > 31 && (charCode < 48 || charCode > 57)) 
      return false; 

     return true; 
     } 
     //--> 
    </SCRIPT> 
<input name="speed" maxlength="10" type="text" placeholder="Speed of frames in ms" onkeypress="return isNumberKey(event)"> 
<input name="width" maxlength="4" type="text" placeholder="Width" onkeypress="return isNumberKey(event)"> 
<input name="height" maxlength="4" type="text" placeholder="Height" onkeypress="return isNumberKey(event)"> 
<input type="submit" name="download" value="Download!"> 
<input type="submit" name="preview" value="Preview!"> 
</form> 

Come si vede fa riferimento la classe GIFEncoder trovato sul primo link. Utilizza anche alcune convalide javascript e multiloadload jQuery.

BTW questa domanda è stata già posta.

+5

Se la domanda è già stata posta, contrassegnala come duplicata; non pubblicare risposte duplicate. – Mat

Problemi correlati