【问题标题】:Use imagick with S3 AWS将 imagick 与 S3 AWS 一起使用
【发布时间】:2013-11-07 01:41:56
【问题描述】:

这是我使用 AWS S3 的第一步,所以我的目标是在上传到 S3 AWS 之前使用imagick 来处理图像。

我使用此功能调整图像大小和缩放图像(在输出浏览器上完美运行):

function resizeImg($img, $width, $height) {

    $i = new Imagick($img);

    $gig = $i->getImageGeometry();

    // crop the image
    if(($gig['width']/$width) < ($gig['height']/$height)) {

        $i->cropImage($gig['width'], floor($height * $gig['width'] / $width), 0, (($gig['height'] - ($height * $gig['width'] / $width)) / 2));

    } else {

        $i->cropImage(ceil($width * $gig['height'] / $height), $gig['height'], (($gig['width'] - ($width * $gig['height'] / $height)) / 2), 0);
    }

    $i->ThumbnailImage($width, $height, true);

    $i->setImageFormat("jpeg");
    $i->setImageCompressionQuality(90);

    $i->getimageblob(); // I tried it with and without getimageblob

    return $i;
}

这是我的尝试,上传到 S3:

$tmpImg = $_FILES['inputImage']['tmp_name'];

$newImgFile = resizeImg($tmpImg, 100, 100); 

$s3->putObjectFile($newImgFile, BUCKET_NAME, $newfilename, S3::ACL_PUBLIC_READ);

但我收到此错误:

警告:S3::inputFile():无法打开输入文件:���� in.....

我做错了什么?

imagick 与 S3 AWS 不兼容吗?

或者有更好的方法吗?

我的下一个目标是将不同的图像大小调整到 S3,例如 100x100、300x300.... 来自同一个输入文件,但当然我必须在第一个目标之前解决。

额外:

使用这里的解决方案: PHP Imagick: Write Image directly to Amazon S3?

我收到此错误:

警告:S3::putObject(): [SignatureDoesNotMatch] 我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。

如果我这样做:$new = urlencode($new);$new = strtolower($new); ...可以上传但不可读!

据我所知,错误来自那里的多余空格或斜杠

如果它很重要,这里是 S3 中图像的 Url

https->awsCount.xxxx.xxxxx/img.jpg?AWSAccessKeyId=xxxx&Expires=1383787878&Signature=xxxx&x-amz-security-token=AQoDYXdzEGAasAJlJXrt/7FPf84wE9stfgBfEoWaPMDHlubQBlQ6oMY6sNMT4cizkEm9khypHulLB/zJ%2BbqqAErvFBKKs2I9bKDBzrKYKhgRn%2Bta057CZaLougsxHLRGquhd5H26br/Odkq98%2BoDTnfK0LHFa9vYbX6sXDIzCSHcZx4%2B5o0y3cKlxCMsYLqw6wYD1DNjJ%2BHlWWuh%2B6V0FtpbYaErB1XUZfRRZdx3ZPEOvyZxQS7uzP8C3B1nK0wo3uqqSAhn9PPtQt5jrRutYRao2KugxK8TbkZbr/ v5NOYSbpc%2BmI2iYYrUjylqgenzf85Avss0CA1GfOzg%2BMs2/TQ7evH7epr09B8Vyd89Gk1XQpVMyrTSvbzDYE8UCcgrUrXgdHTYWdGLVZ%2BBHzft9nHtNhggePD6AXMuIP%2Ba6ZMF

【问题讨论】:

    标签: php amazon-web-services amazon-s3 imagick


    【解决方案1】:

    您试图从文件中放入 S3 中的对象(putObjectFile 将文件名作为参数),但您将图像 blob 作为参数而不是文件名。

    你应该这样做: 在 resizeImg 中:

    return $i-> getimageblob(); //(this is just an assumption)
    

    对于上传到 S3,类似于以下内容:

    $s3 = new AmazonS3();   
    $s3->batch()->create_object(__BUCKET__,$filename, array(
                    'body' => $object,
                    'acl' => AmazonS3::ACL_PUBLIC,
                    'contentType' => $contentType,
                    'storage' => AmazonS3::STORAGE_REDUCED
                    ));
    $file_upload_response = $s3->batch()->send();
    

    请注意,这是使用 PHP SDK V1(不确定您是否使用 V2)。

    【讨论】:

    • 感谢您的回复。我使用 SDK V2。让我做一些测试,我会告诉你它是如何工作的......
    • 再次感谢您的帮助!我已经回答了我自己的问题! :-)
    【解决方案2】:

    我会给出答案,我会展示每个代码的完整代码:

    if (!empty($_FILES["input_file"])) {    
    
            if ($_FILES["input_file"]["error"] !== UPLOAD_ERR_OK) {
    
                echo "<p>An error occurred.</p>";
                exit;
            }    
    
            // Move/Copy from temporary file to local file
    
            $success = move_uploaded_file($_FILES["input_file"]["tmp_name"],
                    'local_temp_file_directory/' . $_FILES["input_file"]["name"]);
    
            if (!$success) {
    
                echo "<p>Unable to save file.</p>";
                exit;
            }    
        }
    
        // I make a function to get the local file and save the edited file
    
        function resizeImg($img_from_local_file, $width, $height, $pathToSaveImg) {
    
            $i = new Imagick($img_from_local_file);
            $gig = $i->getImageGeometry();
    
            if(($gig['width']/$width) < ($gig['height']/$height)) {
    
                $i->cropImage($gig['width'], floor($height * $gig['width'] / $width), 0, (($gig['height'] - ($height * $gig['width'] / $width)) / 2));
    
            } else {
    
                $i->cropImage(ceil($width * $gig['height'] / $height), $gig['height'], (($gig['width'] - ($width * $gig['height'] / $height)) / 2), 0);
            }
    
            $i->ThumbnailImage($width, $height,true);
            $i->setImageFormat("jpeg");
            $i->setImageCompressionQuality(90);
            $i->writeImage($pathToSaveImg);
            return $i->getimage();
        }
    
        // Call the resizeImg function
    
        resizeImg("local_temp_file_directory/".$_FILES["input_file"]["name"], 40, 40, "local_temp_file_directory/resized_".$_FILES["input_file"]["name"]);
    
        $s3->putObject(
            S3::inputFile("local_temp_file_directory/resized_".$_FILES["input_file"]["name"]),
            BUCKET_NAME, 
            "make_a_new_image_name".date("his").".jpg", 
            S3::ACL_PUBLIC_READ, 
            array(), 
            array("Content-Type"=>'image/jpeg'));
    
     // Remove the temporary local file
     unlink("local_temp_file_directory/".$_FILES["input_file"]["name"]);
     unlink("local_temp_file_directory/resized_".$_FILES["input_file"]["name"]);
    

    就是这样……尽情享受吧!

    【讨论】:

      【解决方案3】:

      你可以这样做:

          $outFile = $this->get('kernel')->getRootDir().'/../web/uploads/imageWithImagick.png';
          $url = "http//......";
          $img = new Imagick();
          $dir = "images/profile";
          $uploader = $this->get('core_storage.uploader');
          $width = 300;
          $height = 500;
          $compression = 90;
          $imageFormat = "jpeg";
          $uploader->uploadImagick($url,$img,$outFile,$width,$height,$dir,$imageFormat,$compression);
      

      和功能:

      public function uploadImagick($url,$img,$outFile,$width,$height,$dir,$imageFormat,$compression)
      {
          $handle = fopen($url, 'rb');
          $img->readImageFile($handle);
          $gig = $img->getImageGeometry();
      
          if(($gig['width']/$width) < ($gig['height']/$height)) {
      
              $img->cropImage($gig['width'], floor($height * $gig['width'] / $width), 0, (($gig['height'] - ($height * $gig['width'] / $width)) / 2));
      
          } else {
      
              $img->cropImage(ceil($width * $gig['height'] / $height), $gig['height'], (($gig['width'] - ($width * $gig['height'] / $height)) / 2), 0);
          }
          $img->thumbnailImage($width,$height);
          $img->setImageFormat($imageFormat);
          $img->setImageCompressionQuality($compression);
          $img->writeImage($outFile);
          // Generate a unique filename based on the date and add file extension of the uploaded file
          $filename = sprintf('%s/%s.%s', $dir, uniqid(), $imageFormat);
          $adapter = $this->filesystem->getAdapter();
          $adapter->write($filename, file_get_contents($outFile));
          unlink($outFile);
          return $filename;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-11-29
        • 1970-01-01
        • 2018-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多