Introduction
With the development of the internet, image recognition technology has become one of the core features of various applications. Alibaba Cloud OCR (Optical Character Recognition), as a leading image recognition service, is widely used in various scenarios due to its powerful recognition capabilities. Laravel, a popular PHP framework, is favored by developers for its simple and efficient development model. This article will introduce how to integrate Alibaba Cloud OCR with a Laravel project, providing detailed code examples to help developers get started quickly.
1. Prerequisites
Before getting started, make sure you have completed the following preparations:
- Register an Alibaba Cloud account and enable OCR services.
- Create a new Laravel project or use an existing project.
2. Install Alibaba Cloud OCR SDK
In the root directory of your Laravel project, add the dependency for the Alibaba Cloud OCR SDK to the `composer.json` file:
"require": {
"aliyuncs/oss-sdk-php": "2.3.0"
}
Then run the following command to install the dependencies:
composer update
3. Configure Alibaba Cloud OCR SDK
In the `config` directory of your Laravel project, create a new configuration file `ocr.php` and add the following code:
<?php
return [
'access_key' => env('ALIYUN_ACCESS_KEY', ''),
'access_secret' => env('ALIYUN_ACCESS_SECRET', ''),
];
Next, add your Alibaba Cloud Access Key and Access Secret in the `.env` file:
ALIYUN_ACCESS_KEY=your_access_key
ALIYUN_ACCESS_SECRET=your_access_secret
4. Create an OCR Controller
Run the following command in the terminal to create an OCR controller:
php artisan make:controller OCRController
Then add the following code to the `OCRController`:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use AlibabaCloud\SDK\Ocr\V20191230\OcrClient;
use AlibabaCloud\SDK\Ocr\V20191230\Models\RecognizeCharacterResponse;
class OCRController extends Controller
{
public function ocr(Request $request)
{
$imagePath = $request->file('image')->path();
$fileContent = file_get_contents($imagePath);
$client = new OcrClient(config('ocr.access_key'), config('ocr.access_secret'));
try {
$result = $client->recognizeCharacter()
->withImageUrl('')
->format('json')
->request();
return response()->json($result->toArray());
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
}
5. Configure Routes
Add the following route in the `routes/web.php` file:
Route::post('/ocr', 'OCRController@ocr');
6. Create a View
Create a simple image upload view, for example:
<!DOCTYPE html>
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form method="POST" action="/ocr" enctype="multipart/form-data">
@csrf
<input type="file" name="image">
<button type="submit">Upload</button>
</form>
</body>
</html>
7. Testing
Visit `http://your_domain/ocr`, upload an image for testing. The image will be sent to the OCR API for image recognition, and the result will be returned in JSON format. You can process the result according to your project's needs.
Conclusion
Through this article, you have learned how to integrate Alibaba Cloud OCR into a Laravel project. You can make adjustments and extensions based on your specific needs to better suit your application scenarios. We hope this article was helpful!