"PHP Programming Fastest understand" seventh: PHP picture verification code and thumbnail

Source: Internet
Author: User
Tags imagejpeg php programming rand
Example 22 core code for image validation
Copy CodeThe code is as follows:
<?php
Header ("Content-type:image/png");
$num = ' 1234 ';
$imagewidth = 60;
$imageheight = 18;

$numimage = Imagecreate ($imagewidth, $imageheight);
Imagecolorallocate ($numimage, 240,240,240);
for ($i =0; $i <strlen ($num); $i + +) {
$x = Mt_rand (1,8) + $imagewidth * $i/4;
$y = Mt_rand (1, $imageheight/4);
$color =imagecolorallocate ($numimage, Mt_rand (0,150), Mt_rand (0,150), Mt_rand (0,150));
Imagestring ($numimage, 5, $x, $y, $num [$i], $color);
}

For ($i =0 $i <200; $i + +) {
$randcolor =imagecolorallocate ($numimage, Rand (200,255), Rand (200,255), Rand (200,255));
Imagesetpixel ($numimage, Rand ()%70,rand ()%20, $randcolor);
}
Imagepng ($numimage);
Imagedestroy ($numimage);
?>

This is the output of 4 authentication code example, for Chinese characters, need font file and Imagettftext function, when you use the Internet search it. You have to generate random numbers, there are mt_rand functions, you have to use the session to save the random number, if you need to turn to utf-8, you need the ICONV function.

instance 23 thumbnail
Copy CodeThe code is as follows:
<?php
Class SimpleImage {
var $image;
var $image _type;
function Load ($filename) {
$image _info = getimagesize ($filename);
$this->image_type = $image _info[2];
if ($this->image_type = = imagetype_jpeg) {
$this->image = Imagecreatefromjpeg ($filename);
} elseif ($this->image_type = = imagetype_gif) {
$this->image = imagecreatefromgif ($filename);
} elseif ($this->image_type = = imagetype_png) {
$this->image = imagecreatefrompng ($filename);
}
}
function Save ($filename, $image _type=imagetype_jpeg, $compression =75, $permissions =null) {
if ($image _type = = imagetype_jpeg) {
Imagejpeg ($this->image, $filename, $compression);
} elseif ($image _type = = imagetype_gif) {
Imagegif ($this->image, $filename);
} elseif ($image _type = = imagetype_png) {
Imagepng ($this->image, $filename);
}
if ($permissions!= null) {
chmod ($filename, $permissions);
}
}
function output ($image _type=imagetype_jpeg) {
if ($image _type = = imagetype_jpeg) {
Imagejpeg ($this->image);
} elseif ($image _type = = imagetype_gif) {
Imagegif ($this->image);
} elseif ($image _type = = imagetype_png) {
Imagepng ($this->image);
}
}
function GetWidth () {
Return Imagesx ($this->image);
}
function GetHeight () {
Return Imagesy ($this->image);
}
function Resizetoheight ($height) {
$ratio = $height/$this->getheight ();
$width = $this->getwidth () * $ratio;
$this->resize ($width, $height);
}
function Resizetowidth ($width) {
$ratio = $width/$this->getwidth ();
$height = $this->getheight () * $ratio;
$this->resize ($width, $height);
}
function scale ($scale) {
$width = $this->getwidth () * $scale/100;
$height = $this->getheight () * $scale/100;
$this->resize ($width, $height);
}
function Resize ($width, $height) {
$new _image = Imagecreatetruecolor ($width, $height);
Imagecopyresampled ($new _image, $this->image, 0, 0, 0, 0, $width, $height, $this->getwidth (), $this->getheight ( ));
$this->image = $new _image;
}
}

$newfile = Upload_dir. " /icons/". MD5 ($_session[' USER ']->email)." JPG "//Upload file saved directory
$image = new SimpleImage ();
$image->load ($_files[' icons '] [' tmp_name '])/temporary file name uploaded
$image->resizetowidth (80); Set width
$image->save ($newfile);
?>


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.