Line drawing with PHP in a very high image and the script just stops drawing. What is wrong and how can I fix it?

I have a PHP script that creates a very tall image and draws a lot of lines on it (organizational view on the Internet). For the tallest images I tried to create, the line drawing just stops abruptly in the middle of the image: http://i.imgur.com/4Plgr.png

I ran into this problem using imagecreate (), then found that imagecreatetruecolor () can handle large images, so I switched to this. I still have the same problem, but the script can now handle slightly larger images. I think it should be about 1200 lines. A script requires no more than 3 seconds. Here is the complete image: http://i.imgur.com/PaXrs.png

I adjusted the memory limits with ini_set ('memory_limit', '1000M'), but my scripts never get close to the limit.

How to get a script to continue drawing until it ends? Or how can I use PHP to create an image using less memory (which I think is the problem)?

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; } 
+4
source share
2 answers

OP is here. I understood, or at least circumvented my problem. I drew a lot of lines in the image, and the way my code got these points created many repetitions, so I still suspect that this is a memory problem. I strengthened all the points by taking out the repetitions, and now everything works fine. Thanks to everyone who tried to help.

0
source

I'm going to assume that you created a memory leak, and as you do more operations with a larger image, you will end up with a PHP memory limit. Instead of raising the limit, it would be better to find a leak.

Try changing the code so that it explicitly frees the color you create in draw_trail . Also, there is no reason to return $image , as you are passing a resource.

 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); } 
+1
source

Source: https://habr.com/ru/post/1440440/


All Articles