代码如下 | 复制代码 |
<?php /** * php生成验证码 * @param $width 画布宽 * @param $height 画布高 * @param $vcodelen 验证码长度 * @param $pointnum 干扰像素点数量 * @param $linenum 干扰线条数量 * * 思路:创建验证码画布,生成并填充背景色,生成验证码内容/干扰像素点/线,填充到画布,输出。 */ $width= 100; $height= 30; $vcodelen= 4; $pointnum= 200; $linenum= 3; // 创建画布 $image= imagecreatetruecolor($width,$height); // 创建色块 $bgcolor= imagecolorallocate($image, 255, 255, 255); // 填充画布背景色 imagefill($image, 0, 0,$bgcolor); // 验证码内容 for($i=0;$i<$vcodelen;$i++) { // 字体大小 $fontsize= 5; // 字体颜色,颜色在限定范围内随机 $fontcolor= imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); $data='abcdefghijklmnopqrstuvwxyz0123456789' // 验证码内容在以上字符串内随机截取 $fontcontent=substr($data, rand(0,strlen($data)),1); // 字符串显示位置 $x= ($i*$width/4)+rand(5,15); $y= rand(5,10); // 字符串填充图片 // imagestring的字体大小可选1-5,字体再大需要用imagettftext函数(需要字体文件) imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); // imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, '/font/Geneva.dfont', $fontcontent); } // 干扰像素点 for($i=0;$i<$pointnum;$i++) { $pointcolor= imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); // 画布填充像素点函数 imagesetpixel($image, rand(0,$width), rand(0,$height),$pointcolor); } // 干扰线条 for($i=0;$i<$linenum;$i++) { $linecolor= imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); // 画布填充线条函数 imageline($image, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height),$linecolor); } // 图片输出格式 header('content-type: image/png'); // 输出验证码图片 imagepng($image); // 销毁画布 imagedestroy($image); ?> |
小编给大家推荐的这篇文章介绍了php生成图片缩略图功能示例,非常实用,有兴趣的同学快看看吧
本文实例讲述了php生成图片缩略图功能。分享给大家供大家参考,具体如下:
完整代码如下:
代码如下 | 复制代码 |
<?php /* * Created on 2011-3-18 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ /*构造函数-生成缩略图+水印,参数说明: $srcFile-图片文件名, $dstFile-另存文件名, $markwords-水印文字, $markimage-水印图片, $dstW-图片保存宽度, $dstH-图片保存高度, $rate-图片保存品质*/ makethumb("1.jpg","aa/b.jpg","50","50"); functionmakethumb($srcFile,$dstFile,$dstW,$dstH,$rate= 100,$markwords= null,$markimage= null) { $data=GetImageSize($srcFile); switch($data[2]) { case1: $im= @ImageCreateFromGIF($srcFile); break; case2: $im= @ImageCreateFromJPEG($srcFile); break; case3: $im= @ImageCreateFromPNG($srcFile); break; } if(!$im)returnFalse; $srcW= ImageSX($im); $srcH= ImageSY($im); $dstX= 0; $dstY= 0; if($srcW*$dstH>$srcH*$dstW) { $fdstH=round($srcH*$dstW/$srcW); $dstY=floor(($dstH-$fdstH) / 2); $fdstW=$dstW; }else{ $fdstW=round($srcW*$dstH/$srcH); $dstX=floor(($dstW-$fdstW) / 2); $fdstH=$dstH; } $ni= ImageCreateTrueColor($dstW,$dstH); $dstX= ($dstX< 0) ? 0 :$dstX; $dstY= ($dstX< 0) ? 0 :$dstY; $dstX= ($dstX> ($dstW/ 2)) ?floor($dstW/ 2) :$dstX; $dstY= ($dstY> ($dstH/ 2)) ?floor($dstH/ s) :$dstY; $white= ImageColorAllocate($ni, 255, 255, 255); $black= ImageColorAllocate($ni, 0, 0, 0); imagefilledrectangle($ni, 0, 0,$dstW,$dstH,$white);// 填充背景色 ImageCopyResized($ni,$im,$dstX,$dstY, 0, 0,$fdstW,$fdstH,$srcW,$srcH); if($markwords!= null) { $markwords= iconv("gb2312","UTF-8",$markwords); //转换文字编码 ImageTTFText($ni, 20, 30, 450, 560,$black,"simhei.ttf",$markwords);//写入文字水印 //参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容
}elseif($markimage!= null) { $wimage_data=GetImageSize($markimage); switch($wimage_data[2]) { case1: $wimage= @ImageCreateFromGIF($markimage); break; case2: $wimage= @ImageCreateFromJPEG($markimage); break; case3: $wimage= @ImageCreateFromPNG($markimage); break; } imagecopy($ni,$wimage, 500, 560, 0, 0, 88, 31);//写入图片水印,水印图片大小默认为88*31 imagedestroy($wimage); } ImageJpeg($ni,$dstFile,$rate); ImageJpeg($ni,$srcFile,$rate); imagedestroy($im); imagedestroy($ni); } ?> |
PHP使用在很多用户的生活中是很重要的,那么php怎么获取excel文件数据呢?有些什么获取方法,还不知道的下面我们就一起来看看。
很简单就可以实现,下面为大家简单介绍下
1、下载PHPExcel类,是一个文件夹,还得有一个文件PHPExcel.php,两个在同级目录
require__DIR__ .'./PHPExcel/IOFactory.php'
$PHPReader=new\PHPExcel_Reader_Excel2007();
//判断文件类型
if(!$PHPReader->canRead($filePath)) {
$PHPReader=new\PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)) {
echo'no Excel'
returnfalse;
}
}
$PHPExcel=$PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet=$PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn=$currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow=$currentSheet->getHighestRow();
/**从第1行开始输出*/
for($currentRow= 1;$currentRow<=$allRow;$currentRow++) {
/**从第A列开始输出*/
for($currentColumn='A'$currentColumn<=$allColumn;$currentColumn++) {
$val=$currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();
/**ord()将字符转为十进制数*/
$date[$currentRow- 1][] =$val;
}
}
return$date;
Mac系统也是现在使用比较多的系统,今天文章要给大家介绍的是mac系统下怎么安装多个php并自由切换,还不知道具体方法的下面一起来看看。最近工作中遇到一个问题,需要实现在mac系统下安装多个php并实现自由切换,通过查找相关的资料找到了解决的方法,所以想着总结下来,方便大家和自己学习参考,下面话不多说,来看看的介绍吧。
一、安装多版本php
$ brew install php54
$ brew install php70
如果安装报以下错误:
Error: Cannot install homebrew/php/php70 because conflicting formulae are installed.
php54: because different php versions install the same binaries.
Please `brew unlink php54` before continuing.
解决办法有二种:
1、brew unlink php54
2、删除/usr/local/bin,下面有关php的软链接
上面2种方法,目的是一样的。
二、安装切换工具php-version
$ brew install php-version
$ source $(brew --prefix php-version)/php-version.sh
$ php-version
5.4.45
* 7.0.17
$ php-version 5.4.45 //切换到5.4
$ php-version
* 5.4.45
7.0.17
$ echo "source $(brew --prefix php-version)/php-version.sh" >> ~/.bash_profile
代码如下 | 复制代码 |