note 76968 added to function.imagecreatefromjpeg

From: Date: Wed, 08 Aug 2007 10:37:58 +0000
Subject: note 76968 added to function.imagecreatefromjpeg
Groups: php.notes 
Request: Send a blank email to php-notes+get-129289@lists.php.net to get a copy of this message
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)); ?> ---- Server IP: 217.174.196.197 Probable Submitter: 212.155.191.21 ---- Manual Page -- http://www.php.net/manual/en/function.imagecreatefromjpeg.php Edit -- https://master.php.net/note/edit/76968 Del: integrated -- https://master.php.net/note/delete/76968/integrated Del: useless -- https://master.php.net/note/delete/76968/useless Del: bad code -- https://master.php.net/note/delete/76968/bad+code Del: spam -- https://master.php.net/note/delete/76968/spam Del: non-english -- https://master.php.net/note/delete/76968/non-english Del: in docs -- https://master.php.net/note/delete/76968/in+docs Del: other reasons-- https://master.php.net/note/delete/76968 Reject -- https://master.php.net/note/reject/76968 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129289) next »