-1

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?

StuiterSlurf
  • 2,464
  • 4
  • 34
  • 55
Whip
  • 1,891
  • 22
  • 43

1 Answers1

1

Option 1: You can get the access of the folder by. Then the username will get the ownership of that folder.

sudo chown -R <username> /var/www

Option 2: you can cange the folder permissions by

sudo chmod -R a+rwX /var/www

Option 3: Or you can give a group access to the folder

sudo groupadd www
sudo useradd -g <username> www
sudo chown -R :www /var/www
sudo chmod -R g+rwX /var/www
sudo chmod g+s /var/www

Documentation: You can read more about this in their serverguide:

https://help.ubuntu.com/lts/serverguide/serverguide.pdf

StuiterSlurf
  • 2,464
  • 4
  • 34
  • 55