Skip to content

Commit ffaf6c4

Browse files
authored
feat(dlp): add redact data from image samples (GoogleCloudPlatform#1851)
1 parent 91ea495 commit ffaf6c4

File tree

5 files changed

+466
-0
lines changed

5 files changed

+466
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2023 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Dlp;
26+
27+
# [START dlp_redact_image_all_infotypes]
28+
use Google\Cloud\Dlp\V2\DlpServiceClient;
29+
use Google\Cloud\Dlp\V2\ByteContentItem;
30+
31+
/**
32+
* Redact sensitive data from an image using default infoTypes.
33+
*
34+
* @param string $callingProjectId The project ID to run the API call under.
35+
* @param string $imagePath The local filepath of the image to inspect.
36+
* @param string $outputPath The local filepath to save the resulting image to.
37+
*/
38+
function redact_image_all_infotypes(
39+
// TODO(developer): Replace sample parameters before running the code.
40+
string $callingProjectId,
41+
string $imagePath = './test/data/test.png',
42+
string $outputPath = './test/data/redact_image_all_infotypes.png'
43+
): void {
44+
// Instantiate a client.
45+
$dlp = new DlpServiceClient();
46+
47+
// Read image file into a buffer.
48+
$imageRef = fopen($imagePath, 'rb');
49+
$imageBytes = fread($imageRef, filesize($imagePath));
50+
fclose($imageRef);
51+
52+
// Get the image's content type.
53+
$typeConstant = (int) array_search(
54+
mime_content_type($imagePath),
55+
[false, 'image/jpeg', 'image/bmp', 'image/png', 'image/svg']
56+
);
57+
58+
// Create the byte-storing object.
59+
$byteContent = (new ByteContentItem())
60+
->setType($typeConstant)
61+
->setData($imageBytes);
62+
63+
$parent = "projects/$callingProjectId/locations/global";
64+
65+
// Run request.
66+
$response = $dlp->redactImage([
67+
'parent' => $parent,
68+
'byteItem' => $byteContent,
69+
]);
70+
71+
// Save result to file.
72+
file_put_contents($outputPath, $response->getRedactedImage());
73+
74+
// Print completion message.
75+
printf('Redacted image saved to %s ' . PHP_EOL, $outputPath);
76+
}
77+
# [END dlp_redact_image_all_infotypes]
78+
79+
// The following 2 lines are only needed to run the samples
80+
require_once __DIR__ . '/../../testing/sample_helpers.php';
81+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

dlp/src/redact_image_all_text.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2023 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Dlp;
26+
27+
# [START dlp_redact_image_all_text]
28+
use Google\Cloud\Dlp\V2\DlpServiceClient;
29+
use Google\Cloud\Dlp\V2\RedactImageRequest\ImageRedactionConfig;
30+
use Google\Cloud\Dlp\V2\ByteContentItem;
31+
32+
/**
33+
* Redact all detected text in an image.
34+
*
35+
* @param string $callingProjectId The project ID to run the API call under.
36+
* @param string $imagePath The local filepath of the image to redact.
37+
* @param string $outputPath The local filepath to save the resulting image to.
38+
*/
39+
function redact_image_all_text(
40+
// TODO(developer): Replace sample parameters before running the code.
41+
string $callingProjectId,
42+
string $imagePath = './test/data/test.png',
43+
string $outputPath = './test/data/redact_image_all_text.png'
44+
): void {
45+
// Instantiate a client.
46+
$dlp = new DlpServiceClient();
47+
48+
// Read image file into a buffer.
49+
$imageRef = fopen($imagePath, 'rb');
50+
$imageBytes = fread($imageRef, filesize($imagePath));
51+
fclose($imageRef);
52+
53+
// Get the image's content type.
54+
$typeConstant = (int) array_search(
55+
mime_content_type($imagePath),
56+
[false, 'image/jpeg', 'image/bmp', 'image/png', 'image/svg']
57+
);
58+
59+
// Create the byte-storing object.
60+
$byteContent = (new ByteContentItem())
61+
->setType($typeConstant)
62+
->setData($imageBytes);
63+
64+
// Enable redaction of all text.
65+
$imageRedactionConfig = (new ImageRedactionConfig())
66+
->setRedactAllText(true);
67+
68+
$parent = "projects/$callingProjectId/locations/global";
69+
70+
// Run request.
71+
$response = $dlp->redactImage([
72+
'parent' => $parent,
73+
'byteItem' => $byteContent,
74+
'imageRedactionConfigs' => [$imageRedactionConfig]
75+
]);
76+
77+
// Save result to file.
78+
file_put_contents($outputPath, $response->getRedactedImage());
79+
80+
// Print completion message.
81+
printf('Redacted image saved to %s' . PHP_EOL, $outputPath);
82+
}
83+
# [END dlp_redact_image_all_text]
84+
85+
// The following 2 lines are only needed to run the samples
86+
require_once __DIR__ . '/../../testing/sample_helpers.php';
87+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2023 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Dlp;
26+
27+
# [START dlp_redact_image_colored_infotypes]
28+
use Google\Cloud\Dlp\V2\DlpServiceClient;
29+
use Google\Cloud\Dlp\V2\RedactImageRequest\ImageRedactionConfig;
30+
use Google\Cloud\Dlp\V2\ByteContentItem;
31+
use Google\Cloud\Dlp\V2\Color;
32+
use Google\Cloud\Dlp\V2\InfoType;
33+
use Google\Cloud\Dlp\V2\InspectConfig;
34+
35+
/**
36+
* Redact data from an image with color-coded infoTypes.
37+
*
38+
* @param string $callingProjectId The project ID to run the API call under.
39+
* @param string $imagePath The local filepath of the image to inspect.
40+
* @param string $outputPath The local filepath to save the resulting image to.
41+
*/
42+
function redact_image_colored_infotypes(
43+
// TODO(developer): Replace sample parameters before running the code.
44+
string $callingProjectId,
45+
string $imagePath = './test/data/test.png',
46+
string $outputPath = './test/data/sensitive-data-image-redacted-color-coding.png'
47+
): void {
48+
// Instantiate a client.
49+
$dlp = new DlpServiceClient();
50+
51+
// Read image file into a buffer.
52+
$imageRef = fopen($imagePath, 'rb');
53+
$imageBytes = fread($imageRef, filesize($imagePath));
54+
fclose($imageRef);
55+
56+
// Get the image's content type.
57+
$typeConstant = (int) array_search(
58+
mime_content_type($imagePath),
59+
[false, 'image/jpeg', 'image/bmp', 'image/png', 'image/svg']
60+
);
61+
62+
// Create the byte-storing object.
63+
$byteContent = (new ByteContentItem())
64+
->setType($typeConstant)
65+
->setData($imageBytes);
66+
67+
// Define the types of information to redact and associate each one with a different color.
68+
$ssnInfotype = (new InfoType())
69+
->setName('US_SOCIAL_SECURITY_NUMBER');
70+
$emailInfotype = (new InfoType())
71+
->setName('EMAIL_ADDRESS');
72+
$phoneInfotype = (new InfoType())
73+
->setName('PHONE_NUMBER');
74+
$infotypes = [$ssnInfotype, $emailInfotype, $phoneInfotype];
75+
76+
$ssnRedactionConfig = (new ImageRedactionConfig())
77+
->setInfoType($ssnInfotype)
78+
->setRedactionColor((new Color())
79+
->setRed(.3)
80+
->setGreen(.1)
81+
->setBlue(.6));
82+
83+
$emailRedactionConfig = (new ImageRedactionConfig())
84+
->setInfoType($emailInfotype)
85+
->setRedactionColor((new Color())
86+
->setRed(.5)
87+
->setGreen(.5)
88+
->setBlue(1));
89+
90+
$phoneRedactionConfig = (new ImageRedactionConfig())
91+
->setInfoType($phoneInfotype)
92+
->setRedactionColor((new Color())
93+
->setRed(1)
94+
->setGreen(0)
95+
->setBlue(.6));
96+
97+
$imageRedactionConfigs = [$ssnRedactionConfig, $emailRedactionConfig, $phoneRedactionConfig];
98+
99+
// Create the configuration object.
100+
$inspectConfig = (new InspectConfig())
101+
->setInfoTypes($infotypes);
102+
$parent = "projects/$callingProjectId/locations/global";
103+
104+
// Run request.
105+
$response = $dlp->redactImage([
106+
'parent' => $parent,
107+
'byteItem' => $byteContent,
108+
'inspectConfig' => $inspectConfig,
109+
'imageRedactionConfigs' => $imageRedactionConfigs
110+
]);
111+
112+
// Save result to file.
113+
file_put_contents($outputPath, $response->getRedactedImage());
114+
115+
// Print completion message.
116+
printf('Redacted image saved to %s ' . PHP_EOL, $outputPath);
117+
}
118+
# [END dlp_redact_image_colored_infotypes]
119+
120+
// The following 2 lines are only needed to run the samples
121+
require_once __DIR__ . '/../../testing/sample_helpers.php';
122+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)