Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem that php can't generate pictures

Shulou Source: shulou.com Published: 2022-05-31 10:58:38 09月20日 Update

This article mainly introduces "php can not generate pictures how to solve", in the daily operation, I believe that many people in the php can not generate pictures on how to solve the problem, the editor consulted all kinds of data, sorted out a simple and easy-to-use method of operation, hope to answer the "php can not generate pictures how to solve" doubts! Next, please follow the editor to study!

Solution.

1. Open the gd2 library and view it through phpinfo. Clear bom, and the code starts at the top line, so the problem may be on the code.

2. Add the ob_clean () statement before header, and then you can run it.

Pay attention

When generating a picture, header ('Content-type: image/png'); cannot be preceded by output. Or, precede it with: ob_clean (); you can clear the output cache even with output.

Solve an example

/ / set the height width of CAPTCHA\ the number of characters above $img_w = 70; $img_h = 22; $font = 5; $char_len = 5; / / array merge, range () function returns a range array of $char = array_merge (range ('ajar,' z'), range ('cause,' Z'), range ('1th,' 9'); $rand_keys = array_rand ($char, $char_len) / / randomly fetch a specified number of elements from the array to generate the key value if ($char_len = = 1) {/ / if there is only one number, array_rand () returns the non-array type $rand_keys = array ($rand_keys);} shuffle ($rand_keys); / / can not use $code ='; foreach ($rand_keys as $k) {$code. = $char [$k];} session_start () $_ SESSION ['captcha'] = $code; / / add lines, colors / / create a new image $img = imagecreatetruecolor ($img_w, $img_h); / / assign colors $bg_color = imagecolorallocate ($img, 0xcc, 0xcc, 0xcc); / / canvas background color imagefill ($img, 0,0, $bg_color); / / interference lines for ($I = 0; $I < 300) + $I) {$color = imagecolorallocate ($img, mt_rand (0255), mt_rand (0255), mt_rand (0255)); imagesetpixel ($img, mt_rand (0, $img_w), mt_rand (0, $img_h), $color);} for ($I = 0; $I

Tags: Generation pictures arrays learning number output code more lines questions colors help practical next no elements functions methods only can be passed Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux MariaDB Shulou Tech Info Docker