Creating an image without storing it as a local file

前端 未结 8 1005
时光取名叫无心
时光取名叫无心 2020-12-01 08:45

Here\'s my situation - I want to create a resized jpeg image from a user uploaded image, and then send it to S3 for storage, but am looking to avoid writing the resized jpeg

8条回答
  •  爱一瞬间的悲伤
    2020-12-01 09:01

    Once you've got the JPEG in memory (using ImageMagick, GD, or your graphic library of choice), you'll need to upload the object from memory to S3.

    Many PHP S3 classes seem to only support file uploads, but the one at Undesigned seems to do what we're after here -

    // Manipulate image - assume ImageMagick, so $im is image object
    $im = new Imagick();
    // Get image source data
    $im->readimageblob($image_source);
    
    // Upload an object from a resource (requires size):
    $s3->putObject($s3->inputResource($im->getimageblob(), $im->getSize()), 
                      $bucketName, $uploadName, S3::ACL_PUBLIC_READ);
    

    If you're using GD instead, you can use imagecreatefromstring to read an image in from a stream, but I'm not sure whether you can get the size of the resulting object, as required by s3->inputResource above - getimagesize returns the height, width, etc, but not the size of the image resource.

提交回复
热议问题