<?php
Verification Code Class
Class Image {
protected $im;
protected $img _width;
protected $img _height;
protected $img _type;
Generate random numbers
static public Function randstr ($n = 4) {
if ($n <= 0) {
Return ';
}
$str = ' abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789 ';
$str = substr (Str_shuffle ($STR), 0, $n);
return $str;
}
Generate Verification Code
static public Function Chkcode ($w =60, $h =25) {
$w wide, $h high
$code = Self::randstr (4);
Create a canvas
$im = Imagecreatetruecolor ($w, $h);
$bak = Imagecreatetruecolor ($w, $h);
Color, Gray + blue Word
$gray = Imagecolorallocate ($im, 100,100,100);
$blue = Imagecolorallocate ($im, 0,0,255);
$bak _gray = imagecolorallocate ($bak, 100,100,100);
Fill
Imagefill ($im, 0,0, $gray);
Imagefill ($bak, 0,0, $bak _gray);
Write
Imagestring ($im, 5,10,5, $code, $blue);
/*****
Store the verification code in the session
*****/
/*****
The character of the verification code is distorted, for reference, not required.
$niu = 3;
for ($i =0; $i <60; $i + +) {
Calculate y-axis fluctuation by sine function
$y = sin (Deg2rad (720/$w) * $i) * $NIU;
Imagecopy ($bak, $im, $i, $y, $i, 0,1, $h);
}
*****/
Header (' Content-type:image/jpeg ');
Imagejpeg ($bak);
}
static public Function Make_thumb ($ori, $w =200, $h =200) {
Judging the original size, if the original image is smaller than the thumbnail, do not have to deal with.
Read a big picture as a canvas
$info = Self::getinfo ($ori);
if ($info [' func '] = = = = False) {
return false;
}
$createfunc = ' Imagecreatefrom '. $info [' func ']; Analyze the name of the function used to read the large image.
$SRC = $createfunc ($ori);
Create a small canvas and make the background gray
$small = Imagecreatetruecolor ($w, $h);
$gray = Imagecolorallocate ($small, 100,100,100);
Imagefill ($small, 0,0, $gray);
Copy large image to small image
$scale = min ($w/$info [' width '], $h/$info [' height ']); In order to fit the smaller reduction ratio
Calculates the size of the last block, depending on the proportions.
$REALW = $info [' width '] * $scale;
$realh = $info [' height '] * $scale;
Generate a small image
/*
BOOL Imagecopyresampled (Resource $DST _image, resource $src _image, int $dst _x, int $dst _y, int $src _x, int $src _y, int $DST _w, int $dst _h, int $src _w, int $src _h)
*/
Calculate left White
$LW = Round (($w-$REALW)/2); To calculate the left side width
$LH = Round (($h-$REALH)/2); Calculate the height left above
Imagecopyresampled ($small, $SRC, $LW, $LH, 0,0, $REALW, $realh, $info [' width '], $info [' height '];
/*
Header (' Content-type:image/jpeg ');
Imagejpeg ($small);
*/
Calculate the storage path for small images
$thumburl = Str_replace ('. ', ' _thumb ', $ori);
$imagefunc = ' image '. $info [' func '];
if ($imagefunc ($small, $thumburl)) {
Return Str_replace (ROOT, ", $thumburl);
} else {
return false;
}
}
static public Function GetInfo ($ori) {
$arr = getimagesize ($ori);
If the original image is not parsed, direct false
if ($arr = = = False) {
return false;
}
$info = Array ();
$info [' width '] = $arr [0];
$info [' height '] = $arr [1];
Switch ($arr [2]) {
Case 1:
$info [' func '] = ' gif ';
Break
Case 2:
$info [' func '] = ' jpeg ';
Break
Case 3:
$info [' func '] = ' png ';
Break
Case 6:
$info [' func '] = ' wbmp ';
Break
Default
$info [' func '] = false;
}
return $info;
}
}
Image::chkcode ();
?>