Why am I getting ‘Failed to decode QR code: An error occurred while processing some image(s)’ in PHP with ImageMagick?

I am working on a PHP application that involves decoding QR codes using ImageMagick. However, I encountered the following error in my logs:

{"logtime":"2024-12-04 14:19:45.955","message":"Processing file: Image_001.jpg","context":[],"level":200,"level_name":"INFO","channel":"local","datetime":{"date":"2024-12-04 14:19:45.955052","timezone_type":3,"timezone":"Asia/Jakarta"},"extra":{"ip":"NB-ANDY","ipserver":"NB-ANDY"}}
{"logtime":"2024-12-04 14:19:48.191","message":"Failed to decode QR code: An error occurred while processing some image(s). This includes bad arguments, I/O errors and image handling errors from ImageMagick.","context":[],"level":400,"level_name":"ERROR","channel":"local","datetime":{"date":"2024-12-04 14:19:48.191321","timezone_type":3,"timezone":"Asia/Jakarta"},"extra":{"ip":"NB-ANDY","ipserver":"NB-ANDY"}}
{"logtime":"2024-12-04 14:19:48.192","message":"QR code not detected for file: Image_001.jpg","context":[],"level":400,"level_name":"ERROR","channel":"local","datetime":{"date":"2024-12-04 14:19:48.192000","timezone_type":3,"timezone":"Asia/Jakarta"},"extra":{"ip":"NB-ANDY","ipserver":"NB-ANDY"}}

This is My Code:

    private function readQrCode($fileContents)
    {
        $image = Image::make($fileContents)
            ->resize(1200, null, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            })
            ->contrast(15)
            ->brightness(20)
            ->sharpen(10);
            
            if (!$image->width() || !$image->height()) {
                Log::error("Invalid image: cannot read dimensions.");
                return null;
            }
    
        $format = $image->mime();
        $extension = str_replace('image/', '', $format);
    
        if ($extension === 'jpeg' || $extension === 'jpg') {
            $extension = 'jpg';
        } elseif ($extension === 'png') {
            $extension = 'png';
        }
    
        $tempFile = tempnam(sys_get_temp_dir(), 'qr') . '.' . $extension;
        file_put_contents($tempFile, $image->encode($extension, 90));
    
        try {
            $QRCodeReader = new Zbar($tempFile);
            $qrcodeText = $QRCodeReader->scan();
            unlink($tempFile);
    
            preg_match('/Contract No. ?: ?(d+)/i', $qrcodeText, $leaseMatches);
            preg_match('/DocumentTypeIds*:s*(w+)/i', $qrcodeText, $docTypeMatches);
            preg_match('/Customer Names*:s*(.+)/i', $qrcodeText, $customerNameMatches);
            preg_match('/Document Types*:s*.+s*-s*(.+)/i', $qrcodeText, $receiverNameMatches);
            preg_match('/Document Types*:s*([^-]+)/i', $qrcodeText, $docTypeNameMatches);
    
            return [
                'lease_no' => $leaseMatches[1] ?? null,
                'document_type_id' => $docTypeMatches[1] ?? null,
                'document_type_name' => $docTypeNameMatches[1] ?? null,
                'customer_name' => $customerNameMatches[1] ?? null,
                'receiver_name' => $receiverNameMatches[1] ?? null,
                'extension' => $extension,
            ];
        } catch (Exception $e) {
            unlink($tempFile);
            Log::error("Failed to decode QR code: " . $e->getMessage());
            return null;
        }
    }     

Here’s what I’ve tried so far:

  1. Ensured that the ImageMagick extension is installed and enabled in my PHP environment.

  2. Verified that the input images are valid and not corrupted.

  3. Checked the permissions of the image file to make sure it’s accessible.

My Environment:

  • PHP Version: 7.2

  • ImageMagick Version: ImageMagick 7.1.1-41 Q16 x86 bbdcbf7:20241116 https://imagemagick.org

  • Operating System: Windows 10, Win32

  • Additional Libraries: ZBar for QR code reading

What could be causing this error, and how can I debug or resolve it? Is this an issue with:

  • ImageMagick configuration?

  • QR code library (e.g., ZBar)?

  • The input image?

Any insights or suggestions would be greatly appreciated!