classImage {/**
* 图片上增加文字
* @param type $strImgPath
* @param type $arrText*/
public function imgAddText($strText, $intWidth, $intHeight) {//随机取背景图片
$strImgPath = $this->RandomFile(__DIR__ . '/verifycodeimages/', 'jpg|png');if (empty($strImgPath) || !file_exists($strImgPath)) {return null;
}//获取图片信息
$imgInfo = getimagesize($strImgPath);$imgRes = null;switch ($imgInfo[2]) {case 1:
$imgRes = @imagecreatefromgif($strImgPath);break;case 2:
$imgRes = @imagecreatefromjpeg($strImgPath);break;case 3:
$imgRes = @imagecreatefrompng($strImgPath);break;
}//重新设置图片大小
$intPicWidth = imagesx($imgRes);$intPicHeight = imagesy($imgRes);if (function_exists("imagecopyresampled")) {$newImgRes = imagecreatetruecolor($intWidth, $intHeight);
imagecopyresampled($newImgRes, $imgRes, 0, 0, 0, 0, $intWidth, $intHeight, $intPicWidth, $intPicHeight);
}else{$newImgRes = imagecreate($intWidth, $intHeight);
imagecopyresized($newImgRes, $imgRes, 0, 0, 0, 0, $intWidth, $intHeight, $intPicWidth, $intPicHeight);
}//释放原图资源
imagedestroy($imgRes);//设置字体信息
$strFont = __DIR__ . '/Font/shoushu.ttf';$arrTextRange = array();$intFontSize = 30;$arrXY = array();for ($intNum = 0; $intNum < mb_strlen($strText, 'utf-8'); $intNum++) {//随机获取旋转角度
$intAngle = rand(-45, 45);//随机获取文字颜色
$color = imagecolorallocate($newImgRes, rand(0, 255), rand(0, 255), rand(0, 255));//获取文字所占像素位置
$arrBox = imagettfbbox($intFontSize, $intAngle, $strFont, mb_substr($strText, $intNum, 1));$arrTureBox = imagettfbbox($intFontSize, 0, $strFont, mb_substr($strText, $intNum, 1));if ($arrBox) {$intMinX = min(array($arrBox[0], $arrBox[2], $arrBox[4], $arrBox[6]));$intMaxX = max(array($arrBox[0], $arrBox[2], $arrBox[4], $arrBox[6]));$intMinY = min(array($arrBox[1], $arrBox[3], $arrBox[5], $arrBox[7]));$intMaxY = max(array($arrBox[1], $arrBox[3], $arrBox[5], $arrBox[7]));$intFontWidth = $intMaxX - $intMinX;$intFontHeight = $intMaxY - $intMinY;$intX = $intY = 0;$intRangeMaxX = $intWidth - $intFontWidth;$intRangeMaxY = $intHeight - $intFontHeight;$arrRange = array();if (empty($arrXY)) {//如果没有画过文字,那整张图片都可以画
$arrRange[] = array(0, 0, $intRangeMaxX, $intRangeMaxY);
}else{//按x轴排序,判断排除前后几个字后,其他几个字y轴方向是否还有空间
array_multisort(array_column($arrXY, 0), SORT_ASC, $arrXY);for ($intXNum = 0; $intXNum <= count($arrXY); $intXNum++) {//取前一个字的右侧x轴为最小x
$intTempMinX = ($intXNum == 0 ? 0 : $arrXY[$intXNum - 1][2]);//获取右侧的边界值个数
for ($intLen = count($arrXY) - $intXNum + 1; $intLen >= 1; $intLen--) {//取最后一个字的左侧x轴为最大x
$intTempMaxX = ($intLen == count($arrXY) - $intXNum + 1 ? $intWidth : $arrXY[$intXNum + $intLen - 1][0]);$arrTempXY = array();if ($intLen > 1) {//文字区域按照y轴排序
$arrTempXY = array_slice($arrXY, $intXNum, $intLen - 1);array_multisort(array_column($arrTempXY, 1), SORT_ASC, $arrTempXY);
}//循环y轴区间
for ($intYNum = 0; $intYNum <= count($arrTempXY); $intYNum++) {$intTempMinY = ($intYNum == 0 ? 0 : $arrTempXY[$intYNum - 1][3]);$intTempMaxY = ($intYNum == count($arrTempXY) ? $intHeight : $arrTempXY[$intYNum][1]);if ($intTempMaxY - $intTempMinY >= $intFontHeight && $intTempMaxX - $intTempMinX >= $intFontWidth) {$arrRange[] = array($intTempMinX, $intTempMinY, $intTempMaxX - $intFontWidth, $intTempMaxY - $intFontHeight);
}
}
}
}//按y轴排序,判断排除前后几个字后,其他几个字x轴方向是否还有空间
array_multisort(array_column($arrXY, 1), SORT_ASC, $arrXY);for ($intYNum = 0; $intYNum <= count($arrXY); $intYNum++) {//取前一个字的下侧y轴为最小y
$intTempMinY = ($intYNum == 0 ? 0 : $arrXY[$intYNum - 1][3]);//获取下侧的边界值个数
for ($intLen = count($arrXY) - $intYNum + 1; $intLen >= 1; $intLen--) {//取最后一个字的上侧y轴为最大y
$intTempMaxY = ($intLen == count($arrXY) - $intYNum + 1 ? $intHeight : $arrXY[$intYNum + $intLen - 1][1]);$arrTempXY = array();if ($intLen > 1) {//文字区域按照x轴排序
$arrTempXY = array_slice($arrXY, $intYNum, $intLen - 1);array_multisort(array_column($arrTempXY, 0), SORT_ASC, $arrTempXY);
}//循环x轴区间
for ($intXNum = 0; $intXNum <= count($arrTempXY); $intXNum++) {$intTempMinX = ($intXNum == 0 ? 0 : $arrTempXY[$intXNum - 1][2]);$intTempMaxX = ($intXNum == count($arrTempXY) ? $intWidth : $arrTempXY[$intXNum][0]);if ($intTempMaxY - $intTempMinY >= $intFontHeight && $intTempMaxX - $intTempMinX >= $intFontWidth) {$arrRange[] = array($intTempMinX, $intTempMinY, $intTempMaxX - $intFontWidth, $intTempMaxY - $intFontHeight);
}
}
}
}
}//随机取一块区域,在这一区域范围内获取位置
$objRange = $arrRange[rand(0, count($arrRange) - 1)];$intX = rand($objRange[0], $objRange[2]);$intY = rand($objRange[1], $objRange[3]);$arrXY[] = array($intX, $intY, $intX + $intFontWidth, $intY + $intFontHeight);
imagettftext($newImgRes, $intFontSize, $intAngle, $intX - $intMinX, $intY - $intMinY, $color, $strFont, mb_substr($strText, $intNum, 1));$intTrueWidth = max(array($arrTureBox[0], $arrTureBox[2], $arrTureBox[4], $arrTureBox[6])) - min(array($arrTureBox[0], $arrTureBox[2], $arrTureBox[4], $arrTureBox[6]));$intTrueHeight = max(array($arrTureBox[1], $arrTureBox[3], $arrTureBox[5], $arrTureBox[7])) - min(array($arrTureBox[1], $arrTureBox[3], $arrTureBox[5], $arrTureBox[7]));$arrTextRange[] = array('x' => $intX + ($intFontWidth - $intTrueWidth) / 2,
'y' => $intY + ($intFontHeight - $intTrueHeight) / 2,
'angle' => -$intAngle,
'width' => $intTrueWidth,
'height' => $intTrueHeight,
'coordinate' => array(
['x' => $arrBox[0] + $intX - $intMinX, "y" => $arrBox[1] + $intY - $intMinY],['x' => $arrBox[2] + $intX - $intMinX, "y" => $arrBox[3] + $intY - $intMinY],['x' => $arrBox[4] + $intX - $intMinX, "y" => $arrBox[5] + $intY - $intMinY],['x' => $arrBox[6] + $intX - $intMinX, "y" => $arrBox[7] + $intY - $intMinY]
)
);
}
}ob_start();//$im是你自己创建的图片资源
imagepng($newImgRes);
imagedestroy($newImgRes);$image_data = ob_get_contents();ob_end_clean();//得到这个结果,可以直接用于前端的img标签显示
$image_data_base64 = "data:image/png;base64," . base64_encode($image_data);return array('img' => $image_data_base64, 'range' => $arrTextRange);
}/**
* 随机获取文件名
* @param type $strFileFolder
* @param type $strExtensions
* @return type*/
function RandomFile($strFileFolder = '', $strExtensions = '.*') {//fix path:
$strFileFolder = trim($strFileFolder);$strFileFolder = ($strFileFolder == '') ? './' : $strFileFolder;//check folder:
if (!is_dir($strFileFolder)) {return "";
}//create files array
$arrFiles = array();//open directory
if ($strDir = @opendir($strFileFolder)) {//go trough all files:
while ($strFile = readdir($strDir)) {if (!preg_match('/^\.+$/', $strFile) andpreg_match('/\.(' . $strExtensions . ')$/', $strFile)) {//feed the array:
$arrFiles[] = $strFile;
}
}//close directory
closedir($strDir);
}else{return "";
}if (count($arrFiles) == 0) {return "";
}//seed random function:
mt_srand((double) microtime() * 1000000);//get an random index:
$intRand = mt_rand(0, count($arrFiles) - 1);//check again:
if (!isset($arrFiles[$intRand])) {return "";
}//return the random file:
return $strFileFolder . $arrFiles[$intRand];
}
}