phpphp-7php-5.3e-notices

Errors PHP Notice: Undefined offset:, Trying to access array offset on value of type null


I get multiples php errors in server log , the error show on 2 lines of my php code, However php script is working but it show these errors in server log.

PHP Notice:  Trying to access array offset on value of type null 
PHP Notice:  Undefined offset: 79
PHP Notice:  Undefined offset: 89
PHP Notice:  Undefined offset: 32 

Here i added the partial piece of my php code where error is showing:

function right2img($color,$text,$pos,$font,$fontxy,$im){
    $letters = imagecreatefromstring(base64_decode($font));
    $rgb = getRGB($color);
    $index = imagecolorexact($letters, 0, 0, 0);
    imagecolorset ($letters, $index, $rgb[0], $rgb[1], $rgb[2]);
    for($i=0;$i<strlen($text);$i++){
        $c = ord($text[$i]);
        imagecopy ($im, $letters, $pos, 5, $fontxy[$c]["x"], $fontxy[$c]["y"], $fontxy[$c]["w"], 5);
        $pos+= $fontxy[$c]["w"];
    }
    return $im;
}

The errors are came from these 2 lines of above code

    imagecopy ($im, $letters, $pos, 5, $fontxy[$c]["x"], $fontxy[$c]["y"], $fontxy[$c]["w"], 5);
    $pos+= $fontxy[$c]["w"];

Can't figure how to fix it. ....any help ?


Solution

  • The $fontxy might be null.
    You have to check the valid index value for $fontxy.

    if(isset($fontxy[$c]) && isset($fontxy[$c]["x"]) && isset($fontxy[$c]["y"])&& isset($fontxy[$c]["w"])){
    imagecopy ($im, $letters, $pos, 5, $fontxy[$c]["x"], $fontxy[$c]["y"], $fontxy[$c]["w"], 5);
            $pos+= $fontxy[$c]["w"];
    }