Home > Enterprise >  PHP explode() must be of type string
PHP explode() must be of type string

Time:07-02

I'm getting the average color of an image this way:

exec('magick "' . $tempCut . '" -resize 1x1\! -format "%[fx:int(255*r .5)],%[fx:int(255*g .5)],%[fx:int(255*b .5)]" info:-', $col, $return_var);
file_put_contents($root . '/wth.txt', $col);
$arr = explode(',', $col);

wth.txt contains this string:

15,81,139

but explode gives the fatal error that it has to be a string, even though it is one??

What's wrong here and how to fix this?

CodePudding user response:

As per https://www.php.net/manual/en/function.exec.php exec output param is an array:

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

So, $col is an array and not a string. If you are sure that the string you are looking for is in the first line of output then following should work:

exec('magick "' . $tempCut . '" -resize 1x1\! -format "%[fx:int(255*r .5)],%[fx:int(255*g .5)],%[fx:int(255*b .5)]" info:-', $col, $return_var);
file_put_contents($root . '/wth.txt', $col);
$arr = explode(',', $col[0]);

hope this helps.

  •  Tags:  
  • php
  • Related