Voting

: min(zero, nine)?
(Example: nine)

The Note You're Voting On

dmhouse at gmail dot com
17 years ago
For a script that allows you to calculate the "fudge factor" discussed below by Karolis and Yaroukh, try the following. Grab a few images, preferably some large ones (the script should cope with images of up to 10 megapixels or so), some small ones, and some ones in between. Add their filenames to the $images array, then load the script in your browser.

<?php

header
('Content-Type: text/plain');

ini_set('memory_limit', '50M');

function
format_size($size) {
if (
$size < 1024) {
return
$size . ' bytes';
}
else {
$size = round($size / 1024, 2);
$suffix = 'KB';
if (
$size >= 1024) {
$size = round($size / 1024, 2);
$suffix = 'MB';
}
return
$size . ' ' . $suffix;
}
}

$start_mem = memory_get_usage();

echo <<<INTRO
The memory required to load an image using imagecreatefromjpeg() is a function
of the image's dimensions and the images's bit depth, multipled by an overhead.
It can calculated from this formula:
Num bytes = Width * Height * Bytes per pixel * Overhead fudge factor
Where Bytes per pixel = Bit depth/8, or Bits per channel * Num channels / 8.
This script calculates the Overhead fudge factor by loading images of
various sizes.
INTRO;

echo
"\n\n";

echo
'Limit: ' . ini_get('memory_limit') . "\n";
echo
'Usage before: ' . format_size($start_mem) . "\n";

// Place the images to load in the following array:
$images = array('image1.jpg', 'image2.jpg', 'image3.jpg');
$ffs = array();

echo
"\n";

foreach (
$images as $image) {
$info = getimagesize($image);
printf('Loading image %s, size %s * %s, bpp %s... ',
$image, $info[0], $info[1], $info['bits']);
$im = imagecreatefromjpeg($image);
$mem = memory_get_usage();
echo
'done' . "\n";
echo
'Memory usage: ' . format_size($mem) . "\n";
echo
'Difference: ' . format_size($mem - $start_mem) . "\n";
$ff = (($mem - $start_mem) /
(
$info[0] * $info[1] * ($info['bits'] / 8) * $info['channels']));
$ffs[] = $ff;
echo
'Difference / (Width * Height * Bytes per pixel): ' . $ff . "\n";
imagedestroy($im);
$start_mem = memory_get_usage();
echo
'Destroyed. Memory usage: ' . format_size($start_mem) . "\n";

echo
"\n";
}

echo
'Mean fudge factor: ' . (array_sum($ffs) / count($ffs));

?>

<< Back to user notes page

To Top