【发布时间】:2019-05-16 08:05:13
【问题描述】:
我想通过imagemagick将apng转换为gif,但我不知道该怎么做
我看到Splitting APNG into png images with PHP 我尝试了,但它在我的图像中仍然不起作用
我喜欢
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 不处理 APNG 格式
-
真的吗?但我看到我的示例网站使用它来转换
-
运行
convert -list format。如果您没有看到 APNG 列出,则 Imagemagick 不支持它。它不在我的列表中,也不在imagemagick.org/script/formats.php 的网页上。据我所知,仅支持 PNG 和 MNG -
有什么方法可以转换我真的需要QAQ
-
您可以使用 PHP exec() 运行命令行工具
标签: php imagemagick gd apng