<?php
namespace App\Http\Services;

use Storage;
use Image;
use File;
use App\Settings;

Class Uploader
{
    public static function SaveImage($image, $name, $path, $prefix='', $encode='jpg')
    {
        $compression = Settings::where('setting_name', 'image-compression')->first();
        $compression = $compression->setting_value && ($compression->setting_value >= 0) && ($compression->setting_value <= 100) ? $compression->setting_value : 80;

        # Get uploaded image and compress into jpg
        $jpg = Image::make($image)->encode($encode, $compression);
        # Make a unique filename
        $imageName = $name.'__'.str_slug($prefix).'__'.uniqid().'__'.$compression.'.'.$encode;
        # Define the path
        $storage_path = storage_path('app/public/'.$path);
        # Create directory if not exists
        File::makeDirectory($storage_path, 0775, true, true);
        # Save image
        $jpg->save($storage_path.$imageName);

        #return filename
        return $imageName;

    }

    public static function SaveImageWithThumb($image, $name, $path, $prefix='', $encode='jpg')
    {
        $compression = Settings::where('setting_name', 'image-compression')->first();
        $compression = $compression->setting_value && ($compression->setting_value >= 0) && ($compression->setting_value <= 100) ? $compression->setting_value : 80;

        # Get uploaded image and compress into jpg
        $jpg = Image::make($image)->encode($encode, $compression);
        # Make a unique filename
        $imageName = $name.'__'.str_slug($prefix).'__'.uniqid().'__'.$compression.'.'.$encode;
        # Define the path
        $storage_path = storage_path('app/public/'.$path);
        # Create directory if not exists
        File::makeDirectory($storage_path, 0775, true, true);
        # Save image
        $jpg->save($storage_path.'big_'.$imageName);

        Image::make($image)->encode($encode, $compression)
          ->resize(null, 450, function ($constraint) {
            $constraint->aspectRatio();
          })->save($storage_path.$imageName);

        #return filename
        return [
          'thumb' => $imageName,
          'original' => 'big_'.$imageName
        ];

    }


    public static function SaveGallery($image, $album, $path)
    {
        $compression = Settings::where('setting_name', 'image-compression')->first();
        $compression = $compression->setting_value && ($compression->setting_value >= 0) && ($compression->setting_value <= 100) ? $compression->setting_value : 80;

        # Make unique filename
        $originalname = pathinfo($image->getClientOriginalName(), PATHINFO_FILENAME);
        $unique = str_slug($originalname).'-'.uniqid();
        $ext = $image->getClientOriginalExtension();

        $filename = $unique.'.'.$ext;
        $thumbnailName = $unique.'_tn.'.$ext;

        # Compress image
        $img = Image::make($image)->encode($ext, $compression);

        # Define save path
        $folder = storage_path('app/public/'.$path.$album.'/');

        # Create folder if not exists
        File::makeDirectory($folder, 0775, true, true);

        # Save image
        $img->save($folder.$filename);

        # Save thumbnail
        Image::make($img->resize(null, 200, function ($constraint) {
            $constraint->aspectRatio();
        })->save($folder.$thumbnailName));

        # Return file url
        return $filename;

    }
}


?>
