zxbodya / yii2-gallery-manager

93 stars 61 forks source link

Return internal server Error 500 When upload #43

Closed bahrammzm closed 7 years ago

bahrammzm commented 7 years ago

when i select image for upload ,it return follow error Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:81/en/gallery-image/galleryApi?behaviorName=galleryBehavior&action=ajaxUpload

bahrammzm commented 7 years ago

GalleryImage.php (Model)

public function behaviors()
    {
        return [
             'galleryBehavior' => [
                 'class' => GalleryBehavior::className(),
                 'type' => 'gallery',
                 'extension' => 'jpg',
                 'directory' => Yii::getAlias('@webroot') . '/uploads',
                 'url' => Yii::getAlias('@web') . '/uploads',
                 'versions' => [
                     'small' => function ($img) {
                         /** @var \Imagine\Image\ImageInterface $img */
                         return $img
                             ->copy()
                             ->thumbnail(new \Imagine\Image\Box(200, 200));
                     },
                     'medium' => function ($img) {
                         /** @var Imagine\Image\ImageInterface $img */
                         $dstSize = $img->getSize();
                         $maxWidth = 800;
                         if ($dstSize->getWidth() > $maxWidth) {
                             $dstSize = $dstSize->widen($maxWidth);
                         }
                         return $img
                             ->copy()
                             ->resize($dstSize);
                     },
                 ]
             ]
        ];
    }

GalleryImageController.php

 public function actions()
    {
        return [
           'galleryApi' => [
               'class' => GalleryManagerAction::className(),
               'types' => [
                   'gallery' => GalleryImage::className()
               ]
           ],
        ];
    }    

index.php (view)

<?php
    echo GalleryManager::widget(
            [
                'model' => $model,
                'behaviorName' => 'galleryBehavior',
                'apiRoute' => 'gallery-image/galleryApi'
            ]
        );

    foreach($model->getBehavior('galleryBehavior')->getImages() as $image) {
        echo Html::img($image->getUrl('medium'));
    }
    ?>
zxbodya commented 7 years ago

check upload limitations in your php and webserver config:

php - upload_max_filesize, post_max_size webserver - client_max_body_size (for nginx)

also issue can be because of memory_limit in php (image processing can require a lot of ram)

zxbodya commented 7 years ago

what is size of image you are trying to upload? (both file size and dimensions)

bahrammzm commented 7 years ago

i checked these options inside php.ini file memory_limit = 128M upload_max_filesize = 64M post_max_size = 3M

still issue is remaining ! also i inserted a row in mysql and an image inside uploads folder with same name , But gallery manager does not show any image !

zxbodya commented 7 years ago

Can you show content of error response you are getting from server?

bahrammzm commented 7 years ago

my fille is less than 1mb . as i said internal server error 500 in AjaxUpload

capture

zxbodya commented 7 years ago

open response details in network tab

bahrammzm commented 7 years ago

capture

zxbodya commented 7 years ago

select failed request and look to response content there…

bahrammzm commented 7 years ago

it return PHP Fatal Error Call to a member function getBehavior() on a non-object

bahrammzm commented 7 years ago

For use this package i have to login as a User to my site Or it's possible as Guest user??

bahrammzm commented 7 years ago

i found out that the issue occur when i want insert new row in table ! whereas in update it work properly . now how can i handle it for insert new row in table ?

ivanuz commented 7 years ago

I have the same problem Call to a member function getBehavior() on null chrome_2017-05-01_03-19-05

zxbodya commented 7 years ago

i found out that the issue occur when i want insert new row in table ! whereas in update it work properly . now how can i handle it for insert new row in table ?

you mean uploading gallery photo to before creating db record for gallery it would belong to? that is not possible with current implementation.

I have the same problem Call to a member function getBehavior() on null

I do not see galleryId in request url, but it should be there… Can you upload your code to make it easier to pinpoint what is the reason?

ivanuz commented 7 years ago

I do not see galleryId in request url, but it should be there… Can you upload your code to make it easier to pinpoint what is the reason?

Model:

`use zxbodya\yii2\galleryManager\GalleryBehavior; class Gallerys extends \yii\db\ActiveRecord {

public static function tableName()
{
    return 'gallery_image';
}

public function behaviors()
{
    return [
        'galleryBehavior' => [
            'class' => GalleryBehavior::className(),
            'type' => 'gallerys',
            'extension' => 'jpg',
            'directory' => Yii::getAlias('@webroot') . '/im/gallery',
            'url' => Yii::getAlias('@web') . '/im/gallery',
            'versions' => [
                'small' => function ($img) {
                    /** @var \Imagine\Image\ImageInterface $img */
                    return $img
                        ->copy()
                        ->thumbnail(new \Imagine\Image\Box(200, 200));
                },
                'medium' => function ($img) {
                    /** @var Imagine\Image\ImageInterface $img */
                    $dstSize = $img->getSize();
                    $maxWidth = 800;
                    if ($dstSize->getWidth() > $maxWidth) {
                        $dstSize = $dstSize->widen($maxWidth);
                    }
                    return $img
                        ->copy()
                        ->resize($dstSize);
                },
            ]
        ]
    ];
}
public function rules()
{
    return [
        [['ownerId'], 'required'],
        [['rank'], 'integer'],
        [['description'], 'string'],
        [['type', 'ownerId', 'name'], 'string', 'max' => 255],
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'type' => 'Тип',
        'ownerId' => 'ID Владельца',
        'rank' => 'Позиция',
        'name' => 'Название',
        'description' => 'Описание',
    ];
}

}`

Controller: `namespace app\modules\backend\controllers;

use app\modules\backend\models\Gallerys; use Yii; use yii\web\Controller; use yii\web\NotFoundHttpException; use zxbodya\yii2\galleryManager\GalleryManagerAction;

class GallerysController extends Controller {

public function actions()
{
    return [
        'galleryApi' => [
            'class' => GalleryManagerAction::className(),
            // mappings between type names and model classes (should be the same as in behaviour)
            'types' => [
                'gallerys' => Gallerys::className()
            ]
        ],
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
    ];
}

public function actionIndex() {

    $model = new Gallerys();

    return $this->render('index', [
        'model' => $model,
    ]);
}`

View: `<?php

use yii\helpers\Html; use zxbodya\yii2\galleryManager\GalleryManager; ?>

`
zxbodya commented 7 years ago

$model = new Gallerys();

you can not upload images to new gallery. it should be saved before you can show widget.

ivanuz commented 7 years ago

you can not upload images to new gallery. it should be saved before you can show widget.

Can you please show an example of action to render view with existing record? And how can i save record if it's empty before view render?

zxbodya commented 7 years ago

@ivanuz what do you mean? it is in readme - just load existing model, and then pass it into the view…

ivanuz commented 7 years ago

Thanks a lot! Just created an empty record and it works!

$model = Gallerys::findOne('1');

        return $this->render('index', [
            'model' => $model,
        ]);