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);