承接 yektadg/medialibrary 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yektadg/medialibrary

Composer 安装命令:

composer require yektadg/medialibrary

包简介

simple library for handling medias for projects

README 文档

README

MediaLibrary is a laravel library for handling images in laravel projects . You can upload your images , modify their alt , categorize them into the folders and etc. It also compress your images to 4 different sizes and keeps the original size , so you can access them in client side based on user screen or use it with lazy loading .

Packagist Version License: GPL v3

Screenshots

laravel-media-library

Installation

You can install the package via composer:

 composer require yektadg/medialibrary

Once installed, you should publish the provided assets to create the necessary migration and config files.

 php artisan vendor:publish --provider="YektaDG\Medialibrary\Providers\MediaLibraryServiceProvider" 

Requirements

BootStrap 5

jQuery

Axios Js

Usage

Just add the following line at the end of your laravel blade after all javascript codes .

<x-mediaLibrary::ml-init :id="your prefered id(withoutspace)"></x-mediaLibrary::ml-init>

If you want to just access the media library view for using alongside your customized view you can use the following line .

<x-mediaLibrary::media-library :libraryId="your prefered id(withoutspace)"></x-mediaLibrary::media-library>

If you want to use both on the same page beware of id conflict between them .

Then you must add ml-button class to your html button to open the library .

You can use MediaLibrary for different use types ( you can view all usage under this section ) , so you should specify it when defining button .

Sample of defining MediaLibrary for hidden usage :

<button
        useType="hidden"
        multipleSelect="true"
        useId="image-holder-1"
        class="ml-button"
        type="button">
    Upload Images
</button>
<input id="image-holder-1" type="hidden" name="image-holder" class="image-holder">

In above code after clicking on Upload Images MediaLibrary pops up and after selecting images , they will be stored in the hidden input with html id image-holder-1 .

The three first attributes are MediaLibrary attributes .you can see all of MediaLibrary attributes in the below section .

Attributes

MediaLibrary uses different html attributes on html elements (like buttons,anchor and ...) to handle different situation .

You can see list of attributes below :

Attribute Description
useType Defines the type of element for usage (for now 3 types are supported : hidden , imagePreview , tinymce)
multipleSelect Allows MediaLibrary to select multiple images for use (like when you use tinymce)
useId Refers the html element that holds the images

Note : All attributes and their values are case-sensitive

Features

Limiting Access To Medias

You can limit access to medias by adding accessAllMedia attribute to the Request using laravel middleware.

Here is the example how to do it :

  1. First make a middleware with laravel command php artisan make:middleware CheckAccessMedia
  2. Register your middleware in kernel.php at $routeMiddleware array in Http folder of your project 'CheckMediaAccess' => CheckMediaAccess::class
  3. Then add the alias of your middleware in middleware array in medialibray.php at config folder of your project in this case 'CheckMediaAccess'
  4. Edit your newly created middleware and add your condition for example :
public function handle(Request $request, Closure $next)
   {
       $user = auth()->user();
       $condition = false;
       if ($user && $user->id == 1) {
           $condition = true;
       }
       $request->merge(['accessAllMedia' => $condition]);

       return $next($request);
   }

In above example user with id of 1 can access all of uploaded medias but all other users can only access their medias.

Lazy Loading

MediaLibrary generates images in 4 different size , you can use them for lazy loading . Below code loads images based on user screen size :

/**
 * changes the image tag src based on windwos
 */
$(document).ready(() => {
    let images = [];
    let i = 0;
    $('.resizing').each(function () {
        const sizes = [1280, 1500];
        let src = $(this).attr('originalSrc');
        let name = src.slice(0, src.lastIndexOf('.'))
        let size = getClosest(sizes, $(window).width());
        let ext = src.substring(src.lastIndexOf(".") + 1);
        let path = name + `-${size}x-${ext}`;

        images.push({
            'id': ++i,
            'element': this,
            'url': path,
        })
    })
    if (images.length > 0) {
        imageExist(images)
    }
})


/**
 * check if image exists then put it in src else replace it with original Image
 */
function imageExist(images) {
    const result = images.reduce((obj, cur) => ({...obj, [cur.id]: cur}), {})   //converting to map
    axios.post(imageExistRoute, {
        'images': images
    }).then(res => {
        res.data.forEach(el => {
            let element = $(result[el['id']]['element']);
            if (el['element'] == true) {
                element.attr('src', el['url'])       //  setting the new src
            } else {
                element.attr('src', element.attr('originalSrc'))
            }
        })
    }).catch();
}

/**
 * returns the closest number to the window size
 * @param arr
 * @param target
 * @returns {*}
 */
function getClosest(arr, target) {
    if (arr == null) {
        return
    }
    return arr.reduce((prev, current) => Math.abs(current - target) < Math.abs(prev - target) ? current : prev);
}

Note : you need to define imageExistRoute before above codes;

const imageExistRoute = '{{route('medialibrary.image-exists')}}';

Then you can use lazy loading by adding resizing class and originalSrc attribute to your html image tag .

 <img class="resizing" src="address to your default image"
      originalSrc="address to your real image" alt="">

Note : default image should have low disk size and be a constant image for all pages

Language

You can edit toast.blade.php for changing toast header in resources/views/vendor/yektadg/medialibrary.

For changing toast messages edit mlLang.js in public/vendor/yektadg/medialibrary.

yektadg/medialibrary 适用场景与选型建议

yektadg/medialibrary 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.5k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 yektadg/medialibrary 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yektadg/medialibrary 我们能提供哪些服务?
定制开发 / 二次开发

基于 yektadg/medialibrary 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-01-25