Created
July 28, 2012 05:56
-
-
Save a10y/3192010 to your computer and use it in GitHub Desktop.
PHP Script that fetches S3 Urls to Binary images from a MySQL DB, does computation and returns URL to resultant S3 file
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
<html> | |
<head><title>Cube Testing</title></head> | |
<body> | |
<?php | |
/** | |
* | |
Author: Andrew Duffy | |
test.php: Take the sum of two floating-point images | |
stored in binary files on S3 whose URLs are to be | |
looked up in a MySQL database, read, summed, and | |
then have that sum written to another binary file, | |
uploaded to S3, and then return the public link | |
to the resultant file. | |
IMPORTANT NOTE: This file MUST be in the same directory as the `sdk.class.php' file, | |
as well as the `lib/' `utilities/' and `services/' directories which the sdk | |
class file depend on. More info on the PHP SDK for AWS can be found at: | |
http://aws.amazon.com/sdkforphp/ | |
*/ | |
require_once 'sdk.class.php'; ##Include the SDK | |
$servername = "NAME OF SERVER HERE"; | |
$user = "NAME OF MYSQL USER "; | |
$pwd = "PASSWORD FOR MYSQL USER"; | |
$db = "NAME OF DATABASE TO USE"; | |
$s3 = new AmazonS3(); | |
$bucket = "NAME OF S3 BUCKET TO STORE RESULTS IN"; | |
echo "<h3>Opening a connection to the database...</h3></br>"; | |
$mysql = new mysqli($servername, $user, $pwd, $db); | |
$res = $mysql->query("SELECT url FROM cubes"); | |
echo "Number of URLs: " . $res->num_rows . "</br>"; | |
$rows = $res->fetch_all(MYSQLI_ASSOC); | |
$urls = array(); #The Array of URLs | |
foreach ($rows as $row){ | |
array_push( $urls, $row["url"] ); #Populate our list of URLs | |
} | |
list($a, $b) = $urls; #Assign the urls | |
echo "<h2>Now we're ready to download!</h2></br>"; | |
echo "<p>Downloading image A</br>"; | |
$text_a = file_get_contents($a); //Fill the file with the image's binary data | |
echo "<p>Downloading image B</br>"; | |
$text_b = file_get_contents($b); //" ^ Ditto ^ " | |
//Open the files for binary writing, and write the data to them | |
$a_file = fopen('./imgA.tmp', 'wb'); | |
$b_file = fopen('./imgB.tmp', 'wb'); | |
fwrite($a_file, $text_a); | |
fwrite($b_file, $text_b); | |
echo "<h1> Generating sum-of-images file...". shell_exec('echo "./add ./imgA.tmp ./imgB.tmp summed.tmp" | bash') . "</h1>"; | |
echo "<h3>Time to Upload to S3...</h3></br>"; | |
//Get ready to upload the output file back up to S3 | |
$filename = "summed.dat"; | |
$s3->create_object($bucket, $filename, array( "fileUpload" => "summed.tmp", "grants" => array("id" => AmazonS3::USERS_ALL, "permission" => AmazonS3::GRANT_READ))); | |
//Get the S3 url of the object we just uploaded | |
$final_url = $s3->get_object_url($bucket, $filename, '5 minutes'); | |
echo "<h2>Task Accomplished! URL to summed image is now <a href=\"" . $final_url . "\">".$final_url."</a></br>"; | |
//Close down the database connection | |
echo "<h3>Closing the database connection...</h3></br>"; | |
$mysql->close(); | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It got a little messy (as PHP scripts tend to be :P )