PHP code example of melsaka / mediafile

1. Go to this page and download the library: Download melsaka/mediafile library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

melsaka / mediafile example snippets


$folder = Folder::whereSlug($request->folder)->firstOrFail();

$mediafile = new MediaFile($folder);

$media = $mediafile->store($request->file('file')); 

$media = $mediafile->update($request, $media);

$mediafile = new MediaFile($folder);

$mediafile->delete($media);

composer 

'providers' => [
    ...
    Melsaka\MediaFile\MediaFileServiceProvider::class,
    ...
];

php artisan vendor:publish --tag=mediafile-config

php artisan vendor:publish --tag=mediafile-assets

php artisan migrate

// publish controllers
php artisan vendor:publish --tag=mediafile-controllers

// publish migrations
php artisan vendor:publish --tag=mediafile-migrations

// publish views
php artisan vendor:publish --tag=mediafile-views

// import the MediaFolder Class
use Melsaka\MediaFile\MediaFolder;

// create a MediaFolder instance
$mediafolder = new MediaFolder();

// and finally create the folder 
$mediafolder->store($request->name, $request->order);

$mediafolder->update($request, $folder);

$mediafolder->delete($folder);

// import the MediaFile Class
use Melsaka\MediaFile\MediaFile;

// select the folder that you wanna upload images into it.
$folder = Folder::whereSlug($request->folder)->firstOrFail();

// create a MediaFile instance
$mediafile = new MediaFile($folder);

// and finally upload/store the iamge 
$mediafile->store($request->file('image'));

$mediafile->update($request, $media);

$mediafile->delete($media);

$imageUrl = 'https://source.unsplash.com/featured/300x201';

$imageFile = $mediafile->createUploadImageFromUrl($imageUrl);

$mediafile->store($imageFile);

namespace App\Livewire;
 
use Livewire\Component;
use Livewire\WithFileUploads;
 
class UploadPhoto extends Component
{
    use WithFileUploads;
 
    public $photo;
 
    public function save()
    {
        $uploadedImage = $mediafile->livewireFile($this->photo);
        
        $mediafile->store($uploadedFile);
    }
}

$mediafile = new MediaFile($folder);

$mediafile->isFolder($media);

use Melsaka\MediaFile\Models\Media;

$media = Media::first();

// User Instance
$media->user;

// Fodler Instance
$media->folder;

// File name without extension
$media->name;

// File extension
$media->extension;

// The encoder used for this image, ex: webp
$media->encoder;

// The type of the file, ex: Image/png
$media->type;

// The uri of the file, ex: storage/images/2023/08/image.png
$media->uri;

// The width of the image, ex: 1920
$media->width;

// The height of the image, ex: 1080
$media->height;

// The alt of the image file
$media->alt;

// The title of the image file
$media->title;

// The caption of the image file
$media->caption;

// returns the original file url
$media->getFileLink();

// returns the full file name, ex: image.png
$media->getFileName();

// returns file size, ex: 128 kb
$media->getFileSize();

// returns true if file is image
$media->isFileAnImage();

// returns the url of the encoded version of the original image file.
$media->getEncodedImageLink();

// returns the sizes and urls of the image thumbnails when $onlySizes is true, 
// when you set $onlySizes to false it will returns the seperator, sizes, and urls. 
// The $name param allows to get the url of a single thumbnail, ex: $media->getThumbnail('s')
$media->getThumbnails($name = null, $onlySizes = true);

// returns the srcset of an image file.
$media->getSrcset();

// returns the sizes of an image file.
$media->getSizes();

use Illuminate\Support\Facades\Route;

use Melsaka\MediaFile\MediaFile;

MediaFile::routes();

MediaFile::routes('media');