Modify get the CAPTCHA code picture to the local PHP program

Source: Internet
Author: User
Tags imagecopy php code

The recent project is not very big, so time to compare the space, yesterday, thinking about writing something, think of a few days ago, the telecom company voted to choose the activity of smiling angels, voting is to fill in the verification code, think of the next to write a vote cheat program, but when I come back from vacation, people's activities have ended, yesterday suddenly remembered, Wrote a code to obtain the image of the local PHP program, in order to have similar voting activities in the future can be used directly.

The program uses the GD Library of PHP, the principle is very simple, is to create a blank picture, and then the verification code of the picture using the PHP library imagecreatefromjpeg function to create an image object, and finally calculate the long width of the picture, Once again, use PHP's built-in imagecopy to copy to a blank picture created at the beginning.

All the code is as follows:

Header ("Content-type:image/png");

Set_time_limit (0);/Set the PHP timeout time

$url = $_get[' url '];

$url = "Http://vcer.baidu.com/verify";

$imginfo = getimagesize ($url);

$IMGW = $imginfo [0];

$IMGH = $imginfo [1];

$BG = Imagecreatetruecolor ($IMGW, $IMGH);

$image = Imagecreatefromjpeg ($url);

Imagecolorallocate ($image, 255,255,255);

Imagecopy ($BG, $image, 0,0, 0,0, $IMGW, $IMGH);

Imagedestroy ($image);

Imagepng ($BG);

The code here supports the format of the Authenticode format as JPG, and a PNG or GIF format can refer to the second page.

Through a previous page to get the CAPTCHA code picture to the local PHP program, for the validation code jpg picture is normal output, for PNG, GIF code is not normal use, today slightly modify the PHP code, so that it can support PNG, GIF, JPG Three format of the verification code.

PHP to determine the format of the picture can use PHP built-in Exif_imagetype function, very convenient,

Detailed usage of exif_imagetype can be accessed by: http://php.net/manual/en/function.exif-imagetype.php

Header ("Content-type:image/png");

Set_time_limit (0);/Set the PHP timeout time

$url = $_get[' url '];

$url = "Http://vcer.baidu.com/verify";

if (empty ($url)) {

echo "No picture";

Die

}

$imginfo = getimagesize ($url);

$type = Exif_imagetype ($url);

$IMGW = $imginfo [0];

$IMGH = $imginfo [1];

$BG = Imagecreatetruecolor ($IMGW, $IMGH);

if ($type ==imagetype_gif) {

$image = Imagecreatefromgif ($url);

}elseif ($type ==imagetype_jpeg) {

$image = Imagecreatefromjpeg ($url);

}elseif ($type ==imagetype_png) {

$image = Imagecreatefrompng ($url);

}

Imagecolorallocate ($image, 255,255,255);

Imagecopy ($BG, $image, 0,0, 0,0, $IMGW, $IMGH);

Imagedestroy ($image);

Imagepng ($BG);

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.