How to replace PHP imagecopyresampled with Amazon

2020-07-24 16:32发布

问题:

I'm using the jquery file upload script and have had success uploading the main image to Amazon S3. I'm now trying to swap out the multiple image sizes for uploads to sub folders or objects in S3.

Inside the create_scaled_image function in the UploadHandler.php file how do I replace the imagecopyresampled call with an S3 call? Is this even possible?

Here is what does the image resizing and writes the file out:

 $success = $src_img && @imagecopyresampled(
        $new_img,
        $src_img,
        $dst_x,
        $dst_y,
        0,
        0,
        $new_width,
        $new_height,
        $img_width,
        $img_height
    ) && $write_image($new_img, $new_file_path, $image_quality);
    // Free up memory (imagedestroy does not delete files):
    @imagedestroy($src_img);
    @imagedestroy($new_img);

Can I somehow resize the image in memory and then save the file to my S3 object?

 $bucket = "pics.mysite.com";

 $file = ??
 $uploaded_file = ??
 $response = $s3->create_object($bucket, $file, array('fileUpload' => $uploaded_file, 'acl' => AmazonS3::ACL_PUBLIC));
 if ($response->isOK()) {
     //success
 }

回答1:

You can capture the output of imagejpeg() and the related save functions to produce an in-memory representation of the image file's contents. I don't know if that s3 library allows you to upload a string, rather than just pointing it at a file, but it'd go something like this:

... image operations
ob_start();
imagejpeg($img); // out $img gd handle as a .jpg file
$jpg = ob_get_clean();

$s3->upload(.... 'filedata' => $jpg);
                 ^^^^^^^^^^---whatever it'be in the S3 lib to send from a string