1 | <?php |
||
2 | |||
3 | namespace App\Utils; |
||
4 | |||
5 | use Aws\Rekognition\RekognitionClient; |
||
6 | use Illuminate\Support\Facades\Storage; |
||
7 | |||
8 | class ResumeParser |
||
9 | { |
||
10 | public static function extractPhoto($resumeFilePath, $photoFilePath) |
||
11 | { |
||
12 | $imageData = null; |
||
13 | |||
14 | //File::mimeType($resumeFilePath) does not work on s3 |
||
15 | if (substr($resumeFilePath, -4) === '.pdf' && Storage::disk('s3')->exists($resumeFilePath)) { |
||
16 | $imageData = self::pdfToImage($resumeFilePath); |
||
17 | } |
||
18 | |||
19 | if ($imageData) { |
||
20 | $photo = self::rekognizePhoto($imageData); |
||
21 | |||
22 | if ($photo) { |
||
23 | Storage::disk('s3-avatars')->put($photoFilePath, $photo->getImageBlob()); |
||
24 | |||
25 | return true; |
||
26 | } |
||
27 | } |
||
28 | |||
29 | return false; |
||
30 | } |
||
31 | |||
32 | protected static function pdfToImage($resumeFilePath) |
||
33 | { |
||
34 | $pdfFile = Storage::disk('s3')->get($resumeFilePath); |
||
35 | |||
36 | return PdfToImage::fromContent($pdfFile)->getImageData(); |
||
37 | } |
||
38 | |||
39 | protected static function rekognizePhoto($imageData): ?\Imagick |
||
40 | { |
||
41 | $options = [ |
||
42 | 'region' => 'eu-central-1', |
||
43 | 'version' => 'latest', |
||
44 | ]; |
||
45 | |||
46 | $rekognition = new RekognitionClient($options); |
||
47 | $result = $rekognition->detectLabels( |
||
48 | [ |
||
49 | 'Image' => [ |
||
50 | 'Bytes' => $imageData, |
||
51 | ], |
||
52 | ] |
||
53 | ); |
||
54 | |||
55 | if ($result) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
56 | foreach ($result['Labels'] as $label) { |
||
57 | if ($label['Name'] == 'Person' && !empty($label['Instances'] && !empty($label['Instances'][0]['BoundingBox']))) { |
||
58 | return self::cropImage( |
||
59 | $imageData, |
||
60 | $label['Instances'][0]['BoundingBox']['Width'], |
||
61 | $label['Instances'][0]['BoundingBox']['Height'], |
||
62 | $label['Instances'][0]['BoundingBox']['Left'], |
||
63 | $label['Instances'][0]['BoundingBox']['Top'] |
||
64 | ); |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | return null; |
||
70 | } |
||
71 | |||
72 | protected static function cropImage($imageData, $width, $height, $left, $top) |
||
73 | { |
||
74 | $image = new \Imagick(); |
||
75 | $image->readImageBlob($imageData); |
||
76 | $geometry = $image->getImageGeometry(); |
||
77 | |||
78 | $cropX = $left * $geometry['width']; |
||
79 | $cropY = $top * $geometry['height']; |
||
80 | $cropWidth = $width * $geometry['width']; |
||
81 | $cropHeight = $height * $geometry['height']; |
||
82 | |||
83 | $image->cropImage($cropWidth, $cropHeight, $cropX, $cropY); |
||
84 | |||
85 | return $image; |
||
86 | } |
||
87 | } |
||
88 |