Архив

Архив раздела ‘PHP’

javascript:

function canUseWebp(){
	let elem=document.createElement('canvas');
	if(!!(elem.getContext&&elem.getContext('2d'))){
		return elem.toDataURL('image/webp').indexOf('data:image/webp')==0;
	}
	return false;
}
window.onload=function(){
	let images=document.querySelectorAll('[data-bg]');
	for(let i=0;i<images.length;i++){
		let image=images[i].getAttribute('data-bg');
		images[i].style.backgroundImage='url('+image+')';
	}
	let isitFirefox=window.navigator.userAgent.match(/Firefox\/([0-9]+)\./);
	let firefoxVer=isitFirefox?parseInt(isitFirefox[1]):0;
	if(canUseWebp()||firefoxVer>=65){
		let imagesWebp=document.querySelectorAll('[data-bg-webp]');
		for(let i=0;i<imagesWebp.length;i++){
			let imageWebp=imagesWebp[i].getAttribute('data-bg-webp');
			imagesWebp[i].style.backgroundImage='url('+imageWebp+')';
		}
	}
};

html:

<div class="item" style="background-image:url('/img/banners/banner2.webp');" data-bg="/img/banners/banner2.jpg" data-bg-webp="/img/banners/banner2.webp"></div>

пример программного вывода:

function attr_background_image($image,$dir,$add=''){
	$html='';
	$image_path=ROOT_HTTP.$dir.$image;
	$image_pathinfo=pathinfo($image_path);
	$ext=($image_pathinfo['extension']=='jpg')?'jpeg':$image_pathinfo['extension'];
	if(USE_WEBP==1&&$ext!='svg'&&file_exists(ROOT_DIR.$dir.$image_pathinfo['filename'].'.webp')){
		$html.=' style="background-image:url(\''.ROOT_HTTP.$dir.$image_pathinfo['filename'].'.webp\');" data-bg="'.ROOT_HTTP.$dir.$image.'" data-bg-webp="'.ROOT_HTTP.$dir.$image_pathinfo['filename'].'.webp"';
	}
	else{
		$html.=' style="background-image:url(\''.ROOT_HTTP.$dir.$image.'\');'.$add.'"';
	}
	return $html;
}
$block_bg='';
if($block['photo']||$block['bgcolor']){
	if($block['photo']){
		$bgcolor='';
		if($block['bgcolor']){
			$bgcolor.='background-color:'.$block['bgcolor'].';';
		}
		$block_bg=attr_background_image($block["photo"],IMAGES_BLOCKS_DIR,$bgcolor);
	}
	elseif($block['bgcolor']){
		$block_bg.=' style="';
		$block_bg.='background-color:'.$block['bgcolor'].';';
		$block_bg.='"';
	}
}

или

<div class="item"<?=attr_background_image($item["photo"],IMAGES_BANNERS_DIR)?>>

https://webinmind.ru/javascript/webp-to-background-image

Categories: Javascript, PHP Tags: , ,
function write_ini($array,$file){
	$res=array();
	foreach($array as $key=>$val){
		if(is_array($val)){
			$res[]="[$key]";
			foreach($val as $skey=>$sval){
				$res[]="$skey = ".(is_numeric($sval)?$sval:'"'.$sval.'"');
			}
		}
		else{
			$res[]="$key = ".(is_numeric($val)?$val:'"'.$val.'"');
		}
	}
	safefilerewrite($file,implode("\r\n",$res));
}
function safefilerewrite($fileName,$dataToSave){
	if($fp=fopen($fileName,'w')){
		$startTime=microtime(TRUE);
		do{
			$canWrite=flock($fp,LOCK_EX);
			if(!$canWrite){
				usleep(round(rand(0,100)*1000));
			}
		}
		while((!$canWrite)and((microtime(TRUE)-$startTime)<5));
		if($canWrite){
			fwrite($fp,$dataToSave);
			flock($fp,LOCK_UN);
		}
		fclose($fp);
	}
}
$ini=parse_ini_file($_SERVER['DOCUMENT_ROOT']."/file.ini");
$ini['key']='value';
write_ini($ini,$_SERVER['DOCUMENT_ROOT']."/file.ini");
Categories: PHP Tags:
19 апреля 2021 Нет комментариев
function get_file_size($bytes){
	if($bytes<1000*1024){
		return number_format($bytes/1024,2)."KB";
	}
	elseif($bytes<1000*1048576){
		return number_format($bytes/1048576,2)."MB";
	}
	elseif($bytes<1000*1073741824){
		return number_format($bytes/1073741824,2)."GB";
	}
	else{
		return number_format($bytes/1099511627776,2)."TB";
	}
}
Categories: PHP Tags:
31 марта 2021 Нет комментариев
//список
print_r(glob(ROOT_DIR.RESIZE_CACHE_DIR."*".$fname));
//удалить по маске
foreach(glob(ROOT_DIR.RESIZE_CACHE_DIR."*".$fname) as $file){
	unlink($file);
}
Categories: PHP Tags:
11 марта 2021 Нет комментариев

Для латиницы функция ucfirst
Для utf-8:

function ucfirst_utf8($str){
	return mb_strtoupper(mb_substr($str,0,1)).mb_substr($str,1);
}
echo ucfirst_utf8("предложение с большой буквы");
Categories: PHP Tags:
16 февраля 2021 Нет комментариев
$result=array_diff($all_files,$current_files);
Categories: PHP Tags:
25 августа 2020 Нет комментариев
$zip=new ZipArchive;
$zip->open(ROOT_DIR.IMPORT_DIR.'import.zip');
for($i=0;$i<$zip->numFiles;$i++){
	$stat=$zip->statIndex($i);
	$import_file=basename($stat['name']);
	echo $import_file."\r\n";
}
$zip->extractTo(ROOT_DIR.IMPORT_DIR);
$zip->close();
Categories: PHP Tags: