function gig_generate_image_by_post_type($term_name, $post_type) { // Пути к разным ресурсам $bg_path = ($post_type === 'mexc_wiki') ? WP_CONTENT_DIR . '/uploads/Wiki.jpg' : WP_CONTENT_DIR . '/uploads/Glossary.jpg'; // Определяем шрифт $arabic_font_path = WP_CONTENT_DIR . '/uploads/fonts/Amiri-Regular.ttf'; $latin_font_path = WP_CONTENT_DIR . '/uploads/fonts/Helvetica.ttf'; if (preg_match('/\p{Arabic}/u', $term_name)) { $font_path = $arabic_font_path; } else { $font_path = $latin_font_path; } if (!file_exists($bg_path) || !file_exists($font_path)) { error_log('Glossary Image Generator: Missing background or font for post type ' . $post_type); return false; } $image = imagecreatefromjpeg($bg_path); if (!$image) return false; // Цвет текста $color = imagecolorallocate($image, 255, 255, 255); $max_width = imagesx($image) * 0.9; $image_height = imagesy($image); // Размер шрифта $font_size = 72; $min_font_size = 10; // Проверка ширины текста $get_text_width = function($text, $size) use ($font_path) { $bbox = imagettfbbox($size, 0, $font_path, $text); return abs($bbox[2] - $bbox[0]); }; // Перенос строк $wrap_text = function($text, $size, $max_width) use ($get_text_width) { $words = explode(' ', $text); $lines = []; $current_line = ''; foreach ($words as $word) { $test_line = $current_line === '' ? $word : $current_line . ' ' . $word; $test_width = $get_text_width($test_line, $size); if ($test_width <= $max_width) { $current_line = $test_line; } else { if ($current_line !== '') $lines[] = $current_line; $current_line = $word; } } if ($current_line !== '') $lines[] = $current_line; return $lines; }; // Подгонка текста do { $lines = $wrap_text($term_name, $font_size, $max_width); $max_line_width = 0; foreach ($lines as $line) { $w = $get_text_width($line, $font_size); if ($w > $max_line_width) $max_line_width = $w; } if ($max_line_width <= $max_width) break; $font_size--; if ($font_size < $min_font_size) break; } while (true); // Центровка по вертикали $bbox = imagettfbbox($font_size, 0, $font_path, 'م'); // арабская буква для высоты $font_height = abs($bbox[7] - $bbox[1]); $line_height = intval($font_height * 1.3); $text_block_height = count($lines) * $line_height; $y = ($image_height / 2) - ($text_block_height / 2) + $line_height; foreach ($lines as $line) { $text_width = $get_text_width($line, $font_size); $x = (imagesx($image) - $text_width) / 2; imagettftext($image, $font_size, 0, $x, $y, $color, $font_path, $line); $y += $line_height; } // Сохраняем файл $upload_dir = wp_upload_dir(); $translit_name = gig_transliterate_filename($term_name); if (empty($translit_name)) { $translit_name = md5($term_name); // fallback для арабского } $file_name = sanitize_file_name($translit_name . '-' . $post_type . '-image.jpg'); $file_path = $upload_dir['path'] . '/' . $file_name; if (file_exists($file_path)) unlink($file_path); imagejpeg($image, $file_path, 90); imagedestroy($image); // WP Attachment $filetype = wp_check_filetype($file_name, null); $attachment = [ 'post_mime_type' => $filetype['type'], 'post_title' => $term_name, 'post_status' => 'inherit', 'guid' => $upload_dir['url'] . '/' . $file_name, ]; $attach_id = wp_insert_attachment($attachment, $file_path); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata($attach_id, $file_path); wp_update_attachment_metadata($attach_id, $attach_data); return $attach_id; }