使用Yii将调整大小的图像上传到S3

时间:2014-09-22 06:40:53

标签: php yii amazon-s3 upload resize-image

我想使用Yii框架将调整大小的图像上传到Amazon S3存储桶,但要直接执行 - 无需将原始(未调整大小)图像上传到任何文件夹,Yii,网站或服务器结构中的任何位置。

我使用 ThumbsGen 扩展程序来创建图像的缩略图。如果我在自己的服务器上上传文件,代码可以工作。但是,如果我将图像上传到S3,那么它将不会创建缩略图。

我的代码如下所示:

<?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,
        ));
    }
}

结果,我收到了一条PHP警告:

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

getimagesize - 获取图片大小

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

您的代码:

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

$ fullImgSource-&gt; headers ['size'] - 我怀疑它是否存储了图像的文件路径;

试试这个:

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

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

还可以阅读Transferring Files To and From Amazon S3