0

I've tried looking at a lot of solutions and still can't seem to find a fix for my issue. I keep getting the following error:

Parameter #2 $string of function explode expects string, SimpleXMLElement|null given. 

I've never had this issue before and it's only started appearing after updating Statamic to the latest version. This is my code within the GenerateFavicons.php:

private function createThumbnail(
    string $import,
    string $export,
    int $width,
    int $height,
    string $background,
    ?int $border
): void {
$svg = file_get_contents($import);
    if (!$svg) {
        return;
    }
    $svgObj = simplexml_load_string($svg);

    if (!$svgObj) {
        return;
    }

    $viewBox = explode(' ', $svgObj['viewBox']);
    $viewBoxWidth = intval($viewBox[2]);
    $viewBoxHeight = intval($viewBox[3]);

The error is showing for the following line of code:

$viewBox = explode(' ', $svgObj['viewBox']);

From what I've seen, a lot of people have been saying you shouldn't explode an array. I'm not quite sure where to go from here.

Thanks in advance.

3
  • 1
    Why would you explode an array? It's already broken into pieces. Also, your error is saying that $svgObj['viewBox'] is not a string. Did try var_dump($svgObj['viewBox']) to see what $svgObj['viewBox'] is? Commented Nov 11, 2021 at 11:51
  • 2
    $svgObj['something'] is itself a SimpleXMLElement (or could be null) which is what the warning is about. The code itself will probably work because of type juggling but it's better to explicitly cast it using e.g. explode(' ', strval($svgObj['viewBox'])). Seeing how the value could be null it's probably good to check that as well Commented Nov 11, 2021 at 11:56
  • @apokryfos, that got rid of the error. Thank you Commented Nov 11, 2021 at 11:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.