/** * 生成验证码图片 * * @param String $word 验证码在session中的变量名称 */ function valiCode($word = 'randcode') { Header("Content-type: image/png"); $border = 1; //是否要边框 1要:0不要 $how = 4; //验证码位数 $w = $how * 15; //图片宽度 $h = 25; //图片高度 $fontsize = 32; //字体大小 $alpha = "abcdefghijkmnpqrstuvwxyz"; //验证码内容1:字母 $number = "23456789"; //验证码内容2:数字 $randcode = ""; //验证码字符串初始化 srand((double) microtime() * 1000000); //初始化随机数种子 $im = imagecreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = imagecolorallocate($im, 255, 255, 255); //设置背景颜色 imageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = imagecolorallocate($im, 9, 9, 9); //设置边框颜色 imagerectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = imagecolorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 imagechar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } /* * 如果需要添加干扰就将注释去掉 * * 以下for()循环为绘背景干扰线代码 */ /* + -------------------------------绘背景干扰线 开始-------------------------------------------- + */ for ($i = 0; $i < 5; $i++) { $color1 = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 imagearc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } /* + -------------------------------绘背景干扰线 结束-------------------------------------- + */ /* * 如果需要添加干扰就将注释去掉 * * 以下for()循环为绘背景干扰点代码 */ /* + --------------------------------绘背景干扰点 开始------------------------------------------ + */ for ($i = 0; $i < $how * 40; $i++) { $color2 = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 imageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } /* + --------------------------------绘背景干扰点 结束------------------------------------------ + */ //把验证码字符串写入session 方便提交登录信息时检验验证码是否正确 例如:$_POST['randcode'] = $_SESSION['randcode'] session_start(); $_SESSION[$word] = $randcode; /*绘图结束*/ imagepng($im); imagedestroy($im); /*绘图结束*/ }
<?php echo "Alive: create image\n"; $width = 50; $height = 100; $ImHandle = imagecreate($width, $height); echo "Alive: Define colors\n"; $MyBlue = imagecolorAllocate($ImHandle, 0, 0, 255); $MyRed = imagecolorAllocate($ImHandle, 255, 0, 0); $MyWhite = imagecolorAllocate($ImHandle, 255, 255, 255); $MyBlack = imagecolorAllocate($ImHandle, 0, 0, 0); echo "Alive: Draw\n"; ImageFill($ImHandle, 0, 0, $MyBlack); ImageLine($ImHandle, 20, 20, 180, 20, $MyWhite); ImageLine($ImHandle, 20, 20, 20, 70, $MyBlue); ImageLine($ImHandle, 20, 70, 180, 70, $MyRed); ImageLine($ImHandle, 180, 20, 180, 45, $MyWhite); ImageLine($ImHandle, 180, 70, 180, 45, $MyRed); ImageLine($ImHandle, 20, 20, 100, 45, $MyBlue); ImageLine($ImHandle, 20, 70, 100, 45, $MyBlue); ImageLine($ImHandle, 100, 45, 180, 45, $MyRed); ImageFill($ImHandle, 21, 45, $MyBlue); ImageFill($ImHandle, 100, 69, $MyRed); ImageFill($ImHandle, 100, 21, $MyWhite); echo "Alive: ImageString\n"; ImageString($ImHandle, 4, 40, 75, "Czech Republic", $MyWhite); echo "Alive: Send to browser\n"; //Header("Content-type: image/PNG"); //ImagePNG($ImHandle); echo "Alive: Free resources\n"; imagedestroy($ImHandle);
<?php $length = 5; $alphabet = '1234567890abcdefghijkmnpqrstuvwxyz'; $string = substr(str_shuffle(str_repeat($alphabet, $length)), 0, $length); //���������� ����� $img = imagecreate(140, 50); //������� �������� if ($color == "red") { $color = imagecolorAllocate($img, 249, 183, 169); $border_color = imagecolorAllocate($img, 212, 12, 12); } else { $color = imagecolorAllocate($img, 217, 239, 185); $border_color = imagecolorAllocate($img, 160, 214, 81); } $text_color = imagecolorAllocate($img, 102, 102, 102); $�� = (imageSX($img) - 1 * strlen($string)) / 2; imagettftext($img, 23, -3, 10, 30, $text_color, 'font.TTF', $string); imageGif($img, "img/number.gif"); imageDestroy($img); $_SESSION["capcha"] = $string; ?> <div class="loginform" <?php if ($_SESSION['try'] > $num_for_lock) { echo 'style="background:url(../img/lock.png) no-repeat 60% 30%;"'; } ?> > <form method="post" action="index.php" > <fieldset > <p>
public function actionUploadify() { $organID = $_POST['fileClass']; $add = $_POST['add']; $role = $_POST['role'] ? $_POST['role'] : 0; if (!empty($_FILES)) { $fileNames = $_FILES['Filedata']['name']; //复制文件到目的地址 $ImgName = $fileNames; $file_path = pathinfo($fileNames); // 获取文件信息 $aa = explode('#', $file_path['filename']); // 不含后缀名文件 $file_path ['filename'] $GoodsNO = $aa[0]; $fileName = $this->getRandomName($fileNames); if ($add == '1') { $data = $this->getGoodsName($GoodsNO, $organID, $role); if (!$data) { $rs = array('code' => 150, 'msg' => '上传失败!' . $ImgName . '找不到对应商品'); echo json_encode($rs); exit; } $GoodsName = $data['Name']; $GoodsID = $data['ID']; } if ($role == 'second') { $afileDir = "dealer/" . $organID . '/goods/small/'; $bfileDir = "dealer/" . $organID . '/goods/normal/'; $mfileDir = "dealer/" . $organID . '/goods/thumb/'; } else { if ($role == 'first') { $fileDir = "maker/images/" . $organID . '/'; } else { if ($role == 'inquiryupload') { $fileDir = "servicer/images/inquiry/" . $organID . '/'; } else { if ($role == 1) { $fileDir = "maker/" . $organID . '/'; } else { if ($role == 2) { $fileDir = "dealer/" . $organID . '/'; } else { if ($role == 3) { $fileDir = "servicer/" . $organID . '/'; } } } } } } $afilePath = Yii::app()->params['uploadPath'] . 'tmp/' . $afileDir; $bfilePath = Yii::app()->params['uploadPath'] . 'tmp/' . $bfileDir; $mfilePath = Yii::app()->params['uploadPath'] . 'tmp/' . $mfileDir; if (!file_exists($afilePath)) { mkdir($afilePath, 0777, true); chmod($afilePath, 0777); } if (!file_exists($bfilePath)) { mkdir($bfilePath, 0777, true); chmod($bfilePath, 0777); } if (!file_exists($mfilePath)) { mkdir($mfilePath, 0777, true); chmod($mfilePath, 0777); } $targetFile = $bfilePath . 'A' . $fileName; //var_dump($targetFile); $imgurlname = $bfileDir . 'A' . $fileName; // 新文件名 $newimgname = $afileDir . 'A' . $fileName; //缩放后的文件名 $mimgname = $mfileDir . 'A' . $fileName; //缩略小图的文件名 // $bimgname = $fileDir . 'B' . $fileName; //原图的文件名 $tmpfile = $_FILES['Filedata']['tmp_name']; if ($role == 'second') { $sql = "select * from {{dealer_goods_image_relation}} where OrganID=" . $organID . " and ImageName='" . $ImgName . "'"; } else { if ($role == 'first') { $sql = "select * from {{make_goods_image_relation}} where OrganID=" . $organID . " and ImageName='" . $ImgName . "'"; } } $datas = DBUtil::query($sql); if ($add == '0') { $datas = 0; } if (!$datas) { @move_uploaded_file($tmpfile, $targetFile); //定义缩略图名称 $newfile = $afilePath . 'A' . $fileName; $mnewfile = $mfilePath . 'A' . $fileName; $bnewfile = $bfilePath . 'A' . $fileName; //定义缩略图尺寸 $width = 350; $height = 350; $mwidth = 80; $mheight = 80; //获得原图相关信息 $arr = getimagesize($targetFile); $ratio_orig = $arr[0] / $arr[1]; //生成一个缩略图资源 $thumb = ImageCreateTrueColor($width, $height); $mthumb = ImageCreateTrueColor($mwidth, $mheight); //图片尺寸的缩放 if ($width / $height > $ratio_orig) { $width = $height * $ratio_orig; $x = (350 - $width) / 2; $y = 0; } else { $height = $width / $ratio_orig; $x = 0; $y = (350 - $height) / 2; } if ($mwidth / $mheight > $ratio_orig) { $mwidth = $mheight * $ratio_orig; $mx = (80 - $mwidth) / 2; $my = 0; } else { $mheight = $mwidth / $ratio_orig; $mx = 0; $my = (80 - $mheight) / 2; } //分配一个白色 $color = imagecolorAllocate($thumb, 255, 255, 255); $mcolor = imagecolorAllocate($mthumb, 255, 255, 255); //将缩略图资源填充颜色 imagefill($thumb, 0, 0, $color); imagefill($mthumb, 0, 0, $mcolor); // 1 为 GIF 格式、 2 为 JPEG/JPG 格式、3 为 PNG 格式 //获得原图的资源 if ($arr[2] == 1) { $image = imagecreatefromgif($targetFile); } else { if ($arr[2] == 3) { $image = imagecreatefrompng($targetFile); } else { if ($arr[2] == 2) { $image = imagecreatefromjpeg($targetFile); } } } if ($arr[2] == 1) { $mimage = imagecreatefromgif($targetFile); } else { if ($arr[2] == 3) { $mimage = imagecreatefrompng($targetFile); } else { if ($arr[2] == 2) { $mimage = imagecreatefromjpeg($targetFile); } } } //将原图生成缩略图资源 imagecopyresized($thumb, $image, $x, $y, 0, 0, $width, $height, $arr[0], $arr[1]); imagecopyresized($mthumb, $mimage, $mx, $my, 0, 0, $mwidth, $mheight, $arr[0], $arr[1]); //将缩略图资源生成图片 if ($arr[2] == 1) { imagegif($thumb, $newfile); } else { if ($arr[2] == 3) { imagepng($thumb, $newfile); } else { if ($arr[2] == 2) { imagejpeg($thumb, $newfile); } } } if ($arr[2] == 1) { imagegif($mthumb, $mnewfile); } else { if ($arr[2] == 3) { imagepng($mthumb, $mnewfile); } else { if ($arr[2] == 2) { imagejpeg($mthumb, $mnewfile); } } } $ftp = new Ftp(); $ftp->uploadfile($mnewfile, $mimgname); $ftp->uploadfile($bnewfile, $imgurlname); $res = $ftp->uploadfile($newfile, $newimgname); $ftp->close(); if ($res['success']) { //删除本地文件 @unlink($targetFile); @unlink($newfile); @unlink($mnewfile); if ($add == '1') { // $rs = array('code' => 200, 'filename' => $newimgname, 'GoodsNO' => $GoodsNO, 'ImgName' => $ImgName, 'GoodsName' => $GoodsName, 'GoodsID' => $GoodsID, 'msg' => '上传成功!'); } else { $rs = array('code' => 200, 'filename' => $newimgname, 'ftpfileurl' => $res['url'], 'ImgName' => $ImgName, 'msg' => '上传成功'); } } } else { $rs = array('code' => 100, 'msg' => '上传失败!' . $ImgName . '已经上传'); echo json_encode($rs); exit; } } else { $rs = array('code' => 100, 'msg' => '上传失败!'); } echo json_encode($rs); }