2010-02-03 32 views

risposta

6

Non è possibile utilizzare le funzioni GD standard preinstallate con PHP.

C'è un class on phpclasses.org per questo. Non l'ho mai usato da solo, ma è usato da molti altri pacchetti.

In alternativa, se si ha accesso a ImageMagick da PHP, utilizzando la libreria MagickWand o la riga di comando, utilizzarlo. Con ImageMagick, non c'è problema.

+0

Il codice ** phpclasses.org ** è un incubo da accedere e i file di input per esso devono essere file GIF. Non funziona per i file JPEG ...: -/ –

+0

È possibile convertire le immagini JPEG di input in immagini GIF (statiche) utilizzando questa risposta a un'altra domanda SO: https://stackoverflow.com/a/755843/1617737 –

3

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

Selezionare 2 foto e scrivere 100 per velocità 900 per larghezza e 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.

Nota: questa domanda è già stata posta.

+0

Grazie per il tuo bellissimo esempio! – Roy

+0

@Tom quando ho provato questo errore di ritorno del codice come "non può essere visualizzato perché contiene errori." Potete per favore aiutarmi. – Jalpa

7

Per una soluzione piacevole, veloce e più recente, vedere this SO answer.

Per una soluzione ancora più recente, here is my fork, con un numero di piccole correzioni & miglioramenti. Un esempio di esso da un'applicazione effettiva:

$anim = new GifCreator\AnimGif(); 

$gif = $anim->create($image_files); 
//file_put_contents("test.gif", $gif); 

header("Content-type: image/gif"); 
echo $gif; 

(Richiede PHP5.3 con GD2.)

Esempio che funziona con PHP 5.6 e GD 2.4.11:

require_once "AnimGif.php"; 

/* 
* Create an array containing file paths, resource var (initialized with imagecreatefromXXX), 
* image URLs or even binary code from image files. 
* All sorted in order to appear. 
*/ 
$image_files = array(
    //imagecreatefrompng("/../images/pic1.png"), // Resource var 
    //"/../images/pic2.png", // Image file path 
    //file_get_contents("/../images/pic3.jpg"), // Binary source code 
    'https://yt3.ggpht.com/-KxeE9Hu93eE/AAAAAAAAAAI/AAAAAAAAAAA/D-DB1Umuimk/s100-c-k-no-mo-rj-c0xffffff/photo.jpg', // URL 
    'https://media.licdn.com/mpr/mpr/shrinknp_100_100/AAEAAQAAAAAAAAloAAAAJDRkZGY2MWZmLTM1NDYtNDBhOS04MjYwLWNkM2UzYjdiZGZmMA.png', // URL 
    'http://is5.mzstatic.com/image/thumb/Purple128/v4/e4/63/e7/e463e779-e6d0-0c3d-3ec1-97fdbaae230a/source/100x100bb.jpg' // URL 
); 

/* 
* Create an array containing the duration (in millisecond) of each frame. 
*/ 
$durations_millis = array(
    1000, 
    2000, 
    3000 
); 

/* 
* Fix durations. 
*/ 
$durations = array(); 
for ($i = 0; $i < count($durations_millis); $i++) { 
    $durations[$i] = $durations_millis[$i]/10; 
} 

/* 
* Specify number of loops. (0 = infinite looping.) 
*/ 
$num_loops = 0; 

/* 
* Create gif object. 
*/ 
$anim_gif = new GifCreator\AnimGif(); 
$gif_object = $anim_gif->create($image_files, $durations, $num_loops); 

/* 
* Get the animated GIF binary. 
*/ 
$gif_binary = $gif_object->get(); 

/* 
* Set the file name of the saved/returned animated GIF. 
*/ 
$file_name = "animated.gif"; 

/* 
* Optionally, save animated GIF in a folder as a GIF: 
*/ 
//file_put_contents($file_name, $gif_binary); 

/* 
* Optionally, return the animated GIF to client. 
*/ 
header("Content-type: image/gif"); 
header('Content-Disposition: filename="' . $file_name . '"'); // Optional 
echo $gif_binary; 

/* 
* All done. 
*/ 
exit; 
+0

Questo non funziona per il mio (PHP5.6 con GD2.4.11). Mi dà solo "L'immagine" ..."non può essere visualizzato perché contiene errori." –

+1

@ ban-geoengineering, grazie per averlo indicato. (Bene, tutto è possibile, dato che PHP5.6 non esisteva al momento.) Una possibile correzione è stata appena unita al codice Per favore, riprova, e se il problema persiste, per favore invia un problema al progetto su GitHub, in modo che possa essere indirizzato correttamente. –

+0

Grazie per questo. Hai appena aggiornato la tua risposta con lo snippet di codice funzionante. –

Problemi correlati