<?php

namespace App\Models;

use CodeIgniter\Model;

class UploadModel extends Model
{
    
    public function upload_image($image = NULL)
    {
        if($image)
        {
             $input = $this->validate([
                'image' => 'uploaded[image]|max_size[image,1024]|ext_in[image,jpg,jpeg,png],'
             ]);
             if (!$input) 
             { 
                return false;
             }
             else
             { 
                if($file = $image) 
                {
                    if ($file->isValid() && ! $file->hasMoved()) 
                    {
                        $name = $file->getName();
                        $ext = $file->getClientExtension();
                        $newName = $file->getRandomName(); 
                        $file->move('../public/uploads', $newName);
                        return "/uploads/".$newName;
                    }
                }
            }
        }
        else
        {
            return false;
        }
    }

    public function upload_image_2($image = NULL, $width = 0, $height = 0)
    {
        if($image)
        {
             $input = $this->validate([
                'image' => 'uploaded[file]|max_size[file,1024]|ext_in[image,jpg,jpeg,png, pdf],'
             ]);
             if (!$input) 
             { 
                return false;
             }
             else
             { 
                if($file = $image) 
                {
                    if ($file->isValid() && ! $file->hasMoved()) 
                    {
                        $name = $file->getName();
                        $ext = $file->getClientExtension();
                        $newName = $file->getRandomName(); 
                        $file->move('../public/uploads', $newName);
                        return "/uploads/".$newName;
                    }
                }
            }
        }
        else
        {
            return false;
        }
    }
}
