How to save the verification code Image

Source: Internet
Author: User
If code GD generates an image $ im: {code...}, then the question is, how can I store this image on my server? For example, code GD generates an image $ im:

Public function generateCode () {$ w = 50; $ h = 20; $ this-> load-> config ('common '); $ common_config = $ this-> config-> item ('common'); $ pic_verify_code = $ common_config ['pic _ verify_code ']; $ code = func :: getRandCode ($ pic_verify_code); // create an image and define the color value $ im = imagecreate ($ w, $ h); $ black = imagecolorallocate ($ im, 90, 90 ); $ gray = imagecolorallocate ($ im, 189,252,201); $ bgcolor = imagecolorallocate ($ im, 255,255,255); // fill the background imagefill ($ im, 0, 0, $ gray ); // draw the border imagerectangle ($ im, 0, 0, $ W-1, $ h-1, $ black); // draw two dotted lines randomly, interference function $ style = array ($ black, $ black, $ gray ); imagesetstyle ($ im, $ style); $ y1 = rand (0, $ h); $ y2 = rand (0, $ h); $ y3 = rand (0, $ h); $ y4 = rand (0, $ h); imageline ($ im, 0, $ y1, $ w, $ y3, IMG_COLOR_STYLED); imageline ($ im, 0, $ y2, $ w, $ y4, IMG_COLOR_STYLED); // randomly generate a large number of black spots on the canvas, which can interfere with ($ I = 0; $ I <80; $ I ++) {imagesetpixel ($ im, rand (0, $ w), rand (0, $ h), $ black );} // randomly display numbers on the canvas. The horizontal spacing and position of characters are randomly generated according to a certain fluctuation range $ strx = rand (3, 8); for ($ I = 0; $ I <$ pic_verify_code; $ I ++) {$ strpos = rand (1, 6); imagestring ($ im, 5, $ strx, $ strpos, substr ($ code, $ I, 1), $ black); $ strx + = rand (8, 12 );}

So the question is, how can I store this image on my server?

Reply content:

For example, code GD generates an image $ im:

Public function generateCode () {$ w = 50; $ h = 20; $ this-> load-> config ('common '); $ common_config = $ this-> config-> item ('common'); $ pic_verify_code = $ common_config ['pic _ verify_code ']; $ code = func :: getRandCode ($ pic_verify_code); // create an image and define the color value $ im = imagecreate ($ w, $ h); $ black = imagecolorallocate ($ im, 90, 90 ); $ gray = imagecolorallocate ($ im, 189,252,201); $ bgcolor = imagecolorallocate ($ im, 255,255,255); // fill the background imagefill ($ im, 0, 0, $ gray ); // draw the border imagerectangle ($ im, 0, 0, $ W-1, $ h-1, $ black); // draw two dotted lines randomly, interference function $ style = array ($ black, $ black, $ gray ); imagesetstyle ($ im, $ style); $ y1 = rand (0, $ h); $ y2 = rand (0, $ h); $ y3 = rand (0, $ h); $ y4 = rand (0, $ h); imageline ($ im, 0, $ y1, $ w, $ y3, IMG_COLOR_STYLED); imageline ($ im, 0, $ y2, $ w, $ y4, IMG_COLOR_STYLED); // randomly generate a large number of black spots on the canvas, which can interfere with ($ I = 0; $ I <80; $ I ++) {imagesetpixel ($ im, rand (0, $ w), rand (0, $ h), $ black );} // randomly display numbers on the canvas. The horizontal spacing and position of characters are randomly generated according to a certain fluctuation range $ strx = rand (3, 8); for ($ I = 0; $ I <$ pic_verify_code; $ I ++) {$ strpos = rand (1, 6); imagestring ($ im, 5, $ strx, $ strpos, substr ($ code, $ I, 1), $ black); $ strx + = rand (8, 12 );}

So the question is, how can I store this image on my server?

Write to file, e. fwite ()

imagepng($im,'pic.png');

Simply send the verification code to the client. What should I do to save it;
Xxx. php:
Imagepng ($ im );
Log in there

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.