2018-02-08 18:53:52 +00:00
|
|
|
<?php
|
|
|
|
|
2022-03-10 11:54:29 +00:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $bytes
|
|
|
|
* @param bool $is_drive_size
|
|
|
|
* @param string $beforeunit
|
|
|
|
* @param string $afterunit
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function format_bytes($bytes, bool $is_drive_size = true, string $beforeunit = '', string $afterunit = ''): string
|
2018-02-08 18:53:52 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
$btype = ($is_drive_size === true) ? 1000 : 1024;
|
|
|
|
$labels = ['B', 'KB', 'MB', 'GB', 'TB'];
|
2022-12-11 10:58:58 +00:00
|
|
|
// use 1000 rather than 1024 to simulate HD size not real size
|
2023-06-06 10:08:47 +00:00
|
|
|
for ($x = 0; $bytes >= $btype && $x < (count($labels) - 1); $bytes /= $btype, $x++) ;
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($labels[$x] == 'TB') {
|
2023-06-06 10:08:47 +00:00
|
|
|
return round($bytes, 3) . $beforeunit . $labels[$x] . $afterunit;
|
2022-03-19 13:54:32 +00:00
|
|
|
} elseif ($labels[$x] == 'GB') {
|
2023-06-06 10:08:47 +00:00
|
|
|
return round($bytes, 2) . $beforeunit . $labels[$x] . $afterunit;
|
2022-03-19 13:54:32 +00:00
|
|
|
} elseif ($labels[$x] == 'MB') {
|
2023-06-06 10:08:47 +00:00
|
|
|
return round($bytes, 2) . $beforeunit . $labels[$x] . $afterunit;
|
2022-03-19 13:54:32 +00:00
|
|
|
} else {
|
2023-06-06 10:08:47 +00:00
|
|
|
return round($bytes, 0) . $beforeunit . $labels[$x] . $afterunit;
|
2022-03-19 13:54:32 +00:00
|
|
|
}
|
2018-02-08 18:53:52 +00:00
|
|
|
}
|
2018-06-13 19:03:26 +00:00
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $title
|
|
|
|
* @param string $separator
|
|
|
|
* @param string $language
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function str_slug($title, string $separator = '-', string $language = 'en'): string
|
2022-03-10 11:54:29 +00:00
|
|
|
{
|
|
|
|
return Str::slug($title, $separator, $language);
|
|
|
|
}
|
|
|
|
|
2023-06-06 10:08:47 +00:00
|
|
|
if (!function_exists('str_is')) {
|
2022-03-10 11:54:29 +00:00
|
|
|
/**
|
|
|
|
* Determine if a given string matches a given pattern.
|
|
|
|
*
|
2023-06-06 10:08:47 +00:00
|
|
|
* @param string|array $pattern
|
2022-11-25 00:35:56 +00:00
|
|
|
* @param string $value
|
2022-03-10 11:54:29 +00:00
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @deprecated Str::is() should be used directly instead. Will be removed in Laravel 6.0.
|
|
|
|
*/
|
2022-11-25 00:35:56 +00:00
|
|
|
function str_is($pattern, string $value): bool
|
2022-03-10 11:54:29 +00:00
|
|
|
{
|
|
|
|
return Str::is($pattern, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $hex
|
|
|
|
* @return float|int
|
|
|
|
*/
|
2022-03-19 13:54:32 +00:00
|
|
|
function get_brightness($hex)
|
|
|
|
{
|
2018-06-13 19:03:26 +00:00
|
|
|
// returns brightness value from 0 to 255
|
|
|
|
// strip off any leading #
|
2022-03-21 21:37:14 +00:00
|
|
|
// $hex = str_replace('#', '', $hex);
|
|
|
|
$hex = preg_replace("/[^0-9A-Fa-f]/", '', $hex);
|
2022-03-19 13:54:32 +00:00
|
|
|
if (strlen($hex) == 3) {
|
2023-06-06 10:08:47 +00:00
|
|
|
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
|
2018-06-13 19:03:26 +00:00
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-06-13 19:03:26 +00:00
|
|
|
$c_r = hexdec(substr($hex, 0, 2));
|
|
|
|
$c_g = hexdec(substr($hex, 2, 2));
|
|
|
|
$c_b = hexdec(substr($hex, 4, 2));
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-06-13 19:03:26 +00:00
|
|
|
return (($c_r * 299) + ($c_g * 587) + ($c_b * 114)) / 1000;
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $hex
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function title_color($hex): string
|
2018-06-13 19:03:26 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
if (get_brightness($hex) > 130) {
|
2018-06-13 19:03:26 +00:00
|
|
|
return ' black';
|
|
|
|
} else {
|
|
|
|
return ' white';
|
|
|
|
}
|
2018-10-29 15:12:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getLinkTargetAttribute(): string
|
2019-06-18 11:25:05 +00:00
|
|
|
{
|
|
|
|
$target = \App\Setting::fetch('window_target');
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($target === 'current') {
|
2019-06-18 11:25:05 +00:00
|
|
|
return '';
|
|
|
|
} else {
|
2023-06-06 10:08:47 +00:00
|
|
|
return ' target="' . $target . '"';
|
2019-06-18 11:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @return array|string|string[]|null
|
|
|
|
*/
|
2018-10-29 15:12:47 +00:00
|
|
|
function className($name)
|
|
|
|
{
|
2022-11-25 00:35:56 +00:00
|
|
|
return preg_replace('/[^\p{L}\p{N}]/u', '', $name);
|
2018-10-29 15:12:47 +00:00
|
|
|
}
|
2023-06-05 16:27:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file
|
2023-06-06 10:08:47 +00:00
|
|
|
* @param string $extension
|
2023-06-05 16:27:30 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-06-06 10:08:47 +00:00
|
|
|
function isImage(string $file, string $extension): bool
|
2023-06-05 16:27:30 +00:00
|
|
|
{
|
2023-06-06 10:08:47 +00:00
|
|
|
$allowedExtensions = ['jpg', 'jpeg', 'png', 'bmp', 'gif', 'svg', 'webp'];
|
|
|
|
|
|
|
|
if (!in_array($extension, $allowedExtensions)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-26 16:51:12 +00:00
|
|
|
$tempFileName = @tempnam("/tmp", "image-check-");
|
2023-06-05 16:27:30 +00:00
|
|
|
$handle = fopen($tempFileName, "w");
|
|
|
|
|
|
|
|
fwrite($handle, $file);
|
|
|
|
fclose($handle);
|
|
|
|
|
2023-09-26 16:51:12 +00:00
|
|
|
if ($extension == 'svg') {
|
|
|
|
return 'image/svg+xml' === mime_content_type($tempFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
$size = @getimagesize($tempFileName);
|
2023-06-05 16:27:30 +00:00
|
|
|
return is_array($size) && str_starts_with($size['mime'], 'image');
|
|
|
|
}
|