Home >Backend Development >PHP Tutorial >'The Quickest to Understand PHP Programming' Lecture 7: PHP Image Verification Code and Thumbnail_PHP Tutorial

'The Quickest to Understand PHP Programming' Lecture 7: PHP Image Verification Code and Thumbnail_PHP Tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-21 15:33:04842browse

Example 22 Core code of image verification

Copy code The code is as follows:

//header( "content-type:image/png");
$num ='1234';
$imagewidth=60;
$imageheight=18;

$numimage = imagecreate($imagewidth ,$imageheight);
imagecolorallocate($numimage,240,240,240);
for($i=0;$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 to output 4 verifications As an example of coding, for Chinese characters, a font file and imagettftext function are required. When using them, please search online. If you want to generate random numbers, there is the mt_rand function; you also need to use session to save the random numbers; if you need to convert them to utf-8, you need the iconv function.

Example 23 Thumbnail
Copy code The code is as follows:

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";// Directory where uploaded files are saved
$image = new SimpleImage();
$image->load($_FILES['icons']['tmp_name']);//Uploaded temporary file name
$image->resizeToWidth(80);Set width
$image->save($newfile);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322671.htmlTechArticleExample 22 The core code of image verification is copied as follows: ?php //header("content-type:image /png"); $num ='1234'; $imagewidth=60; $imageheight=18; $numimage = imagecreate($imag...
Statement:
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