gnu*_*ard 26
图像格式的大多数实现忽略尾随零。因此,将文件填充到所需大小相对简单。(5 兆字节是 10240 个块。)
#! /usr/bin/env bash
print_usage_and_exit(){
echo "Usage: $0 FILE BLOCKS"
echo "This script will pad FILE with zeros up to BLOCKS."
echo "FILE must not already be larger than BLOCKS."
exit
}
FILE=$1
BLOCKS=$2
if [ -z $FILE ] || [ -z $BLOCKS ] || [ -z "${BLOCKS##*[!0-9]*}" ]; then
print_usage_and_exit
fi
FILE_SIZE=$(stat $FILE | awk '/Blocks:/ { print $4 }')
SIZE_DIFFERENCE=$(($BLOCKS-$FILE_SIZE))
if [ $SIZE_DIFFERENCE -le 0 ]; then
print_usage_and_exit
fi
dd if=/dev/zero iflag=append count=$SIZE_DIFFERENCE >> $FILE 2>/dev/null
Run Code Online (Sandbox Code Playgroud)
很少有其他答案会计算您需要追加多少个尾随零,然后再追加。在 Linux 中有这种简单的方法:
truncate -s 5MB image
Run Code Online (Sandbox Code Playgroud)
(假设您需要 5 MB,即 5*1000*1000 字节。对于 5 MiB,即 5*1024*1024 字节,请使用-s 5M
)。
如果原来image
大于指定的大小,那么额外的数据将会丢失。如果最初image
较小,则将添加填充零。如果可能,添加的片段将是稀疏的。
如果出于任何原因您不想要稀疏,请使用fallocate
:
fallocate -l 5MB image
Run Code Online (Sandbox Code Playgroud)
(类似但不完全相同:-l 5MB
5 MB,-l 5MiB
5 MiB)。
fallocate
用这种方式只能扩展image
。如果文件已经至少那么大,那么它的大小不会改变(稀疏性可以改变,我不会详细说明)。
正如评论中所建议的,您可以使用图像元数据。jpg 支持 exif 数据。您提到了 php,它可以使用iptcembed()
. 生成足够长的空字节(二进制 0)字符串以填充文件
<?php
$path = '/path/to/image.jpg';
$size = filesize($path);
$goal = 1024 * 1024 * 5; //5MB
$zeros = str_pad("", $goal - $size, "\0"); // make a string of ($goal - $size) binary 0s
$paddedImage = iptcembed($zeros, $path);
file_put_contents("/path/to/padded-image.jpg", $paddedImage);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4581 次 |
最近记录: |