/ePHP/libraries/Image.class.php
PHP | 249 lines | 150 code | 23 blank | 76 comment | 42 complexity | 5c691165ee18456ec527c6f35d067077 MD5 | raw file
Possible License(s): Apache-2.0
- <?php
- /**
- +------------------------------------------------------------------------------
- * ?????????
- * <code>
- * ???????
- * ?view??????<img src="/?c=reg&a=code" onclick="this.src='/?c=reg&a=code'" />
- *
- * //?????
- * public function registerAction()
- * {
- * echo Image::chkVerify() ? '?????' : '?????';
- * }
- * public funciton codeAction()
- * {
- * Image::imgVerify();
- * }
- *
- * //???????
- * print_r(Image::getInfo('/home/a.jpg'));
- *
- * //??gd?????
- * Image::thumbImg('a.jpg','a-thumb.jpg',100,120);//?? a.jpg ????,??:100 ??:120
- *
- * </code>
- +------------------------------------------------------------------------------
- * @version 3.0
- * @author WangXian
- * @email admin@loopx.cn
- * @package libraries
- * @creation_date 2010-10-17
- * @last_modified 2010-12-25 19:14:58
- +------------------------------------------------------------------------------
- */
- class Image
- {
- /**
- * ???????
- * @param $imgFile ?????
- * @return array
- */
- static public function getInfo($imgFile)
- {
- $imageInfo = getimagesize($imgFile);
- if( $imageInfo!== false)
- {
- $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));
- $imageSize = filesize($imgFile);
- $info = array(
- "width"=>$imageInfo[0],
- "height"=>$imageInfo[1],
- "type"=>$imageType,
- "size"=>$imageSize,
- "mime"=>$imageInfo['mime']
- );
- return $info;
- }
- else return false;
- }
- /**
- * ?????
- * ??????????????????????????JPG
- * @param $srcfile:????
- * @param $dstfile:?????????
- * @param $thumbWidth:???????
- * @param $thumbHeight:???????
- */
- static public function thumbImg($srcfile,$dstfile,$thumbWidth,$thumbHeight)
- {
- //?????
- $tow = $thumbWidth;
- $toh = $thumbHeight;
- $make_max = 0;
- $maxtow = $thumbWidth;
- $maxtoh = $thumbHeight;
- if($maxtow >= 300 && $maxtoh >= 300)
- {
- $make_max = 1;
- }
- //??????
- $im = '';
- if(true == ($data = getimagesize($srcfile)) )
- {
- if($data[2] == 1)
- {
- $make_max = 0;//gif???
- if(function_exists("imagecreatefromgif"))
- $im = imagecreatefromgif($srcfile);
- }
- elseif($data[2] == 2)
- {
- if(function_exists("imagecreatefromjpeg"))
- $im = imagecreatefromjpeg($srcfile);
- }
- elseif($data[2] == 3)
- {
- if(function_exists("imagecreatefrompng"))
- $im = imagecreatefrompng($srcfile);
- }
- }
- if(!$im) return '';
- $srcw = imagesx($im);
- $srch = imagesy($im);
- $towh = $tow/$toh;
- $srcwh = $srcw/$srch;
- if($towh <= $srcwh)
- {
- $ftow = $tow;
- $ftoh = $ftow*($srch/$srcw);
- $fmaxtow = $maxtow;
- $fmaxtoh = $fmaxtow*($srch/$srcw);
- }
- else
- {
- $ftoh = $toh;
- $ftow = $ftoh*($srcw/$srch);
- $fmaxtoh = $maxtoh;
- $fmaxtow = $fmaxtoh*($srcw/$srch);
- }
- if($srcw <= $maxtow && $srch <= $maxtoh)
- {
- $make_max = 0;//???
- }
- if($srcw > $tow || $srch > $toh)
- {
- if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh))
- {
- imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
- //???
- if($make_max && @$maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh))
- {
- imagecopyresampled($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
- }
- }
- elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh))
- {
- imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
- //???
- if($make_max && @$maxni = imagecreate($fmaxtow, $fmaxtoh))
- {
- imagecopyresized($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
- }
- }
- else return '';
- if(function_exists('imagejpeg'))
- {
- imagejpeg($ni, $dstfile);
- //???
- if($make_max) imagejpeg($maxni, $srcfile);
- }
- elseif(function_exists('imagepng'))
- {
- imagepng($ni, $dstfile);
- //???
- if($make_max) imagepng($maxni, $srcfile);
- }
- imagedestroy($ni);
- if($make_max) imagedestroy($maxni);
- }
- imagedestroy($im);
- return file_exists($dstfile);
- }
- /**
- * ???
- * ????,50x24
- *
- * @param integer $length :?????
- * @param integer $mode : 0??????1???2?????3????,5???+??
- * @param string $type :????
- * @param boolean $hasborder :??????
- * @return binary
- */
- static public function imgVerify($length=4,$mode=3,$type='png',$hasborder=true)
- {
- $randval = Func::randString($length,$mode);
- $_SESSION['imgVerifyCode']=md5(strtolower($randval));
- //dump($_SESSION);exit;
- $width=50;
- $height=24;
- $width = ($length*9+10)>$width ? $length*9+10 : $width;
- if ( $type!='gif' && function_exists('imagecreatetruecolor'))
- $im = @imagecreatetruecolor($width,$height);
- else $im = @imagecreate($width,$height);
- //$r = array(225,255,255,223);
- //$g = array(225,236,237,255);
- //$b = array(225,236,166,125);
- //$key = mt_rand(0,3);
- //$backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);
- $backColor = imagecolorallocate($im, 252,252,252); //???
- if($hasborder) $border_color = 238;
- else $border_color = 255;
- $borderColor = imagecolorallocate($im, $border_color,$border_color,$border_color); //???
- $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //???
- @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
- @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
- $stringColor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,120),mt_rand(0,120));
- // ??
- for($i=0;$i<10;$i++)
- {
- $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
- imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
- }
- for($i=0;$i<25;$i++)
- {
- $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
- imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
- }
- @imagestring($im, 5, 5, 3, $randval, $stringColor);
- header("Content-type: image/".$type);
- $ImageFun='Image'.$type;
- $ImageFun($im);
- imagedestroy($im);
- }
- /**
- * ????????????
- * @param $verifyCode ????????
- * @return boolean
- */
- static public function chkVerify($verifyCode)
- {
- if(empty($_SESSION['imgVerifyCode'])) return false;
- return $_SESSION['imgVerifyCode'] == md5(strtolower($verifyCode));
- }
- }
- /* End of file Image.class.php */
- /* Location: ./_framework/libraries/Image.class.php */