Created
July 12, 2019 02:48
-
-
Save cybersholt/b5f35e99b4012eee074db99b72b4bde5 to your computer and use it in GitHub Desktop.
Ranger like function for s3, get just enough of an image to read the dimensions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function s3Ranger($key) | |
{ | |
// inspired by https://stackoverflow.com/a/4635991/2320760 | |
$s3 = new S3Client( | |
array( | |
'version' => 'latest', | |
'region' => 'us-east-1', | |
'credentials' => [ | |
'key' => $key, | |
'secret' => $secret | |
] | |
) | |
); | |
$s3->registerStreamWrapper(); | |
$result = $s3->headObject( | |
array( | |
'Bucket' => $bucket, | |
'Key' => $key, | |
'Range' => "0-65536" | |
) | |
); | |
if (!($data = file_get_contents("s3://{$bucket}/{$key}"))) { | |
throw new \Exception('Could not read file: [' . $key . ']'); | |
} | |
$im = imagecreatefromstring($data); | |
$width = imagesx($im); | |
$height = imagesy($im); | |
return [$width, $height]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment