suman
suman

Reputation: 88

Upload resized image to S3, using Yii

I want to upload a resized image to Amazon S3 bucket, using Yii framework, but to do it directly -- without uploading original (not resized) image to any folder, anywhere within Yii, website or server structure.

I have used ThumbsGen extension to create thumbnail of an image. The code works, if I upload file on my own server. But, if I upload the image to S3, then it will not create a thumbnail.

My code is look like this:

<?php

Yii::app()->setComponents(array('ThumbsGen' => array(

        'class' => 'ext.ThumbsGen.ThumbsGen',
        'thumbWidth' => Yii::t('constants', 'SECRETCODE_WIDTH'),
        'thumbHeight' => Yii::t('constants', 'SECRETCODE_HEIGHT'),
        'baseSourceDir' => Yii::app()->request->s3baseUrl.'/uploads/secretcode/original/',
        'baseDestDir' => Yii::app()->request->s3baseUrl. '/uploads/secretcode/thumbs/',
        'postFixThumbName' => null,
        'nameImages' => array('*'),
        'recreate' => false,
)));

class SecretCodeController extends MyController {

      public function actionCreate() {
        $model = new SecretCode;
        $model->scenario = 'create';

        if (isset($_POST['SecretCode'])) {
            $model->attributes = $_POST['SecretCode'];
            $temp = $model->promo_image = CUploadedFile::getInstance($model, 'promo_image');

            if ($model->validate()) {
                $rnd = rand(1, 1000);
                $ext = $model->promo_image->extensionName;
                $filename = 'secret_' . Yii::app()->user->app_id . '_' . time() . '_' . $rnd . '.' . $ext;                      $success = Yii::app()->s3->upload( $temp->tempName , 'uploads/secretcode/original/'.$filename); 

                if(!$success)
                {
                    $model->addError('promo_image', Yii::t('constants', 'IMAGE_FAILURE'));
                } else {

                    $model->promo_image = $filename; $model->promo_image = $filename;
 $fullImgSource = @Yii::app()->s3->getObject(Yii::app()->params->s3bucket_name,"uploads/secretcode/original/".$filename);

                    list($width, $height, $type, $attr) = getimagesize($fullImgSource->headers['size']);
                    $dimensions = array();

                    $dimensions = CommonFunctions :: SetImageDimensions($width, $height,Yii::t('constants', 'SECRETCODE_WIDTH'), Yii::t('constants', 'SECRETCODE_HEIGHT'));

                    Yii::app()->ThumbsGen->thumbWidth = $dimensions['width'];
                     Yii::app()->ThumbsGen->thumbHeight =  $dimensions['height'];
                     Yii::app()->ThumbsGen->createThumbnails();

                }

                    if ($model->save()) {
                        Yii::app()->user->setFlash('success', Yii::t('constants', 'Secret Code')." ". Yii::t('constants', 'ADD_SUCCESS'));
                        $this->redirect(array('create'));
                    }
                }
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }
}

As a result, I'm getting a PHP warning:

getimagesize(42368) [<a href='function.getimagesize'>function.getimagesize</a>]: failed to open stream: No such file or directory

What am I doing wrong?

Upvotes: 1

Views: 1401

Answers (1)

overals
overals

Reputation: 92

getimagesize — Get the size of an image

array getimagesize ( string $filename [, array &$imageinfo ] )

Your code:

list($width, $height, $type, $attr) = getimagesize($fullImgSource->headers['size']);

$fullImgSource->headers['size'] - I doubt that it stores the file path to image;

try this:

list($width, $height, $type, $attr) = $fullImgSource->headers['size'];

or

list($width, $height, $type, $attr) = getimagesize(/*path to image*/);

Can also read Transferring Files To and From Amazon S3

Upvotes: 1

Related Questions