in reply to How to create JPEG of specified size

With ImageMagick installed,
system("convert xc:white -geometry 200x200 output.jpg");
or
my $image = new Image::Magick(geometry => '200x200'); $image->Read('xc:white'); $image->Write('output.jpg');
See http://www.imagemagick.org/script/perl-magick.php for more details.

If you want to READ a given file, figure out its size, and use that, then you will also want the following:

my $original = new Image::Magick(); $original->Read('original.jpg'); $geometry = $original->Get('geometry');
(The 'xc:____' is a "virtual limitless file" for input, kinda like /dev/urandom, except all pixels are of the named color. You can use #FFFFFF notation here too. Or supply an original image filename without the 'xc:' prefix.)

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re^2: How to create JPEG of specified size
by halley (Prior) on Jul 25, 2007 at 15:06 UTC

    In reference to a private message, Limbic~Region, it sounds like you're more interested in bulking up the file to emulate a large download or a large disk load. There are some thoughts about this.

    If jpg is your target format, bulking it up on disk is easy: write a simple small .jpg file, then append as much random binary garbage that you want after it. This will satisfy your desire to make the file bigger without having to go through some tedious guesstimate loop that makes more PIXELS to satisfy the requirements of more BYTES.

    If your intent is to create a larger download, either for stress testing your system or for poisoning a bandwidth hog, then this is a bit trickier: I don't think any browsers are smart enough to parse the jpg as it reads it (and stops when it reaches the end of valid jpg data), but theoretically one could. If you give a 1x1 pixel image with a megabyte of junk following, the jpg parser could stop after the first hundred bytes and not bother reading or downloading the rest. You'd have to do some tests to ensure you're seeing that the whole thing is getting downloaded.

    If someone's leeching your bandwidth by deeplinking your jpg on your server, and you replace it with a surrogate, I suggest NOT inflating the file to hurt the downloader, since you're ultimately paying the bandwidth on your side too. Make a 1x1 or a highly-compressed "bandwidth hog" image to replace the original.

    With GD or ImageMagick, if you want to make more PIXELS to make a genuine image that happens to require more BYTES (disk or memory), then I suggest you create a small original, then use these APIs to create a "stretched" equivalent of it at larger dimensions. This will ensure the jpg parser must read the whole thing. An image that is all-white or all-red, like the previous examples, will compress VERY well, so you will have a lot of PIXELS that don't take much disk space.

    --
    [ e d @ h a l l e y . c c ]

      halley,
      The fake file has to be a valid JPEG and the purpose for needing this has nothing to do with web servers. If you have any ideas on how to manipulate an dummy image through trial and error to reach an approximate target size I would be most appreciative.

      Cheers - L~R

        Pseudocode, since I don't have ImageMagick installed here.
        # load original image (lena.jpg) # get original image's geometry # do # inflate dimensions by 1.2* # resize image to target geometry # add a little scatter noise to the image # set target quality to a high number like 99 # save target image (lenna.jpg) to disk # while (target image file size is too small);
        The scatter noise and quality numbers are to frustrate jpeg's compression as the image grows.

        Are we talking gigabytes of disk space? I think jpeg as a format will break at 2^15 (or maybe 2^16) pixel dimensions.

        --
        [ e d @ h a l l e y . c c ]