0

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

4

1 回答 1

0

这是将动画 gif 转换为 jpg 的代码。取自PHP 手册条目的此评论

<?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”中
  • 添加一个 <img 标签。<img src="http://apllicationdomainpath/convertGifToJpeg.php?gif_name=/images/animated_gif.gif" width="200" height="200" />
  • 确保为 gif 图像和 php 文件提供了正确的名称/路径。
于 2013-01-20T06:53:51.583 回答