Fatal error: Allowed memory size of 33554432 bytes exhausted

Discussion in 'PHP' started by bevjones, Mar 15, 2009.

  1. #1
    hi there - I'm a newbie and I'm after some help.

    I have tried to get my head around this problem, but now I have been advised there is a fault in the code.

    Is anyone able to assist me with the above error message I'm getting?

    I have pasted the code for anyone to help.

    I really, really appreciate any and all of the help and time you can assist and advise me.

    Warmest regards

    Bev.

    <?php

    function Add_date($datefrom, $leng)
    {
    $today = getdate($datefrom);
    $month = $today['mon'];
    $day = $today['mday'];
    $year = $today['year'];
    return mktime (0,0,0,$month,$day+$leng,$year);
    }

    // Bat loi !!!
    function insert($arr, $table)
    {
    while (list($_key, $_val) = each($arr))
    {
    $key[] = $_key;
    $val[] = $_val;
    }
    $arr_str = array();
    for ($i = 0; $i < count($val); $i++)
    {
    if (is_int($val[$i])) $arr_str[] = $val[$i];
    else $arr_str[] = "'" . $val[$i] . "'";
    }
    $fields = implode(",", $key);
    $values = implode(",", $arr_str);

    $sql = "INSERT INTO " . $table . "($fields) VALUES ($values)";


    $result = mysql_query($sql);
    if ($result) return true;
    else return false;
    }

    function update($arr, $table, $arr_where)
    {
    while (list($_key, $_val) = each($arr))
    {
    $key[] = $_key;
    $val[] = $_val;
    }
    $arr_str = array();
    for ($i = 0; $i < count($val); $i++)
    {
    if (is_int($val[$i])) $arr_str[] = $key[$i] . "=" . $val[$i];
    else $arr_str[] = $key[$i] . "='" . $val[$i] . "'";
    }
    $values = implode(",", $arr_str);

    $key = array();
    $val = array();
    while (list($_key, $_val) = each($arr_where))
    {
    $key[] = $_key;
    $val[] = $_val;
    }
    $arr_str = array();
    for ($i = 0; $i < count($val); $i++)
    {
    if (is_int($val[$i])) $arr_str[] = $key[$i] . "=" . $val[$i];
    else $arr_str[] = $key[$i] . "='" . $val[$i] . "'";
    }
    $where = implode(" OR ", $arr_str);

    $sql = "UPDATE " . $table . " SET $values WHERE $where";

    $result = mysql_query($sql);
    if ($result) return true;
    else return false;
    }

    function delete($table, $arr_where)
    {
    $key = array();
    $val = array();
    while (list($_key, $_val) = each($arr_where))
    {
    $key[] = $_key;
    $val[] = $_val;
    }
    $arr_str = array();
    for ($i = 0; $i < count($val); $i++)
    {
    if (is_int($val[$i])) $arr_str[] = $key[$i] . "=" . $val[$i];
    else $arr_str[] = $key[$i] . "='" . $val[$i] . "'";
    }
    $where = implode(" OR ", $arr_str);

    $sql = "DELETE FROM " . $table . " WHERE $where";
    $result = mysql_query($sql);
    if ($result) return true;
    else return false;
    }


    function str2Time($format,$t)//$t: dateTime in format: ddmmyyyyhhiiss (14 chars)
    {
    $epochTime=mktime(substr($t,8,2),substr($t,10,2),substr($t,12,2),substr($t,4,2),substr($t,6,2),substr($t,0,4));
    return date($format,$epochTime);
    }

    function GetStarRate($tpldir, $tut_rating){
    $starimg = "";

    if((($tut_rating >= 0)or($tut_rating == 0)) && ($tut_rating <= 0.50)){
    $starimg = "<img src='".$tpldir."/images/0o5.gif' width='70' height='18'>";
    }
    if((($tut_rating >= 0.50)or($tut_rating == 0.50)) && ($tut_rating <= .99)){
    $starimg = "<img src='".$tpldir."/images/05o5.gif' width='70' height='18'>";
    }
    if((($tut_rating >= 1.00)or($tut_rating == 1.50)) && ($tut_rating <= 1.49)){
    $starimg = "<img src='".$tpldir."/images/1o5.gif' width='70' height='18'>";
    }
    if((($tut_rating >= 1.50)or($tut_rating == 1.50)) && ($tut_rating <= 1.99)){
    $starimg = "<img src='".$tpldir."/images/15o5.gif' width='70' height='18'>";
    }
    if((($tut_rating >= 2.00)or($tut_rating == 2.00)) && ($tut_rating <= 2.49)){
    $starimg = "<img src='".$tpldir."/images/2o5.gif' width='70' height='18'>";
    }

    if((($tut_rating >= 2.50)or($tut_rating == 2.50)) && ($tut_rating <= 2.99)){
    $starimg = "<img src='".$tpldir."/images/25o5.gif' width='70' height='18'>";
    }

    if((($tut_rating >= 3.00)or($tut_rating == 3.00)) && ($tut_rating <= 3.49)){
    $starimg = "<img src='".$tpldir."/images/3o5.gif' width='70' height='18'>";
    }

    if((($tut_rating >= 3.50)or($tut_rating == 3.50)) && ($tut_rating <= 3.99)){
    $starimg = "<img src='".$tpldir."/images/35o5.gif' width='70' height='18'>";
    }
    if((($tut_rating >= 4.00)or($tut_rating == 4.00)) && ($tut_rating <= 4.49)){
    $starimg = "<img src='".$tpldir."/images/4o5.gif' width='70' height='18'>";
    }
    if((($tut_rating >= 4.50)or($tut_rating == 4.50)) && ($tut_rating <= 4.99)){
    $starimg = "<img src='".$tpldir."/images/45o5.gif' width='70' height='18'>";
    }
    if($tut_rating == 5.0){
    $starimg = "<img src='".$tpldir."/images/5o5.gif' width='70' height='18'>";
    }

    return $starimg;
    }
    //


    //$filename should be a JPG and $watermark a PNG-24 with alpha transparency. $quality is 1-100 JPG quality on output.
    function watermark($srcfilename, $newname, $watermark, $quality) {
    $imageInfo = getimagesize($srcfilename);
    $width = $imageInfo[0];
    $height = $imageInfo[1];
    $logoinfo = getimagesize($watermark);
    $logowidth = $logoinfo[0];
    $logoheight = $logoinfo[1];
    $horizextra =$width - $logowidth;
    $vertextra =$height - $logoheight;
    $horizmargin = round($horizextra / 2);
    $vertmargin = round($vertextra / 2);
    $photoImage = ImageCreateFromJPEG($srcfilename);
    ImageAlphaBlending($photoImage, true);
    $logoImage = ImageCreateFromPNG($watermark);
    $logoW = ImageSX($logoImage);
    $logoH = ImageSY($logoImage);
    ImageCopy($photoImage, $logoImage, $horizmargin, $vertmargin, 0, 0, $logoW, $logoH);
    //ImageJPEG($photoImage); // output to browser
    ImageJPEG($photoImage,$newname, $quality);
    ImageDestroy($photoImage);
    ImageDestroy($logoImage);
    }

    function createthumb($name,$filename,$new_w,$new_h)
    {
    $system=explode(".",$name);
    if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
    if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
    if (preg_match("/gif/",$system[1])){$src_img=imagecreatefromgif($name);}
    if (preg_match("/bmp/",$system[1])){$src_img=imagecreatefromwbmp($name);}
    $old_x=imageSX($src_img);
    $old_y=imageSY($src_img);
    if ($old_x > $old_y)
    {
    $thumb_w=$new_w;
    //$thumb_h=$old_y*($new_h/$old_x);
    $thumb_h=$new_h;
    }
    if ($old_x < $old_y)
    {
    //$thumb_w=$old_x*($new_w/$old_y);
    $thumb_w=$new_w;
    $thumb_h=$new_h;
    }
    if ($old_x == $old_y)
    {
    $thumb_w=$new_w;
    $thumb_h=$new_h;
    }
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
    if (preg_match("/png/",$system[1]))
    {
    imagepng($dst_img,$filename);
    } else if (preg_match("/jpg|jpeg/",$system[1])) {
    imagejpeg($dst_img,$filename);
    }
    else if (preg_match("/bmp/",$system[1]))
    {
    imagewbmp($dst_img,$filename);
    }
    else
    {
    imagegif($dst_img,$filename);
    }
    imagedestroy($dst_img);
    imagedestroy($src_img);
    }
    ?>
     
    bevjones, Mar 15, 2009 IP
  2. MaxZ

    MaxZ Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just increase the PHP memory_limit.

    33554432 bytes = 256 M, obvious add an bigger number in your configuration ;)

    Cheers
     
    MaxZ, Mar 15, 2009 IP
  3. Wojtek

    Wojtek Active Member

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    I had a similar problem at work.
    We installed Eventum to keep track of our support department emails but the email grabber would crash with a similar memory exhausted error.
    I increased the php memory limit to 512M but it still crashed on 40M emails for some mysterious reason.

    I had to trim the full email headers to 5million characters ~5mb and all was fine.


    I suspect in your case you're trying to process a big image and it's crashing.
    On a shared hosting account you're out of luck, unless your provider agrees to play with the php configurations.
     
    Wojtek, Mar 15, 2009 IP
  4. MaxZ

    MaxZ Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Theoretically he can add "php_value memory_limit 512M" in an .httacess file in the same folder as the .php
     
    MaxZ, Mar 15, 2009 IP
  5. kuzmanin

    kuzmanin Peon

    Messages:
    242
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Increasing memory limit can crash server.
    If one of the php running applications uses 512 MB rab suppose how many scripts you can run on normal dedicated server ~ 5-6

    The right way is to check your code for memory leak. Ensure that any variables that are used and no more necessary are removed from memory with "unset"
     
    kuzmanin, Mar 15, 2009 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Listen to kuzmanin. Increasing the memory limit is only sensible if you know there is a good reason it should be allocating more memory.

    In this case it is probably a loop gone out of control somewhere.

    Bevjones - how large are the images you're working with?
     
    SmallPotatoes, Mar 15, 2009 IP