Amazon S3上载PDF并创建封面图像

时间:2013-05-23 14:59:56

标签: php pdf amazon-s3 imagick

我有以下代码将PDF上传到Amazon S3,我需要做的是从PDF的第一页创建一个图像并将其上传到s3。

//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);

//check whether a form was submitted
if($_SERVER['REQUEST_METHOD'] == "POST")
{
    //retreive post variables
    $fileName = $_FILES['file']['name'];
    $fileTempName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];

    $extension=end(explode(".", $fileName));
    $rand = rand(1,100000000);
    $sha1 = sha1($rand);
    $md5 = md5($sha1);
    $fName = substr($md5, 0, 20);       
    $finalName = $fName.'.'.$extension;

    //create a new bucket
    $s3->putBucket("bucket", S3::ACL_PUBLIC_READ);

    //move the file
    if ($s3->putObjectFile($fileTempName, "bucket", 'publications/'.$finalName, S3::ACL_PUBLIC_READ)) {
        $s3file='http://bucket.s3.amazonaws.com/publications/'.$finalName;
        $aS3File = 'publications/'.$finalName;

        $im = new imagick($s3file[0]); 
        // convert to jpg 
        $im->setImageColorspace(255); 
        $im->setCompression(Imagick::COMPRESSION_JPEG); 
        $im->setCompressionQuality(75); 
        $im->setImageFormat('jpeg'); 
        //resize 
        $im->resizeImage(640, 877, imagick::FILTER_LANCZOS, 1);  
        //write image on server (line 54) 
        $s3->putObjectFile("","bucket", 'publications/'.$im->writeImage($fName.'.jpg'), S£::ACL_PUBLIC_READ);

    }else{
        echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
    }

为了安全起见,我已将'实际存储桶名称替换为'存储桶',有人可以告诉我我在这里做错了,因为我收到以下错误:

  

PHP致命错误:带有消息的未捕获异常'ImagickException'   '无法读取文件:h'   /var/www/ams/pub-new-issue.php:45\nStack trace:\ n#0   /var/www/ams/pub-new-issue.php(45):Imagick-&gt; __ construct('h')\ n#1   {main} \ n在第45行的/var/www/ams/pub-new-issue.php中抛出,

感谢

1 个答案:

答案 0 :(得分:1)

$s3file='http://bucket.s3.amazonaws.com/publications/'.$finalName;
$im = new imagick($s3file[0]); 

$s3file是一个字符串,但您正在访问其中的数组索引。因此,您获取第一个字符h。在Imagick实例中只使用$s3file,你应该没问题。