用 PHP实现百变头像 [转]
作者:Phzzy
题目很吓人,百变头像,呵呵。其实就是随机显示图片。
原理:读取一组图片地址,存到一个数组,利用随机函数从中取出一张,输出图片。
读取一组图片地址:
图片地址的来源很多,数据库,文件夹或者自己定的读取规则。在这我们选择从一个文件夹中
读取。
主要相关的函数就两个:opendir , readdir。
相关代码(对应程序文件:read_dir.php ):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php // 获取文件夹内图片 $image_mime_array = array('image/jpeg' , 'image/gif' , 'image/png'); // mime 类型 $dir = 'images'; // 图片文件夹 $image_array = read_dir($dir); print_r($image_array); function read_dir($dir){ global $image_mime_array; $file_array = array(); if ($handle = opendir($dir)) { // 打开文件夹 while (false !== ($file = readdir($handle))) { // 循环获取目录内文件信息 if ($file != "." && $file != "..") { $image_info = getimagesize($dir . '/' . $file); // 获取图片信息 // 如果 mime 符合图片类型,则放入数组中 if(in_array($image_info['mime'] , $image_mime_array)){ array_push($file_array , $file); } } } closedir($handle); } return $file_array; } |
这个是读取一个目录下所有文件的函数,因为我们只读取图片,所以通过 getimagesize 函数获
取文件图片信息(当然你也可以通过其他一些方式,但不要通过判断后缀的方式,那是相当不准的)。
随机取出一张图片,输出:
通过上面的 read_dir函数,得到一个数组,包含图片名,然后用 array_rand 函数得到一个随机的
数组下标,并用 file_get_contents函数获取整个图片内容,最后以对应的 mime 输出就可以了。
相关代码(对应程序文件:rand_pic.php ):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php $image_mime_array = array('image/jpeg' , 'image/gif' , 'image/png'); $dir = 'images'; $image_array = read_dir($dir); if(empty($image_array)){ echo '没有图片'; exit(); } $rand_key = array_rand($image_array); // 获得随机下标 $rand_image = $image_array[$rand_key]; // 获得随机图片名 $image = file_get_contents($dir . '/' . $rand_image); // 获取图片内容 $image_info = getimagesize($dir . '/' . $rand_image); // 获取图片信息 $image_mime = $image_info['mime']; @header('Content-type: image/' . $image_mime); // 发送http头 echo $image; // 输出 function read_dir($dir){ // 函数详细代码请参考第一步 } |
获取图片的来源可以改成数据库,比如读一组图片,生成一个 image_array,然后后面步骤一样
就可以得到百变图片了,嘿嘿。
在 html 里的引用方法是
上面这是最简单的显示图片的方式了,我们可以通过 GD库来扩展这个功能,使得得到的图片
是同样的尺寸,甚至可以为每张图片加上各种效果哦☺
下面介绍一个比较简单的功能:生成同样尺寸的图片。主要用到的一个自定义函数:
generate_thumb
相关代码:(具体代码请参考 rand_pic_2.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | function generate_thumb($source_img , $new_width , $new_height , $if_cut) { //图片类型 $img_info = getimagesize($source_img); $img_type = $img_info['mime']; //初始化图象 if($img_type=="image/jpeg") $temp_img = imagecreatefromjpeg($source_img); if($img_type=="image/gif") $temp_img = imagecreatefromgif($source_img); if($img_type=="image/png") $temp_img = imagecreatefrompng($source_img); //原始图象的宽和高 $old_width = imagesx($temp_img); $old_height = imagesy($temp_img); //改变前后的图象的比例 $new_ratio = $new_width/$new_height; $old_ratio = $old_width/$old_height; //生成新图象的参数 //情况一:裁图 则按设置的大小生成目标图象 if($if_cut=="1") { $new_width = $new_width; $new_height = $new_height; if($old_ratio>=$new_ratio) { //高度优先 $old_width = $old_height*$new_ratio; $old_height = $old_height; } else { //宽度优先 $old_width = $old_width; $old_height = $old_width/$new_ratio; } } else { //情况二:不裁图 则按比例生成目标图象 $old_width = $old_width; $old_height = $old_height; if($old_ratio>=$new_ratio) { //高度优先 $new_width = $new_width; $new_height = $new_width/$old_ratio; } else { //宽度优先 $new_width = $new_height*$old_ratio; $new_height = $new_height; } } //生成新图片 $new_img = imagecreatetruecolor($new_width,$new_height); if($img_type=='png' || $img_type=='gif') { imagealphablending($new_img, FALSE);//取消默认的混色模式 imagesavealpha($new_img,TRUE); //设定保存完整的 alpha 通道信息 } imagecopyresampled($new_img,$temp_img,0,0,0,0,$new_width,$new_height, $old_width,$old_height); // 拷贝图片 if($img_type=="image/jpeg") imagejpeg($new_img); if($img_type=="image/gif") imagegif($new_img); if($img_type=="image/png") imagepng($new_img); imagedestroy($new_img); } |
最后调用一次就可以了
generate_thumb($dir . ‘/’ . $rand_image , 100 , 100 , true);
几个参数的意思:
$source_img:原始图片地址
$new_width:生成图片的高度
$new_height:生成图片的宽度
$if_cut:是否剪切(不剪切就是按比例生成)
实际代码部分和文章中的可能有细微差别,以代码为准拉☺。好的程序也是由好的创意来的,
呵呵。




