100 lines
2.7 KiB
PHP
100 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Core;
|
|
|
|
class Medias
|
|
{
|
|
|
|
public static function getImage($model, $conversion = 'normal', $collection = 'images')
|
|
{
|
|
$img = $model->getMedia($collection)->first();
|
|
return $img ? $img->getUrl($conversion) : false;
|
|
}
|
|
|
|
public static function getImages($model)
|
|
{
|
|
if (!$model) {
|
|
return false;
|
|
}
|
|
$model->getMedia();
|
|
foreach ($model->media as $key => $media) {
|
|
$model->media[$key]['url'] = $media->getUrl();
|
|
}
|
|
return $model->media;
|
|
}
|
|
|
|
public static function storeImages($model, $files)
|
|
{
|
|
if ($files) {
|
|
foreach ($files as $file) {
|
|
self::storeImage($model, $file);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function storeImage($model, $file, $collection = 'images')
|
|
{
|
|
return $model->addMedia($file)->sanitizingFileName(function ($fileName) {
|
|
return str_replace(['#', '/', '\\', ' '], '-', $fileName);
|
|
})->toMediaCollection($collection);
|
|
}
|
|
|
|
public static function deleteImages($model, $collection = 'images')
|
|
{
|
|
$ret = $model->clearMediaCollection($collection);
|
|
return true;
|
|
}
|
|
|
|
public static function deleteImage($model, $index)
|
|
{
|
|
$model->getMedia();
|
|
$ret = $model->media[$index]->delete();
|
|
return true;
|
|
}
|
|
|
|
public static function buildURL($image, $conversion = '')
|
|
{
|
|
return self::getPath($image) . self::getConversion($image, $conversion);
|
|
}
|
|
|
|
public static function getPath($image)
|
|
{
|
|
$model = basename(str_replace('\\', '/', $image->model_type));
|
|
return '/storage/' . $model . '/' . $image->collection_name . '/' . $image->id;
|
|
}
|
|
|
|
public static function getConversion($image, $conversion = '')
|
|
{
|
|
// return $conversion ? '/conversions/' . $image->name . '-' . $conversion . self::getExtension($image->file_name) : $image->file_name;
|
|
return $conversion ? '/conversions/' . $image->name . '-' . $conversion . '.jpg' : $image->file_name;
|
|
}
|
|
|
|
public static function getExtension($filename)
|
|
{
|
|
return '.' . pathinfo($filename, PATHINFO_EXTENSION);
|
|
}
|
|
|
|
public static function getThumbSrc($image)
|
|
{
|
|
if (!$image) {
|
|
return null;
|
|
}
|
|
$id = $image['id'];
|
|
$filename = $image['name'] . '-thumb' . self::getExtension($image['file_name']);
|
|
|
|
return "/storage/$id/conversions/$filename";
|
|
}
|
|
|
|
public static function getPreviewSrc($image)
|
|
{
|
|
if (!$image) {
|
|
return null;
|
|
}
|
|
$id = $image['id'];
|
|
$filename = $image['name'] . '-preview' . self::getExtension($image['file_name']);
|
|
|
|
return "/storage/$id/conversions/$filename";
|
|
}
|
|
|
|
}
|