· 7 years ago · Jun 01, 2018, 12:10 PM
1$data = str_replace('data:image/png;base64,', '', $encodedImage);
2 $data = str_replace(' ', '+', $data);
3 $data = base64_decode($data);
4 $newImageName = uniqid().'_profile_image.png';
5 $imagePathName = $uploadConfig['path'].$newImageName;
6 if (!file_exists($uploadConfig['path'])) {
7 mkdir($uploadConfig['path'], 0777, true);
8 }
9
10 file_put_contents($imagePathName, $data);
11 $webPath = $uploadConfig['image_upload_path'].'/'.$newImageName;
12 // Instantiate the client.
13 $bucket_dir = $uploadConfig['bucket'];
14 $access_id = $uploadConfig['access_key'];
15 $secret_key = $uploadConfig['aws_secret'];
16 $region = $uploadConfig['region'];
17 $s3Client = new S3Client([
18 'version' => '2006-03-01',
19 'region' => $region,
20 'credentials' => [
21 'key' => $access_id,
22 'secret' => $secret_key,
23 ],
24 ]);
25
26 $results3 = $s3Client->putObject(array(
27 'Bucket' => $bucket_dir,
28 'Key' => $webPath,
29 'SourceFile' => $uploadConfig['path'] . $newImageName,
30 'ContentType' => 'image/png',
31 'ACL' => 'public-read'
32 ));
33
34 $result['file'] = Config::get('img_url') . $uploadConfig['image_upload_path'].'/'. $newImageName;
35 }