dongna9185 2019-05-16 08:05
浏览 479

如何将APNG拆分为png图像并通过PHP转换为动画gif

I want to convert apng to gif by imagemagick, but I don't know how to do

I see Splitting APNG into png images with PHP I try it but it still no work in my image I something like
1.ezgif.com/apng-to-gif
2.aconvert.com/image/apng-to-gif

<?php
function splitapng($data) {
  $parts = array();

  // Save the PNG signature   
  $signature = substr($data, 0, 8);
  $offset = 8;
  $size = strlen($data);
  while ($offset < $size) {
    // Read the chunk length
    $length = substr($data, $offset, 4);
    $offset += 4;

    // Read the chunk type
    $type = substr($data, $offset, 4);
    $offset += 4;

    // Unpack the length and read the chunk data including 4 byte CRC
    $ilength = unpack('Nlength', $length);
    $ilength = $ilength['length'];
    $chunk = substr($data, $offset, $ilength+4); 
    $offset += $ilength+4;

    if ($type == 'IHDR')
      $header = $length . $type . $chunk;  // save the header chunk
    else if ($type == 'IEND')
      $end = $length . $type . $chunk;     // save the end chunk
    else if ($type == 'IDAT') 
      $parts[] = $length . $type . $chunk; // save the first frame
    else if ($type == 'fdAT') {
      // Animation frames need a bit of tweaking.
      // We need to drop the first 4 bytes and set the correct type.

      $length = pack('N', $ilength-4);
      $type = 'IDAT';
      $chunk = substr($chunk,4);
      $parts[] = $length . $type . $chunk;
    }
  }

  // Now we just add the signature, header, and end chunks to every part.
  for ($i = 0; $i < count($parts); $i++) {
    $parts[$i] = $signature . $header . $parts[$i] . $end;
  }

  return $parts;
}
$filename = 'A.png';

$handle = fopen($filename, 'rb');
$filesize = filesize($filename);
$data = fread($handle, $filesize);
fclose($handle);

$parts = splitapng($data);

for ($i = 0; $i < count($parts); $i++) {
  $handle = fopen("part-$i.png",'wb');
  fwrite($handle,$parts[$i]);
  fclose($handle);
}


?>
  • 写回答

1条回答 默认 最新

  • doulingqiu4349 2019-05-18 05:22
    关注

    i download apng2gif
    and put it with my php with this code

    <?php
    header("Content-Type:text/html; charset=utf-8");
    
    
    $link = substr($_POST["link"],0,-7).'_animation'.substr($_POST["link"],-7);
    
    
    $linkname = substr($link,57,-32);
    function link_code($link) {
      $headers = get_headers($link);
      return substr($headers[0], 9, 3);
    }
    
    $link_code = link_code($link);
    
    if( $link_code == 200 ) {
    file_put_contents($linkname.".png", fopen($link, 'r'));
      $x = iconv("cp950","UTF-8",shell_exec("apng2gif ".$linkname.".png ".$linkname.".gif"));
      echo "OKAY!";
    }
    else{
      echo "Nokay!";
    }
    ?>
    

    Can use AJAX POST link to do apng2gif and check is it alive or not
    thank fmw42 :D

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘