if (!empty($arItem['PARAMS']['SECTION']) && !empty($arItem['PARAMS']['SECTION']['DESCRIPTION'])) {
$sDescription = $arItem['PARAMS']['SECTION']['UF_OPIS'];
} else if (!empty($arItem['PARAMS']['ELEMENT']) && !empty($arItem['PARAMS']['ELEMENT']['UF_OPIS'])) {
$sDescription = $arItem['PARAMS']['ELEMENT']['UF_OPIS'];
}
// Начиная с PHP 7.4.0, строка выше может быть записана как: // echo strip_tags($text, ['p', 'a']);
class Collection
{
public const ASC = 1;
public const DESC = 0;
private $generic;
private $collection = [];
public function __construct(string $class)
{
$this->generic = $class;
}
public function addOne(object $object): self
{
if (!($object instanceof $this->generic)) {
throw new InvalidArgumentException(
'Object mast be instanceof ' . $this->generic . ' class, ' . get_class($object) . ' given'
);
}
$this->collection[] = $object;
return $this;
}
public function addAll(array $objects): self
{
array_map([$this, 'addOne'], $objects);
return $this;
}
public function count(): int
{
return count($this->collection);
}
public function immutable(): self
{
return clone $this;
}
public function getGeneric(): string
{
return $this->generic;
}
private function cmp(string $key, int $order): callable
{
return static function (object $a, object $b) use ($key, $order) {
return $order ? $a->$key <=> $b->$key : $b->$key <=> $a->$key;
};
}
public function where(string $query): self
{
$linq = new Linq($this);
return (new self($this->getGeneric()))->addAll($linq->where($query)->get());
}
public function sort(string $key, int $order = Collection::ASC): self
{
uasort($this->collection, $this->cmp($key, $order));
return $this;
}
public function delete(int $id): void
{
if (array_key_exists($id, $this->collection)) {
unset($this->collection[$id]);
} else {
throw new InvalidArgumentException(
'Key ' . $id . ' is not isset in collection'
);
}
}
public function limit(int $limit, int $offset = 0): self
{
$this->collection = array_slice($this->collection, $offset, $limit);
return $this;
}
public function get(): array
{
return $this->collection;
}
}
class Linq
{
private $collection;
private const CONDITIONS_TYPES = [
'=',
'!=',
'>',
'<',
'<=',
'>=',
];
public function __construct(Collection $collection)
{
$this->collection = $collection;
}
public function where(string $query): self
{
$pattern = '#(?P<field>.*)?(?P<condition>' . implode('|', self::CONDITIONS_TYPES) . ')(?P<value>.*)#is';
preg_match($pattern, $query, $matches);
$count = count($matches);
if (!$count || $count < 4) {
throw new InvalidArgumentException('Not matches ' . '<pre>' . print_r($matches, true) . '</pre>');
}
$args = array_map('trim', $matches);
$array = [
'=' => '===',
'!=' => '!==',
];
return $this->prepare(
$args['field'],
(in_array($args['condition'], $array, true) ? $array[$args['condition']] : $args['condition']),
$args['value']
);
}
private function condition($valFirst, string $condition, $valSecond): bool
{
switch ($condition) {
case '=':
return $valFirst === $valSecond;
case '!=':
return $valFirst !== $valSecond;
case '>':
return $valFirst > $valSecond;
case '<':
return $valFirst < $valSecond;
case '>=':
return $valFirst >= $valSecond;
case '<=':
return $valFirst <= $valSecond;
default:
throw new InvalidArgumentException('Condition not set');
}
}
private function prepare(string $field, string $condition, $value): self
{
$new = new Collection($this->collection->getGeneric());
foreach ($this->collection->get() as $val) {
!$this->condition($val->$field, $condition, $value) ?: $new->addOne($val);
}
$this->collection = $new;
return $this;
}
public function get(): array
{
return $this->collection->get();
}
}
class Changer
{
private $types = [];
private $countTypes;
private $nextType;
public const NEXT = 1;
public const RANDOM = 0;
public static $count = 0;
public function __construct(array $array, int $nextType = self::NEXT)
{
$this->types = $array;
$this->countTypes = count($array);
$this->nextType = $nextType;
}
public function getNext(): string
{
$type = $this->nextType ? $this->types[self::$count++] : $this->types[array_rand($this->types)];
if (self::$count === $this->countTypes) {
self::$count = 0;
}
return $type;
}
}
function getFileList(string $dir): array
{
$array = [];
$fileSPLObjects = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(realpath($dir)),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($fileSPLObjects as $fullFileName => $fileSPLObject) {
if ($fileSPLObject->isFile()) {
$array[] = $fullFileName;
}
}
return $array;
}
// $array = getFileList('./clips');
$array = file('./files.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$result = [];
foreach ($array as $key => $value) {
$parts = explode(' ', $value);
$params = explode('_', $parts[0]);
$result[$key] = (object)[
'pk' => $key,
'author' => (int)$params[0],
'type1' => $params[1],
'type2' => $params[2],
'name' => $value
];
}
$collection = new Collection('stdClass');
$collection->addAll($result);
$clipList = ['LZ', 'MD', 'BS'];
$special = ['AN', 'DB', 'KB'];
$playlist = [];
$genreList = new Changer($clipList);
$spList = new Changer($special);
for ($i = 0; $i < 640; $i++) {
$stype = 'type1';
if ($i) {
$searchKey = $i % 7 === 0 ? $spList->getNext() : $genreList->getNext();
} else {
$searchKey = $genreList->getNext();
}
$linq = $collection->where($stype . ' = ' . $searchKey);
if ($linq->count()) {
$item = $linq->get()[0];
$collection->delete($item->pk);
} else {
$item->name = 'Не найдено ' . $searchKey;
}
$playlist[] = $item->name;
}
echo '<pre>' . print_r($playlist, true) . '</pre>';
exit;
function newImage(int $width, int $height)
{
$newimage = imagecreatetruecolor($width, $height);
$transparent = imagecolorallocatealpha($newimage, 0, 0, 0, 127);
imagefill($newimage, 0, 0, $transparent);
imagealphablending($newimage, true);
imagesavealpha($newimage, true);
return $newimage;
}
function resize($image, int $width, int $height)
{
$newImage = newImage($width, $height);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image));
return $newImage;
}
$imageFile = './donald_duck.png';
#$imageFile = './example.png';
[$filename, $ext] = explode('.', basename($imageFile));
$parts = [
'horizontal' => 2,
'vertical' => 4
];
$quality = 100; // качество в процентах [40 - 100]
$concurrentDirectory = __DIR__ . DIRECTORY_SEPARATOR . 'croped' . DIRECTORY_SEPARATOR . $filename;
// создаём дипекторию
if (!is_dir($concurrentDirectory)) {
mkdir($concurrentDirectory, 777, true);
}
// ресурс изображения
$image = imagecreatefromstring(file_get_contents($imageFile));
imagesavealpha($image, true);
imagealphablending($image, false);
// размеры изображения
$x = imagesx($image);
$y = imagesy($image);
// округляем размеры, чтоб кусочки были одинаковые
$newX = ((int)($x / $parts['horizontal'])) * $parts['horizontal'];
$newY = ((int)($y / $parts['vertical'])) * $parts['vertical'];
// размеры кусочка
$picX = $newX / $parts['horizontal'];
$picY = $newY / $parts['vertical'];
$newImage = resize($image, $newX, $newY);
for ($i = 0, $horizontal = 0; $i < $parts['horizontal'] * $parts['vertical']; $i++) {
$newPicture = newImage($picX, $picY);
if ((($i % $parts['horizontal']) === 0) && ($i !== 0)) {
$horizontal++;
}
$vertical = ($i !== 0) ? ($i % $parts['horizontal']) : $i;
// вырезаем
imagecopyresampled(
$newPicture,
$newImage,
0,
0,
($vertical * $picX),
($horizontal * $picY),
$picX,
$picY,
$picX,
$picY
);
// сохраняем
imagepng(
$newPicture,
$concurrentDirectory . DIRECTORY_SEPARATOR . $filename . '_part' . ($i + 1) . '.png',
(int)($quality / 11),
PNG_ALL_FILTERS
);
imagedestroy($newPicture);
}
Эти значения также находятся в диапазоне [0, 1] и для перевода в [0, 255] их нужно умножить на 255 и округлить.
function getMd5DirHash(string $dir): string
{
$array = [];
$dir = realpath($dir);
$fileSPLObjects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
foreach($fileSPLObjects as $fullFileName => $fileSPLObject ) {
if ($fileSPLObject->isFile()) {
$array[] = $fullFileName;
}
}
$md5 = array_map('md5_file', $array);
return md5(implode('', $md5));
}
$array[] = md5_file($fullFileName);
#$md5 = array_map('md5_file', $array);
return md5(implode('', $array));
$array = [];
$dir = realpath('.'); //путь до папки
$fileSPLObjects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
foreach($fileSPLObjects as $fullFileName => $fileSPLObject ) {
if ($fileSPLObject->isFile()) {
$info = new SplFileInfo($fullFileName);
if (in_array($info->getExtension(), $ext)) {
$array[] = $fullFileName;
}
}
}
echo '<pre>';
print_r($array);
$range = [
'from' => ip2long('194.125.224.0'),
'to' => ip2long('194.125.227.255')
];
$ip = ip2long('194.125.225.124');
echo ($ip >= $range['from'] && $ip <= $range['to']) ? 'yes' : 'no';
$obj = new \SplFileObject($fileName);
ob_get_level() && ob_end_clean();
header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
header('Content-Type: application/force-download');
header('Content-Description: inline; File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="' . $name . '";', false);
header('Content-Length: ' . $size);
while (!$obj->eof()) {
$buf = $obj->fread(16384);
print($buf);
flush();
}
$file = 'путь';
$image = imagecreatefromstring(file_get_contents($file));
$array = array_map('realpath', glob('*.*'));
echo '<pre>' . print_r($array, true) . '</pre>';
$resource = imagecreatefromstring(file_get_contents($array[1]));
echo get_resource_type($resource);
echo '<form action="" method="post">
<input type="text" name="name" />
<input type="submit" name="submit" />
</form>';
$data = 'data.dat';
if (!file_exists($data)) {
touch($data);
}
if (isset($_POST['submit'])) {
$array = file($data, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$int = count($array) ? (int)end($array) : 0;
$array[] = ++$int . ':' . ($_POST['name'] ?? 'Test') . PHP_EOL;
$fp = fopen($data, 'r+b');
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
fwrite($fp, implode(PHP_EOL, $array));
fflush($fp);
flock($fp, LOCK_UN);
}
}