Skip to content

Instantly share code, notes, and snippets.

View cp6's full-sized avatar
🏠
Working from home

corbpie cp6

🏠
Working from home
View GitHub Profile
@cp6
cp6 / convert_bytes.php
Created July 2, 2020 01:12
PHP convert and format bytes function
<?php
function convertBytes(int $bytes, string $convert_to = 'KB', bool $format = true, int $decimals = 2): float
{
if ($convert_to == 'KB') {
$value = ($bytes / 1024);
} elseif ($convert_to == 'MB') {
$value = ($bytes / 1048576);
} elseif ($convert_to == 'GB') {
$value = ($bytes / 1073741824);
} elseif ($convert_to == 'TB') {
@cp6
cp6 / video_details.php
Created July 2, 2020 01:53
Video details and specs function with FFprobe
<?php
function getVideoDetails(string $media_link): array
{
$data = json_decode(shell_exec("ffprobe -v quiet -print_format json -show_format -show_streams '$media_link'"), true);
if (isset($data['streams'])) {//Data exists, video file is readable
$first_stream = $data['streams'][0];
$video_codec = $first_stream['codec_name'];
$video_width = $first_stream['width'];//Video width
$video_height = $first_stream['height'];//Video height
$fr_array = explode('/', $first_stream['r_frame_rate']);//Splits frame rate string into array 60/1 -> 60,1
@cp6
cp6 / aspect_ratio.php
Created July 2, 2020 02:02
Get aspect ratio function
<?php
function getAspectRatio(int $width, int $height): string
{
$ratio_value = ($width / $height);
if ($ratio_value == 1.000) {
$ratio_string = '1:1';
} elseif ($ratio_value == 1.250) {
$ratio_string = '5:4';
} elseif ($ratio_value == 1.333) {
$ratio_string = '4:3';
@cp6
cp6 / int_compare.php
Last active July 2, 2020 02:42
Int or number compare function
<?php
function intCompare(int $value1, int $value2): array
{
if ($value1 > $value2) {//value1 is bigger than value2
$one_is_greater = true;
$symbol = '+';
} else {
$one_is_greater = false;
$symbol = '-';
}
@cp6
cp6 / string_length_check.php
Created July 3, 2020 06:36
String length check for min and max length
<?php
function stringLengthCheck(string $string, int $min, int $max): bool
{
if (strlen($string) >= $min && strlen($string) <= $max) {
return true;
} else {
return false;
}
}
@cp6
cp6 / if_string_contains.php
Created July 3, 2020 06:37
If string contains function
<?php
function stringContains(string $string, string $needle): bool
{
if (strpos($string, $needle) !== false) {
return true;
} else {
return false;
}
}
@cp6
cp6 / size_convert_func.php
Created July 4, 2020 04:00
PHP size conversion function
<?php
function convertSize(float $size, string $convert_from = 'B', string $convert_to = 'KB', bool $format = true, int $decimals = 2): float
{
$size = sprintf("%.2f", $size);//Int to float
if ($convert_from === 'B') {
if ($convert_to === 'KB') {
$value = ($size / 1024);
} elseif ($convert_to === 'MB') {
$value = ($size / 1048576);
} elseif ($convert_to === 'GB') {
@cp6
cp6 / loop_columns.php
Last active July 12, 2020 01:56
PHP MySQL loop with Bootstrap 4 columns
<style>
body {
background: #0d161f;
}
.row {
background: #00b9eb;
padding: .4rem;
margin-top: .6rem;
text-align: center;
}
@cp6
cp6 / sitemapxml_class.php
Last active July 19, 2020 04:02
PHP sitemap xml builder class
<?php
class sitemapBuilder
{
private function db_connect(): object
{
$host = '127.0.0.1';
$db_name = 'sitemap';
$db_user = 'root';
$db_password = '';
@cp6
cp6 / randomtime_func.php
Created July 25, 2020 06:41
PHP generate random time from timestamp
<?php
function randomTimeFormatted(string $max_time): string
{
$t = explode(':', $max_time);
return sprintf("%02d:%02d:%02d", rand(0, $t[0]), rand(0, $t[1]), rand(0, $t[2]));
}
echo randomTimeFormatted('02:38:55');
//02:38:55 will make random string from 0->2 hours, 0->38 mins, 0->55 seconds