<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Intervention\Image\Image;


class News extends Model
{
    protected $table = 'news';

    protected $fillable = [
        'notification_type_id',
        'title',
        'description',
        'news_date',
        'news_time',
        'photo_url',
        'status',
        'created_at',
        'updated_at'
    ];
    public static $path = '/news/';
    public static function creatNew($uploadedFile, $width, $type='jpg')
    {


        $image = \Intervention\Image\Facades\Image::make($uploadedFile);

        if ($type == '.png') {
            $image->encode('png');
        }

        $fileName = filehelper::getRandomName($type);
        $path = self::$path.$fileName;
        $image->resize($width, null, function ($constraint) {
            $constraint->aspectRatio();
        });
        $image->save(public_path() . '/uploads' . $path);
        return '/uploads' .$path;
    }
}
