php - 如何将 APNG 拆分为 png 图像并通过 PHP 转换为动画 gif
问题描述
我想转换apng
为gif
by imagemagick
,但我不知道该怎么做
我看到使用 PHP 将 APNG 拆分为 png 图像我尝试了它,但它在我的图像中仍然不起作用我类似于
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);
}
?>
解决方案
我下载apng2gif
并将其与我的 php 代码一起使用
<?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!";
}
?>
可以使用 AJAX POST 链接来做 apng2gif 并检查它是否还活着
谢谢 fmw42 :D
推荐阅读
- elixir - ecto 中的复合主键
- php - CORS 标头在浏览器中被更改,导致内容被阻止
- php - Woocommerce 基于重量的自定义运费计算
- python - 在 abc 范围内将移位的 ASCII 值转换为 char
- mysql - 在 MySQL 上将值拆分为多行
- spring-cloud - 如何在 Feign 中使用 http2 以获得更好的 RPC 性能?
- excel - 如何为 Excel 创建满足特定要求的唯一 ID
- spring - 发生错误时如何不使用 JMS 消息
- angular - 在 tomcat 上部署 Angular 6 应用程序
- machine-learning - 迁移学习完全不同的领域和任务