php修护ipa图片总是只能显示一半,代码如下,有人能解决不?

蓝冰网 2017-11-19 10:27:43
<?php

class unCrush
{

private $pngFilePath;

public function __construct($path = null)
{
session_start();
if(!$_SESSION['memuserid']){exit;}
if ($path !== null) {
$this->pngFilePath = $path;
}
}

/**
* Decodes optimized pngs
* @param string $path
* @return string $img or an epmty string if path was specified
*/
public function decode($path = null)
{
$img = $this->pdecode();

if (!$img) {
return false;
}

if ($path !== null) {

file_put_contents($path, $img);
return true;
} else {
return $img;
}

}

/**
* Decodes Apple optimized png
* @return string $imageData
*/
private function pdecode()
{

$fh = fopen($this->pngFilePath, 'r');

$headerData = fread($fh, 8);

$header = unpack("C1highbit/A3signature/C2lineendings/C1eof/C1eol", $headerData);
// check if it's a PNG image
if (!is_array($header) && !$header['highbit'] == 0x89 && !$header['signature'] == "PNG") {
return false;
}

$chunks = array();
$isIphoneCompressed = false;
$cnt = 0;
$uncompressed = '';
while (!feof($fh)) {
$data = fread($fh, 8);
if (strlen($data) > 0) { // Fix for empty parts
// Unpack the chunk
$chunk = unpack("N1length/A4type", $data); // get the type and length of the chunk
$data = @fread($fh, $chunk['length']); // can be 0...
$dataCrc = fread($fh, 4); // get the crc
$crc = unpack("N1crc", $dataCrc);
$chunk['crc'] = $crc['crc'];

// This chunk is first when it's a iPhone compressed image
if ($chunk['type'] == 'CgBI') {
$isIphoneCompressed = true;
}
// Extract the header if needed
if ($chunk['type'] == 'IHDR' && $isIphoneCompressed) {
$width = unpack('N*', substr($data, 0, 4));
$height = unpack('N*', substr($data, 4, 4));
$width = $width[1]; //180
$height = $height[1]; //180

$depth = unpack('C1', substr($data, 8, 1));
$depth = $depth[1]; // 8

$ctype = unpack('C1', substr($data, 9, 1));
$ctype = $ctype[1]; // 6

$compression = unpack('C1', substr($data, 10, 1));
$compression = $compression[1]; //0

$filter = unpack('C1', substr($data, 11, 1));
$filter = $filter[1]; // 0

$interlace = unpack('C1', substr($data, 11, 1));
$interlace = $interlace[1]; // 0

}

// Extract and mutate the data chunk if needed (can be multiple)
if ($chunk['type'] == 'IDAT' && $isIphoneCompressed) {
set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) {

return false;

// error was suppressed with the @-operator
if (0 === error_reporting())
return false;

//throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});

try {
$uncompressed .= gzinflate($data);
} catch (Exception $e) {
var_dump($e->getMessage());
restore_error_handler();
return false;
}


}


$chunk['data'] = $data;

// Add the chunk to the chunks array so we can rebuild the thing
$chunks[] = $chunk;

}
}
// IHDR
$out = $headerData;
$out .= pack('N', $chunks[1]['length']);
$out .= $chunks[1]['type'];
$out .= $chunks[1]['data'];
$out .= pack('N', $chunks[1]['crc']);
// data stream
$newData = '';
for ($y = 0; $y < $height; $y++) {
$i = strlen($newData); // setting the offset
$newData .= $uncompressed[$i]; // inject the first pixel, don't know why...
for ($x = 0; $x < $width; $x++) {
$i = strlen($newData); // setting the offset
// Now we need to swap the BGRA to RGBA
$newData .= $uncompressed[$i + 2]; // Place the Red pixel
$newData .= $uncompressed[$i + 1]; // Place the Green pixel
$newData .= $uncompressed[$i + 0]; // Place the Blue pixel
$newData .= $uncompressed[$i + 3]; // Place the Aplha byte
}

}
$compressed = gzcompress($newData);
$out .= pack('N', strlen($compressed));
$out .= 'IDAT';
$out .= $compressed;
$out .= pack('N', crc32('IDAT' . $compressed));

// IEND
$out .= pack('N', 0);
$out .= 'IEND';
$out .= pack('N', crc32('IEND' . null));

return $out;

}

public function setPath($path)
{
if ($path !== null) {
$this->pngFilePath = $path;
}
}
}

使用方法:
require_once 'unCrush.php';
$png = new unCrush('logo.png');
$base64_img = $png->decode();
$new= fopen($path.'/api/zip/22.png','wb');
fwrite($new,$base64_img);
fclose($new);

总是只显示一半的图片
...全文
275 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
打开链接下载源码: https://pan.quark.cn/s/d85aa9a28238 FreeRTOS,其全称为“Free Real-Time Operating System”,是一款具备开源特质且设计轻巧的实时操作系统(RTOS),主要应用于嵌入式系统领域。该“freertos工程文件以及官方文档.rar”压缩包内含了与FreeRTOS关联的工程文件、官方文档资料以及其他用于移植的文件,是进行FreeRTOS学习与应用的关键性资源。 FreeRTOS的核心特质涵盖任务调度机制、中断处理功能、同步机制设计以及内存管理策略。其中,任务调度机制是其核心功能,借助优先级抢占式调度算法,保障高优先级任务能够获得及时响应。中断处理功能是嵌入式系统中的关键环节,FreeRTOS支持在中断执行环境中开展特定操作,同时确保任务执行状态的安全。同步机制例如信号量、互斥量及事件标志组,主要用于实现任务之间的通信与协调。内存管理部分则负责动态分配和释放内存资源,以满足不同任务的特定需求。 工程文件通常由源代码、配置文件、编译脚本等构成,能够帮助开发者深入理解FreeRTOS的工作原理,并在此基础上进行定制化开发。例如,`FreeRTOSConfig.h`作为FreeRTOS的核心配置文件,允许用户依据目标硬件条件和应用需求调整内核参数,比如任务数量、堆大小等。 官方文档资料是学习FreeRTOS的重要参考资料,其内容详尽地阐述了系统架构、API函数、应用示例等关键信息。通过研读这些文档,开发者可以掌握如何创建任务、配置中断、实现任务间的同步与通信,以及如何对FreeRTOS系统进行调试和优化。 移植文件主要涉及将FreeRTOS部署于不同的微控制器(MCU)或硬件平台之上。每个平...

7,776

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧