A non well formed numeric value encountered Laravel SimpleQrcode

Viewed 231

I am working on simple qrcode laravel. I'm trying to store color in rgba format(ajax) in database for a specific id's qrcode's background color and calling it as a variable to change the qr-code's bgcolor. I cannot use hex format because simpleqrcode dependency only accepts rgba format.

So i have stored rgba in database and when i call it to the controller it show me error. A non well formed numeric value encountered. further I researched that when i call colors from database it come with quotations by default and i tried to replace it with str_replace but didn't work.

This is my code:

public function qrcode($id){
      $article = Article::find($id);
      $rgba = $article->bgcolor;
      $html = str_replace('"', '', $rgba);

      $image_path = \QrCode::format('png')
//              ->merge('../storage/app/public/'.$article->image, .15, true)
              ->size(200)

                ->backgroundColor($html)
              ->errorCorrection('H')


              ->generate('127.0.0.1:8000/articles/'.$article->id , '../public/Qrcodes'.$article->image);
// dd($article->bgcolor);
    // $image = '../public/'.$article->image;

      return view('articles.modify_qrcode', compact('article'));

error

error

somebody told me to update composer. i have already updated it.

1 Answers

Based on your comment; dd($html); result: "135, 56, 56"

You code

$rgba = $article->bgcolor;
$html = str_replace('"', '', $rgba);

Will create the variable $html with the string value "135,56,56" But you need 3 integer variables $red, $green and $blue since backgroundColor(int $red, int $green, int $blue, ?int $alpha = null) take 3 colors separately.

What you can do is;

$article = Article::find($id);
list($red, $green, $blue) = array_map('intval', explode(',', $article->bgcolor));

$image_path = \QrCode::format('png')
              ->size(200)
              ->backgroundColor($red, $green, $blue)
              ->errorCorrection('H')
              ->generate('127.0.0.1:8000/articles/'.$article->id , '../public/Qrcodes'.$article->image);

Explanation:

  • explode(',', $article->bgcolor) Will transform the string "135, 56, 56" into an array of string: ["123", "56" and "56"]
  • array_map('intval', []) Will iterate through the array of string to cast them into an array of int : ["123", "56" and "56"] will become [123, 56 and 56]
  • list($red, $green, $blue) will assign the array values into each variables; $red, $green and $blue
Related