1

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);
}


?>
  • Imagemagick does not handle APNG format – fmw42 May 16 '19 at 15:18
  • Really? But I saw my example website use it to convert – Hugh Harlequin May 17 '19 at 05:55
  • Run `convert -list format`. If you do not see APNG listed, then Imagemagick does not support it. It is not in my list nor on the web page at https://imagemagick.org/script/formats.php. As far as I know only PNG and MNG are supported – fmw42 May 17 '19 at 06:09
  • Is there any way can convert I really need QAQ – Hugh Harlequin May 17 '19 at 06:12
  • I do not think so with Imagemagick. You should search Google for other tools that might support it. However, if you think others have used it, then try with the command line or post a link to your APNG file and I will give it try. See apngdis under command line tools at http://littlesvr.ca/apng/. I found it by searching Google. I suspect there are other tools. See also https://en.wikipedia.org/wiki/APNG – fmw42 May 17 '19 at 06:19
  • Correction: for apng to gif, see apng2gif at the link I provided just above. – fmw42 May 17 '19 at 06:25
  • I have saw this before ,but I need php or other web commend for user interface – Hugh Harlequin May 17 '19 at 06:36
  • 1
    You can run command line tools using PHP exec() – fmw42 May 17 '19 at 06:40
  • Let me try it thank you ! – Hugh Harlequin May 17 '19 at 06:45

1 Answers1

0

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