php - 在非常高的图像上用 PHP 绘制线条,然后脚本停止绘制。出了什么问题,我该如何解决?

标签 php image memory

我有一个 PHP 脚本,可以创建一个非常高的图像并在其上绘制很多线条(一种有组织的 Web 外观)。对于我尝试创建的最高图像,线条绘制突然停止到图像的中间到底部:http://i.imgur.com/4Plgr.png

我在使用 imagecreate() 时遇到了这个问题,然后我发现 imagecreatetruecolor() 可以处理更大的图像,所以我改用它。我仍然遇到同样的问题,但脚本现在可以处理更大的图像。我认为它应该绘制大约 1200 行。该脚本的执行时间不会超过 3 秒。这是一个完全执行的图像:http://i.imgur.com/PaXrs.png

我使用 ini_set('memory_limit', '1000M') 调整了内存限制,但我的脚本从未接近限制。

如何强制脚本继续绘制直到完成?或者我如何使用 PHP 使用更少的内存创建图像(我认为这是问题所在)?

if(sizeof($array[0])<300)
$image=imagecreate($width,$height);
else
$image=imagecreatetruecolor($width,$height);
imagefill($image,0,0,imagecolorallocate($image,255,255,255));
for($p=0; $p<sizeof($linepoints); $p++){
$posx1=77+177*$linepoints[$p][0];
$posy1=-4+46*$linepoints[$p][1];
$posx2=77+177*$linepoints[$p][2];
$posy2=-4+46*$linepoints[$p][3];
$image=draw_trail($image,$posx1,$posy1,$posx2,$posy2);
}
imagepng($image,"images/table_backgrounds/table_background".$tsn.".png",9);
imagedestroy($image);

function draw_trail($image,$posx1,$posy1,$posx2,$posy2){
$black=imagecolorallocate($image,0,0,0);
if($posy1==$posy2)
imageline($image,$posx1,$posy1,$posx2,$posy2,$black);
else{
imageline($image,$posx1,$posy1,$posx1+89,$posy1,$black);
imageline($image,$posx1+89,$posy1,$posx1+89,$posy2,$black);
imageline($image,$posx1+89,$posy2,$posx2,$posy2,$black);
}
return $image;
}

最佳答案

我猜测您造成了内存泄漏,并且当您对更大的图像执行更多操作时,您最终会达到 PHP 的内存限制。与其提高上限,不如找到漏洞。

尝试更改您的代码,使其显式取消分配您在 draw_trail 中创建的颜色。此外,没有理由返回 $image,因为您正在传递资源。

if(sizeof($array[0])&lt;300)
$image=imagecreate($width,$height);
else
$image=imagecreatetruecolor($width,$height);
imagefill($image,0,0,imagecolorallocate($image,255,255,255));
for($p=0; $p&lt;sizeof($linepoints); $p++)
{
    $posx1=77+177*$linepoints[$p][0];
    $posy1=-4+46*$linepoints[$p][1];
    $posx2=77+177*$linepoints[$p][2];
    $posy2=-4+46*$linepoints[$p][3];
    draw_trail($image,$posx1,$posy1,$posx2,$posy2);
}
imagepng($image,"images/table_backgrounds/table_background".$tsn.".png",9);
imagedestroy($image);

function draw_trail($image,$posx1,$posy1,$posx2,$posy2)
{
    $black=imagecolorallocate($image,0,0,0);
    if($posy1==$posy2)
    imageline($image,$posx1,$posy1,$posx2,$posy2,$black);
    else
    {
        imageline($image,$posx1,$posy1,$posx1+89,$posy1,$black);
        imageline($image,$posx1+89,$posy1,$posx1+89,$posy2,$black);
        imageline($image,$posx1+89,$posy2,$posx2,$posy2,$black);
    }
    imagecolordeallocate($black);
}

关于php - 在非常高的图像上用 PHP 绘制线条,然后脚本停止绘制。出了什么问题,我该如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12946560/

相关文章:

php - Symfony 2.8-SQL : No such file or directory in "test" environment only

javascript - 如何根据一些数据动态添加 Font Awesome 图标

xcode - 使用按钮时图像不显示

c++ - 内存泄漏 C++

php - 在 Codeigniter/PHP 中提取两个日期之间的事件时遇到问题

php - 无法从 mysql 数据库中的 Froala 所见即所得编辑器发送值

android - CSS设置固定在android手机上的背景图片?

c# - 从 WPF 中的 Image 标记修改图像

c++ - wxWidgets 中的自定义内存分配

WCF 内存使用