Last active
December 29, 2015 07:58
-
-
Save motin/7639508 to your computer and use it in GitHub Desktop.
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 | |
class Photo extends CActiveRecord { | |
/** | |
* | |
* Save a file into multiple formats on S3 | |
* @param unknown_type $filename | |
* @param unknown_type $id | |
* @param unknown_type $type | |
*/ | |
private function saveFileToS3($filename, $id, $type) | |
{ | |
//$formats = array(array(self::FORMAT_DEFAULT)); | |
if ($type == self::TYPEPROFILE) | |
{ | |
$formats[] = array(self::FORMAT_PROFILE_25, 25, 25); | |
$formats[] = array(self::FORMAT_PROFILE_50, 50, 50); | |
} | |
foreach ($formats as $one) | |
{ | |
// Should be resampled | |
if (isset($one[1])) | |
{ | |
$fname_local = $filename . "_" . $one[1] . "x" . $one[2]; | |
$type = self::imageCopyResample($filename, $fname_local, $one[1], $one[2]); | |
if ($type === false) | |
throw new Exception("Could not resample image: " . $fname_local); | |
Yii::trace('saveFileToS3 image resampled: ' . $fname_local, 'Photo'); | |
} | |
// Should not be resampled | |
else | |
{ | |
$fname_local = $filename; | |
list($width, $height, $type) = getimagesize($filename); | |
} | |
// Create file name | |
$fname_s3 = self::getFileName($this->id, $one[0]); | |
// Upload file to S3 | |
U::loadAWS(); | |
$s3 = new AmazonS3(); | |
$response = $s3->create_object(TR_AWS_BUCKET, $fname_s3, array( | |
'fileUpload' => $fname_local, | |
'acl' => AmazonS3::ACL_PUBLIC, | |
'contentType' => image_type_to_mime_type($type), | |
'headers' => array(// raw headers | |
'Cache-Control' => 'max-age', | |
'Expires' => gmdate("r", mktime(0, 0, 0, date("m"), date("d"), date("Y") + 1)), | |
) | |
)); | |
if ($fname_local != $filename) | |
unlink($fname_local); | |
if (!$response->isOK()) | |
throw new Exception("Could not upload file to S3: " . $fname_s3); | |
Yii::trace('saveFileToS3 image uploaded to S3: ' . $fname_s3, 'Photo'); | |
} | |
return count($formats); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment