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

总是只显示一半的图片
...全文
154 回复 点赞 打赏 收藏 举报
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
非技术区
加入

7714

社区成员

.NET技术 非技术区
申请成为版主
帖子事件
创建了帖子
2017-11-19 10:27
社区公告
暂无公告