dongxingguo1978 2009-05-25 09:30
浏览 79
已采纳

imagecreatefromjpeg内存问题

Hello everyone I am trying to resize an image of 700kb with imagecreatefromjpeg.

This gives the error: Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 9356 bytes)

First the memory_limit was 8M then we set this to 24 M but then it still gives the error. Is this a memory leak? What could have happened? We restarted apache already.

Here is some code for those who are interested:

function getResizedBackgroundImageURL($afb="",$params="") {
#1. Verwijder webroot
$filename=C_Settings::getFileRoot().str_replace(C_Settings::getWebRoot(),"",$afb);
$i=strrpos($filename,".");
if ($i!==FALSE) {
    $ext=substr($filename,$i);
    $basefilename=substr($filename,0,$i);

    #Parse width/height params
    $a=explode("&",$params);
    foreach($a as $attr) {
        $b=explode("=",$attr);
        if ($b[0]=="w") {
            $w=$b[1];
        }   elseif ($b[0]=="h") {
            $h=$b[1];
        }
    }

    if (!is_numeric($w)) {
      die("Missing param w for getResizedBackgroundImageURL");
    }

    if (!is_numeric($h)) {
      die("Missing param h for getResizedBackgroundImageURL");
    }

    #Compose new filename
    $newFilename=$basefilename."_w".$w."_h".$h.$ext;
    #See if the resized image exists
    if (!file_exists($newFilename)) {
    if (is_file($filename)) {
        if($ext==".jpg" || $ext==".jpeg"){

            $image_org=imagecreatefromjpeg($filename);


        }
        if($ext==".gif") {
            $image_org=@imagecreatefromgif($filename);
        }

        if ($image_org) {
            list($wp,$hp,$type,$attr)=getimagesize($filename);
            $hfactor=$hp/$h;
            $wfactor=$wp/$w;
            if ($hfactor > $wfactor) {
            $factor=$hfactor;
        } else {
            $factor=$wfactor;
        }   
                if($wp > $w || $hp > $h) {
        $image_w=$wp/$factor;
        $image_h=$hp/$factor;
        }else{
            #if image fits the given boundaries, do not resize
            $image_w=$wp;
        $image_h=$hp;               
        }
        //Note: PHP with GD2.0 required for imagecreatetruecolor
        $img_copy=imagecreatetruecolor($image_w, $image_h);
        imagecopyresampled($img_copy, $image_org, 0, 0, 0, 0, $image_w, $image_h, $wp, $hp);

            if (@imagejpeg($img_copy, $newFilename, 80)) {
                chmod($newFilename,0777);
            }   else {
                echo("<b>Error: </b>Unable to create image $newFilename. Check directory permissions.");
            }   

          imagedestroy($image_org);
            imagedestroy($img_copy);
    }
  }      
    }
    $newURL=C_Settings::getWebRoot().str_replace(C_Settings::getFileRoot(),"",$newFilename);
    return $newURL;     
}   

}

  • 写回答

1条回答 默认 最新

  • dpcwz210393 2009-05-25 09:54
    关注

    Increase your memory until you have enough and make sure you use imagedestroy() when your done with your processing.

    ini_set("memory_limit","10000M");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan