如何使用PHP冻结动画GIF /转换为jpg?

时间:2013-01-20 00:29:58

标签: php gif

我有一个带有一堆动画GIF的页面,我想使用PHP在页面加载时冻结它们或将每个转换为jpg / png。我遇到了这个http://php.net/manual/en/function.imagecreatefromgif.php,但我对PHP并不太熟悉。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

以下是将动画gif转换为jpg的代码。取自 This comment on the PHP manual entry

<?php

//This function gif2jpeg take three parameter as argument. two argument are optional
//first will take the gif file name. second for file name to save the converted file. third argument is an color array

//EXAMPLE:
$gifName = $_GET['gif_name']; //ABSOLUTE PATH OF THE IMAGE (according to its location)
$c['red']=255;
$c['green']=0;
$c['blue']=0;
echo gif2jpeg($gifName, '', $c);


function gif2jpeg($p_fl, $p_new_fl='', $bgcolor=false){
  list($wd, $ht, $tp, $at)=getimagesize($p_fl);
  $img_src=imagecreatefromgif($p_fl);
  $img_dst=imagecreatetruecolor($wd,$ht);
  $clr['red']=255;
  $clr['green']=255;
  $clr['blue']=255;
  if(is_array($bgcolor)) $clr=$bgcolor;
  $kek=imagecolorallocate($img_dst,
                  $clr['red'],$clr['green'],$clr['blue']);
  imagefill($img_dst,0,0,$kek);
  imagecopyresampled($img_dst, $img_src, 0, 0,
                  0, 0, $wd, $ht, $wd, $ht);
  $draw=true;
  if(strlen($p_new_fl)>0){
    if($hnd=fopen($p_new_fl,'w')){
      $draw=false;
      fclose($hnd);
    }
  }
  if(true==$draw){
    header("Content-type: image/jpeg");
    imagejpeg($img_dst);
  }else imagejpeg($img_dst, $p_new_fl);
  imagedestroy($img_dst);
  imagedestroy($img_src);
}

?>

使用方法:

  • 将上述代码添加到php文件'convertGifToJpeg.php'
  • 添加&lt; img标记。 &lt; img src =“http://apllicationdomainpath/convertGifToJpeg.php?gif_name = / images / animated_gif.gif”width =“200”height =“200”/&gt;
  • 确保为gif图像和php文件指定了正确的名称/路径。