|
| 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