Modify the PHP program that obtains the verification code image to the local_PHP tutorial

WBOY
Release: 2016-07-21 14:53:48
Original
908 people have browsed it

The recent project is not very big, so there is room for time comparison. Yesterday I was thinking about writing something. I remembered the telecom company’s voting for Smiling Angels a few days ago. Voting requires filling in a verification code. I thought about it. I wanted to write a voting cheating program, but when I came back from vacation, the event was over. I suddenly remembered yesterday and wrote a PHP program to get the verification code image to the local area, so that I can use it directly in case of similar voting activities in the future. use.

The program uses PHP's GD library. The principle is very simple, that is, first create a blank picture, and then use the imagecreatefromjpeg function in the PHP GD library to create an image object with the verification code picture. Finally, calculate the length and width of the picture, and again Use PHP's built-in imagecopy to copy to the blank image created at the beginning.
All codes are as follows:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] header("Content-type:image/png");
set_time_limit(0);//Set PHP timeout
$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 verification code format as jpg. If it is in png or gif format, please refer to the second page.

Through the previous pagea PHP program that obtains verification code images locally, images with verification codes in jpg format can be output normally, but verification codes with png and gif cannot be used normally. Today I will slightly modify the PHP code so that it can support verification codes in three formats: png, gif, and jpg.

PHP can use PHP’s built-in exif_imagetype function to determine the format of the image, which is very convenient,

For detailed usage of exif_imagetype, please visit: http://php.net/manual/en/function.exif-imagetype.php

Code:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] header("Content-type:image/png");
set_time_limit(0);//Set PHP timeout
$url = $_GET['url'];
$url = "http: //vcer.baidu.com/verify";
if(empty($url)){
echo "No image";
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);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364713.htmlTechArticleThe recent project is not very big, so there is no room for time comparison. Yesterday I was thinking about writing something, and I remembered a few days ago Telecommunications companies vote for Smiling Angels. To vote, you need to fill in a verification code...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!