Created
February 23, 2016 10:43
-
-
Save sharifbdp/9d370c634a241c3998c6 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
/** | |
* File Upload into Amazon S3 | |
* @param string $input_name | |
* @param int $restaurant_id | |
* @return string/bolean | |
*/ | |
public function _fileUpload($input_name, $restaurant_id) { | |
if (isset($_FILES[$input_name]) && ($_FILES[$input_name]) != null) { | |
$file_details = $_FILES[$input_name]['name']; | |
// current time and date | |
$filename = explode(".", $file_details); | |
$picname = $filename[0]; | |
$file_type = $filename[1]; | |
$rand_string = random_string('alnum', 8); | |
// AMAZON | |
$file_new_name = date("YmdHis") . $restaurant_id . $rand_string . "." . $file_type; | |
// THIS PART BY RIDDHIMAN ADIB | |
require_once './aws/aws-autoloader.php'; | |
// Instantiate an Amazon S3 client. | |
$s3 = new Aws\S3\S3Client([ | |
'version' => 'latest', | |
'region' => 'ap-southeast-1', | |
'credentials' => [ | |
'key' => 'KEY', | |
'secret' => 'SECRET', | |
], | |
]); | |
// | |
// Load the image where the logo will be embeded into | |
$image = imagecreatefromjpeg($_FILES[$input_name]["tmp_name"]); | |
// Load the logo image | |
$logoImage = imagecreatefrompng('https://s3-ap-southeast-1.amazonaws.com/harriken/restaurant-pic/2016/02/20160223150236101RzOq2BAM.png'); | |
imagealphablending($logoImage, true); | |
// Get dimensions | |
$imageWidth = imagesx($image); | |
$imageHeight = imagesy($image); | |
$logoWidth = imagesx($logoImage); | |
$logoHeight = imagesy($logoImage); | |
// Paste the logo | |
imagecopy( | |
// destination | |
$image, | |
// source | |
$logoImage, | |
// destination x and y | |
($imageWidth - $logoWidth) / 2, ($imageHeight - $logoHeight) / 2, | |
// source x and y | |
0, 0, | |
// width and height of the area of the source to copy | |
$logoWidth, $logoHeight); | |
// | |
try { | |
$result = $s3->putObject([ | |
'Bucket' => $this->bucket_name, | |
'Key' => $this->upload_dir . date("Y") . '/' . date("m") . '/' . $file_new_name, | |
'Body' => $this->image_data($image), //'Body' => fopen('/path/to/file', 'r'), | |
'ACL' => 'public-read' | |
]); | |
} catch (Aws\Exception\S3Exception $e) { | |
echo "There was an error uploading the file.\n"; | |
} | |
// Release memory | |
imageDestroy($image); | |
imageDestroy($logoImage); | |
if ($result['ObjectURL']) { | |
return $file_new_name; | |
} else { | |
return NULL; | |
} | |
// | |
// | |
/* | |
$response = $s3->deleteObject(array( | |
'Bucket' => $this->bucket_name, | |
'Key' => 'restaurant-pic/2016/02/20160223131538100OjJ4j25N.jpg' | |
)); | |
var_dump($response); exit(); | |
*/ | |
} | |
} | |
function image_data($gdimage) { | |
ob_start(); | |
imagejpeg($gdimage); | |
return(ob_get_clean()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment