Skip to content

Instantly share code, notes, and snippets.

@cybersholt
Created July 12, 2019 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cybersholt/b5f35e99b4012eee074db99b72b4bde5 to your computer and use it in GitHub Desktop.
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.
<?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