Allowed memory size of 33554432 bytes exhausted (tried to allocate 2558 bytes)

Vidíte často tuto hlášku při zpracování obrázků? Možná nechápete proč, když zpracováváte obrázek o velikosti 500kb a máte memory_limit 16M. A chcete vědět jak zabránit pádu skriptu nebo předejít vyřerpání paměti před operacemi s obrázky?


Proč se vyčerpá paměť při zpracování obrázku v PHP?

Problém je v tom, že při použití funkcí imagecreatefrompng, imagecreatefromjpg, imagecreatefromgif a další, php nejdříve zpracovávaný obrázek převede na nekomprimovaný a ten je mnohonásobně větší.

Přesto, že zpracováváte obrázek o velikosti 500kB můžete vyčerpat více než 32MB vaší povolené paměti pro php skripty. V takové situaci by bylo vhodné vypočítat potřebnou paměť pro zpracování obrázku a oznámit uživateli a skriptu, že tudy cesta nevede. Další možností je v takovém případě zvětšit velikost povoleného limitu paměti, ale na to pravděpodobně na většině hostingů nebudete mít právo.

Řešení je následující:

  1. $size=getimagesize($image);
  2. $memoryneed = round(($size[0] * $size[1] * $size['bits'] * $size['channels'] / 8 + pow(2, 16)) * 1.65);
  3. if (function_exists('memory_get_usage') && ((memory_get_usage() + $memoryneed) > ((integer)ini_get('memory_limit') * pow(1024, 2)))) {
  4. ...zpracovat takovýto obrázek nebude možné, měli bychom vyhodit upozornění a zastavit skript...
  5. ...nebo můžeme nastavit limit paměti...
  6. ini_set('memory_limit', (integer) ini_get('memory_limit') + ceil(((memory_get_usage() + $memoryneed) - (integer) ini_get('memory_limit') * pow(1024, 2)) / pow(1024, 2)) . 'M');
  7. }

komentáře

RSS Komentáře k článku RSS Komentáře   Add to Google
20.12.2008 23:33 | Anonym (Lukyer) | skript

perfektni vypocet, hodi se na osefovani hlasek pro uzivatele. Diky moc

01.03.2017 12:42 | Anonym (lzm) | lzm

asics shoes Ralph Lauren Polo mlb shop Jordan retro michael kors australia Nike Air Max moncler outlet burberry outlet sunglasses sm salomon shoes oakley australia chanel australia burberry outlet ugg australia prada bags Coach outlet online burberry scarf beats headphones sunglasses hut Hugo Boss Outlet abercrombie outlet us ugg boots kate spade black friday adidas yeezy boost 350 pandora.com football jerseys tiffany co ray ban online nike shoes michael kors tote michael kors tote michael kors black friday prada factory outlet kate spade sunglasses outlet coach usa cheap coach purse prada official site nike jordan shoes kate spade outlet moncler jacket mens coach factory outlet mcm outlet online Jordan Retro louis vuitton australia cheap basketball shoes pandora ring nike outlet louis vuitton factory outlet pandora australia pandora necklace charms michael kors uk abercrombie paris michael kors outlet coach outlet canada goose sale chanel bag michael kors Oakley Outlet michael kors outlet Ralph Lauren prada outlet prada bags Canada Goose Outlet Prada Factory Outlet Kevin Durant Shoes Chanel Outlet pandora bracelet pandora charms sale online burberry outlet canada coach outlet online ugg slippers longchamp outlet nike shoes abercrombie & fitch coach outlet Prada Outlet Burberry outlet online michael kors prada handbags prada outlet Mizuno Shop Michael Kors Outlet pandora rings abercrombie outlet football jerseys moncler coats nike australia kate spade outlet nike shox shoes prada us coach outlet store ray-ban sunglasses nike jordan shoes piumini moncler replica mk handbags abercrombie and fitch ups tracking canada goose sale online coach outlet online moncler uk burberry outlet canada nike shoes australia coach bags ray ban aviator kate spade prada bag Chanel bags Outlet kate spade outlet nike outlet pandora charms chanel polo outlet michael kors outlet michael kors bags outlet Nike Jordans burberry purse michael kors outlet michael kors outlet pandora australia oakley frogskins yeezy boost 350 shoes prada outlet ray ban polarized prada sale Cheap Ray Ban burberry outlet online burberry scarfs michael kors outlet KD 8 shoes shoes online sale michael kors outlet michael kors outlet ray-ban sunglasses louis vuitton uk Free Run burberry australia coach factory outlet Prada bags Nike Air Jordan pandora rings Pandora bracelet michael kors kate spade bags pandora rings adidas australia pandora bracelets moncler clothing michael kors bags air jordan shoes Nike Hyperdunk 2015 hockey jerseys michael kors purses ugg australia pandora jewelry michael kors purses authentic prada handbags outlet Nike Jordan Shoes pandora rings jewelry pandora charms black friday véronique Billat michael kors outlet pandora rings sale prada outlet online michael kors jet set tote rayban aviator shoes online kate spade mlb shop louis vuitton outlet coach outlet moncler sale nike lebron james ray ban sunglasses official prada site Coach Sunglasses Outlet yeezy boost 350 oakley sunglasses kate spade outlet nike huarache shoes louis vuitton outlet burberry online ray ban prescription sunglasses michael kors outlet tiffany co kate spade canada michael kors outlet brand sunglasses cheap moncler jacket coach diaper bag coach outlet Oakley Holbrook sunglasses hut lebron james shoes coach outlet prada sunglasses Hugo Boss Store coach outlet coach outlet pandora uk michael kors outlet Nike Hyperdunk Shoes michael kors handbags michael kors italy burberry pandora princess ring baseball jerseys Yeezy Boost 350 nike shox louis vuitton outlet basketball jerseys Hermes Outlet MIZUNO Shop US michael kors outlet prada outlet online moncler outlet moncler outlet louboutin shoes coach factory outlet pandora rings mlb.com prada outlet online flash sunglasses ray ban sunglasses burberry canada coach carter air jordan retro burberry scarf michael kors totes michael kors bags burberry outlet kate spade black friday coach factory outlet online abercrombie fitch michael kors handbags outlet michael kors purses outlet pandora rings shoes sale pandora charms uk Nike Free nike basketball shoes nike huarache timberland uk moncler jacket paul smith sale coach handbags burberry canada abercrombie outlet yeezy boost 350 beats studio asics Australia yeezy boost 350 jimmy choo australia michael kors online coach online abercrombie outlet hermes bag Coach Outlet Store pandora necklaces official michael kors salomon hiking shoes coach outlet coach outlet canada goose jacket coach factory outlet prada purses michael kors outlet online mens sunglasses authentic prada outlet online prada outlet online coach outlet online basketball jerseys ray-ban sunglasses oakley sunglasses kate spade outlet online prada online ralph lauren australia coach shoes ray ban outlet Oakley Holbrook sunglasses michael kors outlet bags michael kors outlet ray ban sunglasses huaraches shoes Mizuno Shop Japan coach australia michael kors uk nike australia Mizuno Wave coach bags mcm outlet baseball jerseys michael kors outlet major league baseball Yeezy 350,Boost 350 abercrombie outlet canada goose jacket coach outlet Hyperdunk 2016 pandora charms Basketball shoes nike com mcm factory outlet nike free run burberry outlet adidas shoes michael kors outlet michael kors outlet michael kors outlet online michael kors outlet online Nike KD 8 prada outlet online prada handbags pandora charms sale pandora rings moncler mens jackets Billat michael kors handbags Michael Kors Canada michael kors outlet coach outlet prada factory outlet online moncler down jackets snow boots abercrombie us prada handbags burberry scarf CHanel Factory Outlet kate spade bags coach outlet pandora jewelry adidas australia mk bags kate spade outlet michael kors outlet Jordan Retro Shoes pandora necklace michael kors handbags michael kors longchamp outlet landing gears oakley frogskins sunglasses coach mlb store coach australia authentic prada outlet online polo online ray ban prescription glasses pandora uk ray ban outlet pandora australia prada outlet online abercrombie and fitch burberry online huarache nike nike air huarache jordan retro shoes ugg uk nfl jerseys michael kors wallet oakley sunglasses burberry outlet online Michael Kors Outlet oakley outlet louis vuitton outlet air jordan shoes michael kors us ray ban wayfarer nike id paul smith uk pandora charms Mizuno Running Shoes coach purse kate spade outlet online landing gear michael kors purses on sale michael kors outlet pandora rings kate spade outlet china wholesale coach australia prada factory outlet online pandora australia huaraches jordan shoes pandora charms ray ban glasses pandora charms abercrombie and fitch prada outlet online prada bags outlet china factory sale prada glasses coach outlet online ray ban new wayfarer coach outlet,coach factory pandora charms sale clearance abercrombie us kate spade handbags canada goose outlet burberry us clk benz coach purses burberry outlet Abercrombie Fitch abercrombie outlet Kate Spade Australia michael kors outlet baseball jerseys paul smith asics shoes Australia wholesale Hugo Boss Online michael kors factory outlet nfl shop Coach Outlet beats by dre pandora bracelet landinggear ate spade handbags michael kors bags prada outlet pandora charms sale Coach Outlet Online prada outlet veronique billat moncler jacket cheap ray ban sunglasses prada factory outlet online prada outlet prices prada bags on sale polo outlet online mk outlet prada outlet bercrombie Nederland coach sunglasses for women moncler jacket michael kors canada pandora jewelry Adidas Yeezy Boost 350 coach handbags Nike Air Jordan burberry australia nike jordan shoes prada bags ray ban eyeglasses ray ban clubmaster nike air max pandora bracelet kate spade bags outlet Pandora Charm michael kors outlet moncler us michael kors outlet prada outlet online coach outlet coach bag Longchamp Outlet Mizuno Shoes coach sunglasses ray ban sunglasses Ray Ban Outlet michael kors outlet burberry outlet pandora charms official prada site pandora rings coach outlet coach factory top sunglasses nike jordan ralph lauren nike store michael kors outlet nike lebron shoes timberland boots lzm3.1

Jméno
Název
Text
Lze používat Texy! syntax. Příklad syntaxe: "text odkazu":odkaz, **tučně**, *kurzíva*, `code`. PHP kód uzavírejte do <?php ... ?> a JavaScript do <script> ... </script>