I have this small code to generate profile images with random background color and first letter of name as text over it. I've been using this code for years and it still works fine on xampp and many other sites but in one praticular case its not working on the server.
The image is not being saved. I've checked all directories have 755 permissions. GD is installed and there are no errors in the apache log.
<?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);
I've seen similar questions and their solutions didn't help. How do I diagnose the problem?
Edit: Its not just image files but php can't create any files at all. Other codes that generate csv files are also failing. It seems php just can not write files on this server. Its no permission problem I'm sure of it so there's some configuration issue. How should I get to the root of the problem?