我有下面的代码,它似乎正在生成一个损坏的图像。Photoshop显示"PNG文件被ASCII转换损坏“
$path_pngquant = "pngquant";
$status_code = null;
$image = null;
$output = null;
$image_input_escaped = escapeshellarg('test.png');
$command = "$path_pngquant --strip -- - < $image_input_escaped";
// Execute the command
exec($command, $output, $status_code);
if($status_code == 0)
{
//0 means success
$image = implode('', $output);
file_put_contents('test_2.png', $image);
}发布于 2020-07-01 11:33:35
exec会弄乱二进制流,您需要的是以二进制模式打开从其读取的输出流。幸运的是,popen正是为此而设计的
<?php
$path_pngquant = "pngquant";
$status_code = null;
$image = null;
$output = null;
$image_input_escaped = escapeshellarg('test.png');
$command = "$path_pngquant --strip -- - < $image_input_escaped";
// Execute the command
$handle = popen($command . '2>&1', 'rb'); //MODE : r =read ; b = binary
if($handle){
$output = '';
while (!feof($handle)) {
echo $output;
$output .= fread($handle, 10240); //10240 = 10kb ; read in chunks of 10kb , change it as per need
}
fclose($handle);
file_put_contents('test_2.png', $output);
}2>&1是在常用shell脚本中使用的redirection syntax
https://stackoverflow.com/questions/62668086
复制相似问题