function generate_collage($dir_path, $fixed_width = 1300, $cell_width = 215, $max_size = 150, $padding = 10) { $images = glob($dir_path . '{*.jpg,*.jpeg,*.png}', GLOB_BRACE); $images = array_slice(shuffle($images), 0, 20); // Random 20 max for variety if (empty($images)) return get_template_directory_uri() . '/images/fallback.png'; $cols = floor($fixed_width / ($cell_width + $padding * 2)); // Dynamic columns $rows = ceil(count($images) / $cols); $cell_height = $cell_width * 0.75; // 4:3 aspect for thumbnails $total_height = $rows * ($cell_height + $padding * 2) + $padding; ob_start(); $canvas = imagecreatetruecolor($fixed_width, $total_height); $bg = imagecolorallocate($canvas, 240, 240, 240); imagefill($canvas, 0, 0, $bg); foreach ($images as $i => $img_path) { if ($i >= $cols * $rows) break; $x = ($i % $cols) * ($cell_width + $padding * 2) + $padding; $y = floor($i / $cols) * ($cell_height + $padding * 2) + $padding; $ext = strtolower(pathinfo($img_path, PATHINFO_EXTENSION)); $img = match($ext) { 'png' => imagecreatefrompng($img_path), default => imagecreatefromjpeg($img_path) }; $w = imagesx($img); $h = imagesy($img); $ratio = min($max_size / $w, $max_size / $h, $cell_width / $w, $cell_height / $h); $nw = intval($w * $ratio); $nh = intval($h * $ratio); $thumb = imagecreatetruecolor($nw, $nh); imagecopyresampled($thumb, $img, 0, 0, 0, 0, $nw, $nh, $w, $h); // Center in cell $offset_x = ($cell_width - $nw) / 2; $offset_y = ($cell_height - $nh) / 2; imagecopy($canvas, $thumb, $x + $offset_x, $y + $offset_y, 0, 0, $nw, $nh); imagedestroy($img); imagedestroy($thumb); } ob_clean(); imagejpeg($canvas, null, 85); imagedestroy($canvas); $data = ob_get_clean(); $cache_key = 'collage_' . md5($dir_path . $fixed_width . count($images)); set_transient($cache_key, $data, 600); // 10min cache return 'data:image/jpeg;base64,' . base64_encode($data); } Nothing found for Blog Articles Tag Index

Loading...

Nothing Found

Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.

Are you interested in this service or have any questions? Get in touch with Lynda now!

Testimonials