skip to Main Content

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 ?

2

Answers


  1. 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"];
    }
    
    Login or Signup to reply.
  2. Before accessing $fontxy[$c]["x"], $fontxy[$c]["y"], and $fontxy[$c]["w"], you should check if those keys exist in the $fontxy array using the isset() function. This will prevent the "Undefined offset" errors.

    if (isset($fontxy[$c]["x"], $fontxy[$c]["y"], $fontxy[$c]["w"])) {
        imagecopy ($im, $letters, $pos, 5, $fontxy[$c]["x"], $fontxy[$c]["y"], $fontxy[$c]["w"], 5);
        $pos += $fontxy[$c]["w"];
    } else {
        // Handle the case when the offsets are undefined
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search