Skip to content

Commit

Permalink
STD-1 prep for v2, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
2lives committed Apr 17, 2024
1 parent 6c431ed commit 5a68d12
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 49 deletions.
45 changes: 40 additions & 5 deletions ImageUtil.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
<?php
use Jcupitt\Vips;

// function getResolutionFromExif(file)
// {
function validateFile($photoPath) {
try {
$imageInfo = getimagesize($photoPath);
$width = $imageInfo[0];
$height = $imageInfo[1];
$size = filesize($photoPath);

// }
return (
$size > 27 * 1024 * 1024 || // If size is greater than 27 MB
$width > 6400 || // If width is greater than 6400 pixels
$height > 6400 || // If height is greater than 6400 pixels
$width * $height > 27000000 // If total pixels greater than 27 million
);
} catch (Exception $err) {
return true;
}
}

function attemptImageConversion($file, $conversion_type, $strip_metadata = false)
// TODO - FINISH
{
$convertedImage = null;
switch ($conversion_type) {
case "webp":
echo 'CONVERTING';
$convertedImage = $file->writeToBuffer('.webp', ['Q' => 97]);
break;
case "jpg":
$convertedImage = $file->writeToBuffer('.jpg', ['Q' => 97]);
break;
case "png":
if ($strip_metadata) {
$fields = $file->getFields();

foreach( $fields as $key => $value ) {
try {
echo "removing key..." . $value;
$file->remove($value);
}
catch ( \Exception $e ) {
echo ''. $e->getMessage() .'';
}
}

echo "setting converted image";
$convertedImage = $file->writeToBuffer('.png', ['Q' => 97]);
}
else {
$convertedImage = $file->writeToBuffer('.png', ['Q' => 97]);
}
break;
}

return $convertedImage;
Expand Down
10 changes: 5 additions & 5 deletions Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public function deleteJob($id)
return $this->SkylabStudio->makeRequest("DELETE", $url, $options);
}

public function queueJob($id, $data)
public function queueJob($id, $data = null)
{
$url = $this->SkylabStudio->_buildUrl("jobs", $id, "queue");
$options = $this->SkylabStudio->_buildHeaders();
$url = $this->SkylabSDKUtil->_buildUrl("jobs", $id, "queue");
$options = $this->SkylabSDKUtil->_buildHeaders();

$jsonData = json_encode($data);
$options['body'] = $jsonData;
Expand All @@ -86,8 +86,8 @@ public function queueJob($id, $data)

public function cancelJob($id)
{
$url = $this->SkylabStudio->_buildUrl("jobs", $id, "cancel");
$options = $this->SkylabStudio->_buildHeaders();
$url = $this->SkylabSDKUtil->_buildUrl("jobs", $id, "cancel");
$options = $this->SkylabSDKUtil->_buildHeaders();

return $this->SkylabStudio->makeRequest("POST", $url, $options);
}
Expand Down
33 changes: 18 additions & 15 deletions Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,32 @@ public function createPhoto($data)
private function _uploadPhoto($photoPath, $id, $model = 'job')
{
$client = new GuzzleHttp\Client();
echo 'uploading photo: ~~~~~ ' . $photoPath . ', ' . $id;


$ext = pathinfo($photoPath, PATHINFO_EXTENSION);
$uploadOptions = [];

if (!in_array(strtolower($ext), self::validExtensions)) {
throw new Exception("Invalid file type: must be of type jpg/jpeg/png/webp");
}

// TODO
// $valid = $this->validateFile($photoPath);
$valid = true;
if (!$valid) {
throw new Exception("Invalid file size: must be within 6400x6400, and no larger than 27MB");
if ($model == 'job') {
$invalid = validateFile($photoPath);

if ($invalid) {
throw new Exception("Invalid file size: must be within 6400x6400, and no larger than 27MB");
}

// photo valid, set upload options with tags
$job = $this->SkylabStudio->getJob($id);
if ($job->type == "regular") {
$uploadOptions['headers']["X-Amz-Tagging"] = "job=photo&api=true";
}
}

$response = [];

$photoName = basename($photoPath);

$uploadOptions = [];
$file = Vips\Image::newFromFile($photoPath);

if ($ext === "png") {
Expand All @@ -74,13 +79,6 @@ private function _uploadPhoto($photoPath, $id, $model = 'job')
"use_cache_upload" => false
];

if ($model == "job") {
$job = $this->SkylabStudio->getJob($id);
if ($job->type == "regular") {
$uploadOptions['headers']["X-Amz-Tagging"] = "job=photo&api=true";
}
}

// Create Studio photo record
$photoResp = $this->createPhoto($photoData);

Expand Down Expand Up @@ -155,6 +153,11 @@ public function uploadJobPhoto($photoPath, $id)
return $this->_uploadPhoto($photoPath, $id);
}

// public function uploadProfilePhoto($photoPath, $id)
// {
// return $this->_uploadPhoto($photoPath, $id, 'profile');
// }

public function deletePhoto($id)
{
$url = $this->SkylabSDKUtil->_buildUrl("photos", $id);
Expand Down
15 changes: 0 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,6 @@ $api.uploadJobPhoto($photoPath, $jobId);

If upload fails, the photo object is deleted for you. If upload succeeds and you later decide you no longer want to include that image, use delete_photo to remove it.

#### Upload Profile Photo

This function handles validating a background photo for a profile. Note: `enable_extract` and `replace_background` (profile attributes) **MUST** be true in order to create background photos. Follows the same upload process as uploadJobPhoto.

```php
$api.uploadProfilePhoto($photoPath, $profileId);
```

`Returns: { photo: { photoObject }, uploadResponse: bucketUploadResponseStatus }`

If upload fails, the photo object is deleted for you. If upload succeeds and you later decide you no longer want to include that image, use delete_photo to remove it.

### Get a Photo

```php
Expand All @@ -200,9 +188,6 @@ $api.deletePhoto($photoId);
- requestTimestamp: Timestamp header received in callback under 'X-Skylab-Timestamp'
- signature: Signature header received in callback under 'X-Skylab-Signature'

// TODO - NOT RELEVANT
**NOTE:** If using something like an express server to handle the callback, the JSON response needs to be the raw response. If your express server is running `app.use(express.json)` you will need to create a middleware and pass it to your callback handler to use the raw response: `app.use(express.raw({ type: 'application/json' }))`

```php
$api.validateHmacHeaders($secretKey, $jobJson, $requestTimestamp, $signature);
```
Expand Down
2 changes: 0 additions & 2 deletions SkylabStudio.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ public function _debug($str) {
public function _buildHeaders() {
$headers = ['Content-Type' => 'application/json'];
$headers['X-SLT-API-KEY'] = $this->API_KEY;
// TODO FIX API_CLIENT BEING NULL
$headers['X-SLT-API-CLIENT'] = $this->API_CLIENT;

// TODO DOES THIS DO ANYTHING ATM??
$this->_debug('Set headers: ' . json_encode($headers));

return ['headers' => $headers];
Expand Down
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{
"name": "skylab/studio",
"authors": [
{
"name": "SkylabTech",
"email": "[email protected]",
"homepage": "https://skylabtech.ai",
"role": "Developer"
}
],
"version": "0.0.1",
"author": "SkylabTech <[email protected]>",
"description": "studio.skylabtech.ai PHP client",
"require": {
"guzzlehttp/guzzle": "^7.0",
"jcupitt/vips": "2.3.0"
},
"require-dev": {
"phpunit/phpunit": "^11.0"
},
"authors": [
{
"name": "skylabtech"
}
]
}
}

0 comments on commit 5a68d12

Please sign in to comment.