我正在尝试遍历以二进制格式存储在 MS SQL 数据库中的一堆图像,并使用 php 将它们移动到亚马逊云上的存储桶中。我尝试了几种方法但没有成功,虽然我能够在内存中获取图像文件的实例,但我无法上传。阅读了以下帖子:创建图像而不将其存储为本地文件后,我认为使用 Imagick 库可能会更好,但仍然没有运气。
所以我的问题是,我可以使用未设计的 S3 类或实际的 AWS SDK 从读入 PHP 内存的图像上传到云吗?
这是我到目前为止创建的一些代码,以了解我所采用的方法。我正在使用未设计的 S3 类在云上创建对象。图像表字段上的文件是“图像”类型,包含文件的字符串引用。任何提示,指针,帮助都会很棒。
// 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);