I'm trying to loop through a bunch of images that are stored on an MS SQL database in binary format and moving them to the a bucket on the Amazon cloud using php. I've tried a few methods but with no success, whereas I'm able to grab an instance of the image file in memory, it's this that I can't get uploaded. Having read the following post: Creating an image without storing it as a local file, I thought maybe using the Imagick library would be better but still no luck.
So my question is, can I upload from images read into PHP's memory to the cloud either using the Undesigned S3 class or the actual AWS SDK?
Here's some code that I've created so far to get where I am so to see the approach I've taken. I'm using the Undesigned S3 class for object creation on cloud. The file on the field on the image table is of type 'IMAGE', containing a string reference of the file. Any tips, pointers, help would be great.
// get the images that have not been moved to the cloud yet
$obj_File = new Images();
$aImages = $obj_File->Select_ImagesNotOnCloud();
unset($obj_File);
// set the start time
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
// upload the image to the cloud
$images_processed = 0;
// get and include the Amazon sdk
require_once('../includes/classes/library/plugins/amazon/undesigned/S3.php');
$s3 = new S3("key", "secretkey");
$sBucket = 'testbucket';
// loop through each image and move to the cloud
foreach($aImages as $aImage) {
// save the file to the file server with a temporary name
$oImage = imagecreatefromstring($aImage['FileData']);
// generate random filename
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$sMicrotime = $mtime;
$sFilename = date('Ymdhms') . $sMicrotime;
$sFilename = md5(str_replace(array(' ','.'), '', $sFilename));
// get the correct file extension
$sFilename .= '.' . $aImage['FileExtension'];
// Create Imagick object - the new bit I've just started playing with as per https://stackoverflow.com/questions/189368/creating-an-image-without-storing-it-as-a-local-file/189410#189410
$im = new Imagick();
// Convert image into Imagick
$im->readimageblob($image);
// Upload an object from a resource (requires size):
$result = $s3->putObject($s3::inputResource($im->getimageblob(), $im->getSize()), $sBucket, $sFilename, $s3::ACL_PUBLIC_READ);
// try and put the object into the bucket on the cloud
$response = $s3->putObject(
$s3->inputResource($oImage, $aImage['FileSize']),
$sBucket,
$sFilename,
S3::ACL_PUBLIC_READ
);
echo("<pre>"); var_dump($response); echo("</pre>");
echo('<br /><br />File_Id = ' . $aImage['File_Id'] .', filename generated = '. $sFilename .'<br />');
$images_processed++;
// clear the image object
unset($oImage);
}
// get the end time and calculate the seconds it took to run the script
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
// get final time taken to run script
$sTotalTime = ($endtime - $starttime);
$aTime = explode('.', $sTotalTime);
$sTotalTime = $aTime[0] .'.'. substr($aTime[1], 0, 4);