php只有一半的 >,php解析ipa应用图标只能解析出一半的问题

问题

工作中碰到一个问题,需要使用php来解析上传的ipa应用图标,经过多方搜索,找到一短php程序可行进行此操作,但是出现一个致命问题:解析出来的的应用图标只有一半,因此特来请教各位大侠指点一二。

资源

核心文件unCrush.php

class unCrush

{

private $pngFilePath;

public function __construct($path = null)

{

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;

}

}

}

需要解析的图片

SF无法解析该图片,故改为百度网盘链接

测试代码index.php

ini_set('error_reporting', 'E_ALL & ~E_NOTICE');

header('Content-type: image/png');

require_once './unCrush.php';

$file = './test.png';

$png = new unCrush($file);

echo $png->decode();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值