我有这个小代码来生成具有随机背景颜色和名称的第一个字母作为文本的个人资料图像。我多年来一直在使用此代码,它在 xampp 和许多其他站点上仍然可以正常工作,但在一个实际情况下,它不能在服务器上工作。
图像未保存。我检查了所有目录都有 755 权限。GD 已安装并且 apache 日志中没有错误。
<?php
$name = 'Vipul Kapoor';
$id = 905;
$location = '';
$words = explode(" ", $name);
$countWords = count($words);
if($countWords >= 2){
$firstChars = $words[0][0].$words[$countWords-1][0];
}else{
$firstChars = "";
foreach ($words as $w)
{
$firstChars .= $w[0];
}
}
$alphaCount = strlen($firstChars);
$font = 'fonts/roboto/Roboto-Regular.ttf';
$imgName = $id.'-default.png';
$im = imagecreate(215, 215);
$background_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
$text_color = imagecolorallocate($im, 255, 255, 255);
if($alphaCount == 1) { imagettftext($im, 60, 0, 80, 132, $text_color, $font, $firstChars); }
if($alphaCount == 2) { imagettftext($im, 50, 0, 65, 132, $text_color, $font, $firstChars); }
if($location == ''){
imagepng($im, 'images/profile/'.$imgName);
}else{
imagepng($im, 'admin/img/avatar/'.$imgName);
}
imagedestroy($im);
我见过类似的问题,他们的解决方案没有帮助。如何诊断问题?
编辑:它不仅是图像文件,而且 php 根本无法创建任何文件。生成 csv 文件的其他代码也失败了。似乎 php 无法在此服务器上写入文件。我确定它没有权限问题,所以存在一些配置问题。我应该如何找到问题的根源?