php allowed memory size exhausted on image upload rotation -
php allowed memory size exhausted on image upload rotation -
i have script uploads images , rotates them depending on orientation , i'm experiencing problem when image has exif tags uploaded, error saying:
allowed memory size of 33554432 bytes exhausted (tried allocate 10368 bytes.
and line referring in error log.
i did notice it's happening images have exif tags. if normal images, generated photoshop or uploaded, works without problems.
the actual image orientation code following:
function correctimageorientation($fullpath) { if (function_exists('exif_read_data')) { $exif = exif_read_data($fullpath); if($exif && isset($exif['orientation'])) { $orientation = $exif['orientation']; if($orientation != 1){ $img = imagecreatefromjpeg($fullpath); $deg = 0; switch ($orientation) { case 3: $deg = 180; break; case 6: $deg = 270; break; case 8: $deg = 90; break; } if ($deg) { $img = imagerotate($img, $deg, 0); } // rewrite rotated image disk $filename imagejpeg($img, $fullpath, 100); } // if there rotation necessary } // if have exif orientation info } // if function exists } the exact line in error_log memory problem happens 1 says:
$img = imagerotate($img, $deg, 0); the way calling in script following:
$dirname = session::value('user_id'); $rotatedfile = '/home/myfolder/public_html/'.$dirname.'/'.$file_name; $rotatedfile = $this->correctimageorientation($rotatedfile); what trying accomplish rotated image gets saved in same place original file, replacing it.
again, happening images contain exif information. others uploaded without problems.
what causing memory allocation problem?
your error this:
allowed memory size of 33554432 bytes exhausted (tried allocate 10368 bytes).
33554432 bytes converts 32 megabytes. means php ran out of memory while trying work.
you claim images fail have exif info, doesn’t ring true me cause of this. irregardless, quick solution issue increment php memory function adding , ini_set line connected memory_limit function
for example, add together here after if (function_exists('exif_read_data')) { check. setting 64m since double scripts memory capacity when runs function. code here:
function correctimageorientation($fullpath) { if (function_exists('exif_read_data')) { ini_set('memory_limit', '64m'); $exif = exif_read_data($fullpath); if($exif && isset($exif['orientation'])) { $orientation = $exif['orientation']; if($orientation != 1){ $img = imagecreatefromjpeg($fullpath); $deg = 0; switch ($orientation) { case 3: $deg = 180; break; case 6: $deg = 270; break; case 8: $deg = 90; break; } if ($deg) { $img = imagerotate($img, $deg, 0); } // rewrite rotated image disk $filename imagejpeg($img, $fullpath, 100); } // if there rotation necessary } // if have exif orientation info } // if function exists } what trying accomplish rotated image gets saved in same place original file, replacing it.
the problem using gd library in php eat memory when php loads file scheme & eat more memory when attempting rotate image.
it images have exif info have higher dpi standard 72dpi. while dimensions might seem superficially same image without exif info, 300dpi image 4 times larger in size 72dpi image. why images failing; not exif info overall dpi.
now can alter memory limit in php.ini changing line read memory_limit = 32m. , technically work. don’t consider practice 1 script of function failing.
that’s because when alter setting in php.ini increases ram php interactions; not problem issue. server eating more ram apache (which runs php) basic functions oddball function eats more ram. meaning if code accessed handful of times day, why burden larger server more happy 32m of ram per php process? improve utilize ini_set('memory_limit', '64m'); isolate ram increment needs this.
php image memory upload
Comments
Post a Comment