« Back to History
helpers.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php // lib/helpers.php // Shayari helpers (ready-to-drop) // - interleave_texts_with_images_no_reuse() // - candidate_category_folders() // - get_images_for_category() // - get_images_from_specific_folder() // - fetch_category_rule(PDO,$category_key) // - fetch_post_image_folder(PDO,$post_id) // - render_interleaved_items() /** * Interleave texts with images using NO-REUSE policy. * * @param string[] $texts * @param string[] $images web-relative image paths (e.g. 'uploads/love_shayari_img/x.jpg') * @param int $interval insert an image after this many texts (>=1) * @param string $mode 'sequential'|'random' * @return array items array [{type:'text'|'image','content':string}, ...] */ function interleave_texts_with_images_no_reuse(array $texts, array $images, int $interval = 2, string $mode = 'sequential'): array { $items = []; if ($interval < 1) $interval = 1; $nTexts = count($texts); $nImgs = count($images); if ($mode === 'random' && $nImgs > 1) { $images = $images; // copy shuffle($images); } $imgPtr = 0; for ($i = 0; $i < $nTexts; $i++) { $items[] = ['type' => 'text', 'content' => $texts[$i]]; if ((($i + 1) % $interval) === 0) { if ($imgPtr < $nImgs) { $items[] = ['type' => 'image', 'content' => $images[$imgPtr]]; $imgPtr++; } } } return $items; } /** * Candidate folder name variations for a category key. * Example: 'love_shayari' => ['love_shayari_img','love_shayari','love_shayari_images', ...] * * @param string $category_key * @return string[] */ function candidate_category_folders(string $category_key): array { $key = trim($category_key); $c = []; if ($key !== '') { $c[] = $key . '_img'; $c[] = $key; $c[] = $key . '_images'; $c[] = str_replace(' ', '_', $key) . '_img'; } // unique & normalized $out = []; foreach ($c as $v) { $v = trim($v); if ($v === '') continue; if (!in_array($v, $out, true)) $out[] = $v; } return $out; } /** * Get image web-relative paths for a given category by scanning candidate folders. * * @param string $category_key * @param string $uploads_base_dir Absolute path to uploads directory (example: __DIR__ . '/../uploads' or __DIR__ . '/uploads') * @param string $web_base Web-relative base for returned paths (default 'uploads') * @param array $exts allowed extensions * @return string[] e.g. ['uploads/love_shayari_img/x.jpg', ...] */ function get_images_for_category(string $category_key, string $uploads_base_dir, string $web_base = 'uploads', array $exts = ['jpg','jpeg','png','webp','gif']): array { $images = []; $cands = candidate_category_folders($category_key); foreach ($cands as $folderName) { $absFolder = rtrim($uploads_base_dir, '/\\') . DIRECTORY_SEPARATOR . $folderName; if (!is_dir($absFolder)) continue; $files = []; foreach ($exts as $e) { $files = array_merge($files, glob($absFolder . DIRECTORY_SEPARATOR . '*.' . $e, GLOB_BRACE) ?? []); $files = array_merge($files, glob($absFolder . DIRECTORY_SEPARATOR . '*.' . strtoupper($e), GLOB_BRACE) ?? []); } if ($files) { sort($files, SORT_NATURAL | SORT_FLAG_CASE); $seen = []; foreach ($files as $f) { $base = basename($f); if (isset($seen[$base])) continue; $seen[$base] = true; $images[] = rtrim($web_base, '/\\') . '/' . $folderName . '/' . $base; } return $images; // only first valid folder } } return $images; } /** * Get images from an exact folder name under uploads. * This is strict: folder must match exactly and exist under uploads_base_abs. * * @param string $folderName exact folder name (e.g. 'love_shayari_img') * @param string $uploads_base_abs absolute path to uploads dir (e.g. __DIR__ . '/../uploads' or __DIR__ . '/uploads') * @param string $uploads_web_base web-relative base for returned path (e.g. 'uploads') * @return string[] web-relative image paths */ function get_images_from_specific_folder(string $folderName, string $uploads_base_abs, string $uploads_web_base = 'uploads'): array { $folder_abs = rtrim($uploads_base_abs, '/\\') . DIRECTORY_SEPARATOR . trim($folderName, '/\\'); if (!is_dir($folder_abs)) { return []; } $pattern = $folder_abs . DIRECTORY_SEPARATOR . '*.{jpg,jpeg,png,webp,gif,JPG,JPEG,PNG,WEBP,GIF}'; $files = glob($pattern, GLOB_BRACE) ?: []; if (!$files) return []; sort($files, SORT_NATURAL | SORT_FLAG_CASE); $result = []; foreach ($files as $f) { $result[] = '/' . trim($uploads_web_base, '/\\') . '/' . trim($folderName, '/\\') . '/' . basename($f); } return $result; } /** * Fetch category rule from DB. * Expects table `category_image_rules` with columns category_key, interval_after, select_mode. * * @param PDO $pdo * @param string $category_key * @return array|null ['interval_after'=>int, 'select_mode'=>string] or null */ function fetch_category_rule(PDO $pdo, string $category_key): ?array { $sql = "SELECT interval_after, select_mode FROM category_image_rules WHERE category_key = :k LIMIT 1"; $stmt = $pdo->prepare($sql); $stmt->execute([':k' => $category_key]); $row = $stmt->fetch(PDO::FETCH_ASSOC); if (!$row) return null; return [ 'interval_after' => isset($row['interval_after']) ? (int)$row['interval_after'] : 2, 'select_mode' => $row['select_mode'] ?? 'sequential', ]; } /** * Fetch mapped folder for a post. Expects table `post_image_folder(post_id PK, folder_name)`. * * @param PDO $pdo * @param int $post_id * @return string|null folder name (exact) or null if not mapped */ function fetch_post_image_folder(PDO $pdo, int $post_id): ?string { $sql = "SELECT folder_name FROM post_image_folder WHERE post_id = :pid LIMIT 1"; $stmt = $pdo->prepare($sql); $stmt->execute([':pid' => $post_id]); $row = $stmt->fetch(PDO::FETCH_ASSOC); if (!$row) return null; return $row['folder_name']; } /** * Render interleaved items as HTML. Small convenience function. * * @param array $items array of ['type'=>'text'|'image','content'=>string] * @param array $opts optional: text_wrapper, text_class, img_class, img_attr * @return string */ function render_interleaved_items(array $items, array $opts = []): string { $text_wrapper = $opts['text_wrapper'] ?? 'div'; $text_class = $opts['text_class'] ?? 'shayari-text'; $img_class = $opts['img_class'] ?? 'shayari-image'; $img_attr = $opts['img_attr'] ?? 'loading="lazy"'; $out = ''; foreach ($items as $it) { if (!isset($it['type']) || !isset($it['content'])) continue; if ($it['type'] === 'text') { $content = nl2br(htmlspecialchars($it['content'])); $out .= "<{$text_wrapper} class=\"" . htmlspecialchars($text_class) . "\">{$content}</{$text_wrapper}>"; } elseif ($it['type'] === 'image') { $src = htmlspecialchars($it['content']); $out .= "<div class=\"" . htmlspecialchars($img_class) . "\"><img src=\"{$src}\" {$img_attr} alt=\"\"></div>"; } } return $out; } /** * Fetch post-level image rule. * Table: post_image_rules */ function fetch_post_image_rule(PDO $pdo, int $post_id): ?array { $stmt = $pdo->prepare(" SELECT * FROM post_image_rules WHERE post_id = ? LIMIT 1 "); $stmt->execute([$post_id]); $row = $stmt->fetch(PDO::FETCH_ASSOC); return $row ?: null; } /* End of helpers.php */