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

如何使用PHP冻结动画GIF/转换为jpg?,php,gif,Php,Gif,我有一个带有一堆动画GIF的页面,我想使用PHP在页面加载时冻结它们,或者将每个GIF转换为jpg/png。我遇到过这个问题,但我对PHP不太熟悉。有什么帮助吗?以下是将动画gif转换为jpg的代码。摘自 <?php //This function gif2jpeg take three parameter as argument. two argument are optional //first will take the gif file name. second for file

我有一个带有一堆动画GIF的页面,我想使用PHP在页面加载时冻结它们,或者将每个GIF转换为jpg/png。我遇到过这个问题,但我对PHP不太熟悉。有什么帮助吗?

以下是将动画gif转换为jpg的代码。摘自

<?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”中
  • 添加
  • 确保为gif图像和php文件提供了正确的名称/路径

这个问题的可能重复:(可能没有太大帮助)本页下面的第二个示例可能会有所帮助:我尝试过,但我一直遇到这样的错误“资源被解释为图像,但使用MIME类型文本/html传输”。您可以发布您生成的代码吗?请不要将GIF转换为JPG。您将获得压缩伪影。JPEG仅用于照片。您可以将动画GIF转换为非动画GIF或PNG,而不会降低质量。GIF->JPEG将失去质量。Hi@cusejuice请让我知道这是否有帮助。您编写了此函数吗?如果不是,请确定作者的身份。