PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 11, 2022

[FIXED] How to put PHP image resource into Amazon Web Services?

 October 11, 2022     amazon-s3, gd, php, zend-framework     No comments   

Issue

I am currently building a Zend Framework PHP web service that take an image uploaded from Android phone, resize it, and put it into Amazon Web Services S3.

Here are my codes:

$img = $_FILES['image'];

    if(!$img)
    {
        return null;
    }

    if((($img['type'] == 'image/gif') ||
            ($img['type'] == 'image/jpeg') ||
            ($img['type'] == 'image/png')) &&
            ($img['size'] < 1048576))
    {
        if($img['error'] >0)
        {
            throw new Exception("image contain error ");
        }


        $size24 = 24;

        //obtain the auth settings
        $bootstrap = $this->getInvokeArg('bootstrap');
        $awsConfigs = $bootstrap->getOption('aws');

        $s3 = new Zend_Service_Amazon_S3($awsConfigs['appkey'], $awsConfigs['secretkey']);

        $bucketName = 'item';
        $folderName = 'image';

        $perms = array(
                Zend_Service_Amazon_S3::S3_ACL_HEADER =>
                zend_service_amazon_s3::S3_ACL_PUBLIC_READ
        );


        $name =  $bucketName.'/'. $folderName .'/'. uniqid() .'_'. Zend_Date::now()->toString('yyyyMMdd');
        $smallPath = $name . '_32.png';



        //resize and upload 24x24 image
        $srcImg = imagecreatefrompng($img['tmp_name']);
        $tmp = imagecreatetruecolor($size24, $size24);
        list($oriWidth, $oriHeight) = getimagesize($img['tmp_name']);
        imagecopyresampled($tmp, $srcImg, 0, 0, 0, 0, $size24, $size24, $oriWidth, $oriHeight);
        //not working
                    imagepng($tmp, "tmp_32.png")
        $smallret = $s3->putFile("tmp_32.png", $smallPath, $perms);

        imagedestroy($tmp);
        imagedestroy($srcImg);

    }
    else
    {
        throw new Exception("image size/format not qualified.");
    }

I am thinking a way to convert the image resource to stream, so I can use $s3->putFileStream or putObject method, but I can't find a valid way.

Any idea??


Solution

Here is how you'll get your image into a variable without writing to a file:

ob_start();
imagepng($image);
$image_data = ob_get_contents();
ob_end_clean();

If you have the file contents in a variable, you can use putObject. Here's our example where we use file_get_contents. Notice we're getting all the S3 paths out of our Zend config file.

$image_data = file_get_contents(<filename>);
$aws_accesskey = Zend_Registry::get('config')->amazon->accesskey;
$aws_secret = Zend_Registry::get('config')->amazon->secret;
$s3 = new Zend_Service_Amazon_S3($aws_accesskey, $aws_secret);
$image_path = Zend_Registry::get('config')->amazon->s3->assetsbucket . "/images/$filename";
$s3->putObject($image_path, $image_data, array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
    }


Answered By - Jonathan Barlow
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing