Image Library – intervention/image

Official Site:

http://image.intervention.io/getting_started/installation

composer require intervention/image

Resize and Crop Example:

    private function save_s3($path){
        if(is_s3($path) == true){
            return true;
        }

        //Relative path -> full path
        $file_path = public_path($path);
        $file = File::get($file_path);
        //logg($path);
        $result = Storage::disk('s3')->put($path, $file, 'public');

        //Handle thumb
        $dir = dirname($file_path) . '/thumbs/';
        if(file_exists($dir) == false){
            mkdir($dir);
        }
        $file_name = basename($file_path);
        $thumb = $dir . $file_name;
        if(file_exists($thumb) == false){
            //Make a new thumb file
            $image = Image::make($file_path);

            $width = $image->width();
            $height = $image->height();
            if($width >= $height){
                $image->resize(null, 150, function($constraint){
                    $constraint->aspectRatio();
                });
            }else{
                $image->resize(150, null, function($constraint){
                    $constraint->aspectRatio();
                });
            }
            
            $image->crop(150, 150)->save($thumb);
        }
        $file = File::get($thumb);
        $path = dirname($path) . '/thumbs/' . $file_name;
        $result = Storage::disk('s3')->put($path, $file, 'public');
        //logg($thumb);

        if($result != true){
            return false;
        }else{
            return true;
        }
    }