php要实现对图片的操作,需要开启GD扩展,所以最好打开phpinfo()看一下GD扩展是否开启。
ps:之前项目中就碰到本机(window集成环境)程序已经正常执行,图片也能生成,但是放到测试环境(linux)就显示不出图片。调试了一下是因为缺少ImageTTFText()函数,后又重装PHP加上GD扩展,又出现问题,用ImageTTFText($im, 18, 0, 10, 30*$height, $black, "../fonts/simhei.ttf", $l_resutl_name.':'.$studentExits['uname'].' '.$l_result_score.':'.$studentExits['score']); 往图片上添加文字无效, 排查原因, 这个函数有了,但是字体路径不识别,是GD扩展的一个依赖包路径不对,重新安装php又正常显示;
<?php
/*
* 学生以jpg图片格式,保存自己的答题情况
*/
include 'header.php';
Header("Content-type: image/gif");
//--- 解决保存中文文件名乱码的问题 ---//
$ua = $_SERVER["HTTP_USER_AGENT"];
$filename = '课堂练习'.date('YmdHis',time()).'.gif';
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
header('Content-Type: application/octet-stream');
if (preg_match("/MSIE/", $ua) || preg_match("/rv:/", $ua)) {// || preg_match("/rv:/", $ua)匹配IE11
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
//---- 查询学生答题信息 start ----//
$studentId = getParmInt('userId');
$paper = $db->select('modular_vote_question_paper','id,status','id='.$fid);
$paper_id = $paper['id']; //试卷id
//如果试卷不是已经公布答案的状态
if($paper['status'] != 3){
//exit;
}
//---- 查询已有试题 ---- //
$questionArr = $db->selectAll('modular_vote_questions ', '*', 'fid='.$fid.' and isdelete=0');
foreach ($questionArr as $qv){
$optionArr[$qv['id']] = $qv['option'];
}
$optionStr = implode(',', $optionArr);
$optionStr = $optionStr?$optionStr:0;
$optionsArr = $db->selectAll('modular_vote_question_option', 'id,title', 'id in('.$optionStr.') order by id asc');
foreach ($optionsArr as $ov){
$optionssArr[$ov['id']] = $ov['title'];
}
//---- 学生提交的答案 ----//
$student_answer = $db->selectAll('modular_vote_answers','question_id,answer','fid='.$fid.' and uid='.$studentId.' order by id asc');
foreach ($student_answer as $k=>$v){
$student_answer[$v['question_id']] = $v['answer'];
}
$studentExits = $db->select('modular_vote_answer_user','id,status,score,uname','fid='.$fid.' and uid='.$studentId);
//---- 查询学生答题信息 end ----//
$im = imagecreate(1500,200);//创建一个底图
$white = ImageColorAllocate($im, 255,255,255);//第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色
$black = ImageColorAllocate($im, 0,0,0);
$height = 1;//文字高度位置,控制文字在图片中的位置
ImageTTFText($im, 18, 0, 10, 30*$height, $black, "../fonts/simhei.ttf", $l_resutl_name.':'.$studentExits['uname'].' '.$l_result_score.':'.$studentExits['score']); //图片上打印学生姓名得分
$height++;
foreach ($questionArr as $k=>$qa){
$str = $qa['title'];
ImageTTFText($im, 18, 0, 10, 30*$height, $black, "../fonts/simhei.ttf", $l_question.':'.$str); //图片上打印标题
$height++;
foreach (explode(',', $optionArr[$qa['id']]) as $qk=>$qv){
$str = $optionssArr[$qv];
ImageTTFText($im, 18, 0, 10, 30*$height, $black, "../fonts/simhei.ttf", $sortArr[$qk].'.'.$str); //图片上打印题目选项
$height++;
}
ImageTTFText($im, 18, 0, 10, 30*$height, $black, "../fonts/simhei.ttf", $l_your_chance.' '.$student_answer[$qa['id']]); //图片上打印学生答案
$height++;
if($student_answer[$qa['id']] == $qa['answer']){
//绿色
ImageTTFText($im, 18, 0, 10, 30*$height, ImageColorAllocate($im, 32,255,54), "../fonts/simhei.ttf", $l_your_right); //图片上打印学生答案
}else{
//红色
ImageTTFText($im, 18, 0, 10, 30*$height, ImageColorAllocate($im, 251,41,36), "../fonts/simhei.ttf", $l_right_chance.' '.$qa['answer']); //图片上打印学生答案
}
$height = $height + 2;
//如果还有下一道题,就给图片延长高度+150px
if (isset($questionArr[$k+1])){
imgresize($im,$height);
//重新声明黑色
$black = ImageColorAllocate($im, 0,0,0);
}
}
ImageGif($im);
ImageDestroy($im);
exit;
//重新定制图片尺寸,该函数的$img参数为引用传参
function imgresize(&$img,$height){
$imgWidth = 1500;
$imgHeight = 30*$height+240;
$image = imagecreate($imgWidth,$imgHeight);
ImageColorAllocate($image, 255,255,255);//第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色
imagecopyresampled($image, $img, 0, 0, 0, 0,$imgWidth,$height*30,1500, $height*30);//将原有图片内容拷贝过来
$img = $image;
}