承接 rachyharkov/codeigniter4-media 相关项目开发

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

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

rachyharkov/codeigniter4-media

Composer 安装命令:

composer require rachyharkov/codeigniter4-media

包简介

Codeigniter 4 library for managing file or media and handling upload behavior

README 文档

README


Banner

CodeIgniter 4 Media Library

Codeigniter package for to handle media upload file task (at least help a bit for my current job). My main goal on this package is codeigniter 4 have a library that be able to handle task such as organize file upload with minimial line of code, also i'm inspired by Laravel Media Library, so i decided to make this package.

Latest Version on Packagist Total Downloads Contributors Forks Stargazers Issues MIT License LinkedIn

Table of Contents

Getting Started

This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.

Prerequisites

  • PHP 7.2+
  • CodeIgniter Framework (4.* recommended)
  • Composer
  • PHP sockets extension enabled

Installation

composer require rachyharkov/codeigniter4-media @dev

Publishing Resource

You need to publish the resources for the default configuration

php spark media:publish

Migrate

then migrate new published table

php spark migrate

Setup Model

just set your model like this

use CodeIgniter\Model;
use Rachyharkov\CodeigniterMedia\HasMedia;
use Rachyharkov\CodeigniterMedia\InteractsWithMedia;

class User extends Model implements HasMedia
{
    use InteractsWithMedia;

    // rest of codes
}

done

How to use?

Store single File

using name attribute of html input to let codeigniter4-media get the file, and add to specified collection (if no name entered, it will be using "default")

$this->user_model->insert($data);
$this->user_model->addMediaFromRequest('photo')->toMediaCollection('profile_photo');

Store single file - store thumbnail

It's useful if you want to store the thumbnail of the file, just add withThumbnail method after addMediaFromRequest method

$this->user_model->insert($data);
$this->user_model->addMediaFromRequest('photo')->withThumbnail()->toMediaCollection('profile_photo');

Store single File - with custom name

only use usingFileName method after addMediaFromRequest method, this will be useful if you want to rename the file before store it to database

$this->user_model->insert($data);
$this->user_model->addMediaFromRequest('photo')->usingFileName('data_'.random(20))->toMediaCollection('profile_photo');

Store Multi File - Different Name

store file from multi different request name (for example, you have 2 input file with different input file name attribute value, and you want to store it to same collection)

$this->user_model->insert($data);
$this->user_model->addMediaWithRequestCollectionMapping([
      'file_input_photo' => 'profile_photo_collection',
      'file_input_profile_cover' => 'profile_photo_collection'
    ])

Store Multi File - Same Name

This time using addMedia, make sure it's an file object (binary payload) to make addMedia accept value, in this example using multiple file input with same name attribute value

$this->product_model->insert($data);
$product_images = $this->request->getFiles();

    foreach ($product_images['photos'] as $key => $p) {
    $this->product_model->addMedia($p)->toMediaCollection('product_image');
    }

Get Single File - Metadata

$user = $this->user_model->where('id', $id)->first();
$user->photo = $this->user_model->mediaOf('profile_photo')->getFirstMedia();

return view('user/edit', $data);

above will return null if no file meta information returned, handle it like this

    <img src="<?= $user->media ? $user->media->file_path.'/'.$user->media->file_name : $user->media ?>" alt="User Photo Profile">

Get Single File - Just URL

This is the example of how to assign new object to existing object (for example user object) with new property (photo) that contains the url of file

$user = $this->user_model->where('id', $id)->first();
$user->photo = $this->user_model->mediaOf('profile_photo')->getFirstMediaUrl();

return view('user/edit', $data);

It's possible to pass parameter with 'thumb' value to get the thumbnail of the file

$media->getFirstMediaUrl('thumb');

Get All file of collection

$user = $this->user_model->where('id', $id)->first();
$user->collection_of_photo_profile = $this->user_model->mediaOf('profile_photo');

    return view('user/edit', $data);

Query result with more data? Just assign it

The second parameter mediaOf() accept id of media who owned by the record

$users  = $this->users_model->findAll();
foreach($users as $key => $value) {
    $users[$key]->photo = $this
                            ->ticket_log_model
                            ->mediaOf('profile_photo', $value->id)
                            ->getFirstMediaUrl();
}

Delete file collection

$this->product_model->where('id', $product->id)->delete();
$this->user_model->clearMediaCollection('profile_photo');

Delete and upload? Of course you can do that

Just like this after you find or update the record

$this->user_model->find($id);

    //or

$this->user_model->update($data, $id);

$this->user_model->clearMediaCollection('profile_photo');
$this->user_model->addMediaFromRequest('photo')->toMediaCollection('profile_photo');

API Mode

create method in your controller just like this, set asTemp to true if you want to return the file metadata (this is useful if you want to show the file right after upload process completed, make sure to return it)

🪄 Backend - Store File

public function api_upload()
{
$user_id = $this->request->getVar('user_id');
    return $this->user_model->addMediaFromRequest('file')->toMediaCollection('profile_photo')->responseJson();
}

you will get this response

{
    status: "success",
    message: "File uploaded successfuly",
    data: {
        collection_name: "profile_photo"
        file_ext: "jpg"
        file_name: "default"
        file_path: "uploads/profile_photo"
        file_size: 62431
        file_type: "image/jpeg"
        model_id: "200090"
        model_type: "App\\Models\\User"
        orig_name: "20211128_165410.jpg"
        unique_name:  "1691502324_94b5e01970c97f5ac670.jpg"
    }
}

🪄 Backend - Delete File

On your controller, create method like this (make sure to return the responseJson method)

    public function api_delete()
    {
        return $this->user_model->clearMediaCollection(request()->getVar('temp_id'), 'profile_photo')->responseJson();
    }

You will get this response

    {
        status: "success",
        message: "File 1691502324_94b5e01970c97f5ac670.jpg deleted successfuly",
    }

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what makes the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Rachmad Nur Hayat - https://rachmad.dev - rachmadnurhayat@gmail.com

Support on Trakteer

love what i made?, please support me on Trakteer

from overseas? you can Paypal me

rachyharkov rachyharkov

rachyharkov/codeigniter4-media 适用场景与选型建议

rachyharkov/codeigniter4-media 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 144 次下载、GitHub Stars 达 9, 最近一次更新时间为 2023 年 08 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「media」 「Media management」 「codeigniter」 「PHP7」 「php-library」 「upload media」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 rachyharkov/codeigniter4-media 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 144
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-02