Can I create an image with a specific size in bytes?

Most implementations of image formats ignore trailing zeros. Therefore, it is relatively straightforward to pad a file to a desired size. (5 megabytes is 10240 blocks.)

#! /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

if you insist on Windows - I suggest going to CMD , find a suitable small file ...

>copy /B file1+file1 file2 while replacing file1 and file2 with suitable filenames .. you can repeat said copy command by using file2 as source (first 2 names) .. the files will be binary concatenated - so you get a target file of increasing size - until you are satisfied

not as nice as a script - but it works out of the (windows)-box only needs 1 source file .. that doesn't even necessarily has to be an image - as mime-type for uploads mostly is determined by the file-ending


As suggested in the comments, you can use the image metadata. jpg supports exif data. You mentioned php, which can write raw exif data with iptcembed(). Generate a string of null bytes (binary 0) long enough to pad out the file

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