0

这些是我使用 AWS S3 的第一步,因此我的目标是imagick在上传到 S3 AWS 之前用于操作图像。

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

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:将图像直接写入 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

4

3 回答 3

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)。

于 2013-11-08T11:15:38.360 回答
0

你可以这样做:

    $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;
}
于 2014-10-28T09:01:59.930 回答
0

我会得到答案,我会为每个人展示整个代码:

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"]);

就是这样......享受它!

于 2013-11-13T00:20:10.060 回答