【问题标题】:Upload resized image to S3, using Yii使用 Yii 将调整大小的图像上传到 S3
【发布时间】:2014-11-16 01:26:22
【问题描述】:

我想使用 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

我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    getimagesize — 获取图像的大小

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

    您的代码:

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

    $fullImgSource->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

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 1970-01-01
      • 2023-04-11
      • 2013-03-26
      • 2013-07-12
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多