Для получения полного доступа
зарегистрируйтесь.

blacksmoke26    13   21 712

Открыт к предложениям по работе

Full Stack developer
@ Lesson-Planner.UK
@ Recruit.ac

Yii2 Plugns:
Yii2 CDN

Git Profile:
https://github.com/blacksmoke26/

  • Chakwal, Pakistan
  • Black Smoke
  • yii2, javascript, php, mysql, css, sass, html, sql
  • Зарегистрирован 3 года назад
/**
 * Check that given file is an image file.
 * @param string $filename The file path to be checked
 * @return bool TRUE when image file | FALSE when none
 */
function isImageFile ( string $filename ) {
	if ( !is_file ( $filename) || !is_readable ( $filename) ) {
		return false;
	} else if ( class_exists('\finfo') ) {
		$mime = (new \finfo(FILEINFO_MIME))->file ( $filename);
		return strpos((string)$mime, 'image') !== false;
	} else if ( function_exists('getimagesize') ) {
		$size = @getimagesize ( $filename);
		return !@is_array($size) ? false : true;
	} else if ( function_exists('exif_imagetype') && exif_imagetype ( $filename) === false ) {
		return false;
	} else if ( function_exists('mime_content_type') ) {
		$mime = @mime_content_type ($filename);
		return strpos((string)$mime, 'image') !== false;
	} else {
		return true;
	}
}

/************
 * EXAMPLE
 ***********/
// Can validate all sort of images
var_dump ( isImageFile('image.jpg') );

/***********
 * OUTPUT
 **********/
# (bool) true
/**
 * Create image file from base64 image data
 * @param string $filename Path to the file where to write the data.
 * @param string $data The base64 image data
 * @return bool True when created | False anyway
 */
function imgBase64ToFile ( string $filename, string $data ) {
	list(, $data) = explode(';', $data);
	list(, $data) = explode(',', $data);
	$data = base64_decode($data);
	return file_put_contents($filename, $data) !== false;
}

/************
 * EXAMPLE
 ***********/
$base64Data = <<<BASE64
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAGrUlEQVR42uzceWwUdRjGcUSg3BVo5FQqIEVb1KgQkFCQy6hEiSCCSggqlyJRYzgMEm8iJpXDGNGImphIUJZCKGqqoCbcRiERUS6jqK1SkbMFgb5+/9g/Jj9mdjql2233fZ/kk5C2bJp3nu3MzvxmGoiIUUz9ALRTPwDt1A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('image.png', $base64Data);
Сниппет,  PHP

Generate a GUID string

/**
 * Generate a GUID string
 * @link http://php.net/manual/en/function.random-int.php#118636
 * @return string
 */
function guid () {
	/** @var string $uid */
	return sprintf ( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

		// 32 bits for "time_low"
		random_int ( 0, 0xffff ), random_int ( 0, 0xffff ),

		// 16 bits for "time_mid"
		random_int ( 0, 0xffff ),

		// 16 bits for "time_hi_and_version",
		// four most significant bits holds version number 4
		random_int ( 0, 0x0fff ) | 0x4000,

		// 16 bits, 8 bits for "clk_seq_hi_res",
		// 8 bits for "clk_seq_low",
		// two most significant bits holds zero and one for variant DCE1.1
		random_int ( 0, 0x3fff ) | 0x8000,

		// 48 bits for "node"
		random_int ( 0, 0xffff ), random_int ( 0, 0xffff ), random_int ( 0, 0xffff ) );
}

/*************
 * EXAMPLE
 ************/
 var_dump ( guid () );

 /*************
 * OUTPUT
 ************/
 # (string) '35cf783a-e866-4956-871c-6af2ef6f7bc9'
/**
 * Check that URL is valid and exists.
 * @param string $url Url to check
 * @return bool TRUE when valid | FALSE anyway
 */
function urlExists ( $url ) {
	// Remove all illegal characters from a url
	$url = filter_var($url, FILTER_SANITIZE_URL);

	// Validate URI
	if (filter_var($url, FILTER_VALIDATE_URL) === FALSE
		// check only for http/https schemes.
		|| !in_array(strtolower(parse_url($url, PHP_URL_SCHEME)), ['http','https'], true )
	) {
		return false;
	}

	// Check that url exists
	$file_headers = @get_headers($url);
	return !!( !is_array($file_headers) || strpos($file_headers[0], '404') === false );
}

/*************
 * EXAMPLE
 ************/

 var_dump (urlExists ('http://www.yiiframework.com/'));
 var_dump (urlExists ('http://foo.bar/'));

 /*************
 * OUTPUT
 ************/
 #1 (bool) true
 #2 (bool) false