You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:将图片自动发帖插件改写为支持视频自动发帖

How to Modify Your Plugin to Support Video Uploads & Video Posts

Let’s break down the key changes needed across your frontend form, server-side upload handling, and post rendering logic. I’ll use example code snippets based on common plugin architectures (adjust for your specific tech stack if needed):

1. Update the Frontend Upload Form

First, modify your form to accept video files (you can keep image support too if desired):

<!-- Updated Form with Video/Image Support -->
<form id="post-upload-form" enctype="multipart/form-data" method="POST">
  <div>
    <label for="post-title">Post Title:</label>
    <input type="text" id="post-title" name="post_title" required>
  </div>
  <div>
    <label for="post-description">Post Description:</label>
    <textarea id="post-description" name="post_description" required></textarea>
  </div>
  <div>
    <label for="post-media">Upload Video or Image:</label>
    <input type="file" id="post-media" name="post_media" accept="video/*, image/*" required>
    <p>Allowed formats: MP4, WebM, OGG (videos) | JPG, PNG (images)</p>
  </div>
  <button type="submit">Publish Post</button>
</form>

2. Server-Side: Handle Video Uploads

Adjust your backend code to validate and store video files properly. Here’s a PHP example:

// Process uploaded media on form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['post_media'])) {
  $uploadDir = '/path/to/your/plugin/uploads/';
  $fileName = uniqid() . '-' . basename($_FILES['post_media']['name']);
  $filePath = $uploadDir . $fileName;
  $fileType = mime_content_type($_FILES['post_media']['tmp_name']);

  // Define allowed media types
  $allowedVideoTypes = ['video/mp4', 'video/webm', 'video/ogg'];
  $allowedImageTypes = ['image/jpeg', 'image/png'];
  $allowedTypes = array_merge($allowedVideoTypes, $allowedImageTypes);

  // Validate file type
  if (!in_array($fileType, $allowedTypes)) {
    die("Error: Invalid file type. Please upload a supported video or image.");
  }

  // Validate file size (e.g., 50MB max)
  $maxFileSize = 50 * 1024 * 1024; // 50MB
  if ($_FILES['post_media']['size'] > $maxFileSize) {
    die("Error: File too large. Max allowed size is 50MB.");
  }

  // Save the uploaded file
  if (move_uploaded_file($_FILES['post_media']['tmp_name'], $filePath)) {
    $mediaType = in_array($fileType, $allowedVideoTypes) ? 'video' : 'image';
    
    // Generate post content (replace with your existing post creation flow)
    $postContent = buildPostContent(
      $_POST['post_title'],
      $_POST['post_description'],
      $fileName,
      $mediaType
    );

    // Publish the post (use your plugin's existing save/publish function)
    publishNewPost($postContent);
    echo "Post published successfully!";
  } else {
    die("Error: Failed to upload your media file.");
  }
}

3. Modify Post Content Generation

Update your post rendering function to embed videos instead of images when appropriate:

function buildPostContent($title, $description, $mediaFileName, $mediaType) {
  $mediaUrl = '/plugin/uploads/' . $mediaFileName; // Adjust to your public upload URL

  $postHtml = "<h2>{$title}</h2>";
  $postHtml .= "<p>{$description}</p>";

  // Add media element based on type
  if ($mediaType === 'video') {
    $postHtml .= "<video controls width='100%' style='max-width: 800px; margin: 1rem 0;'>
                    <source src='{$mediaUrl}' type='video/mp4'>
                    <source src='{$mediaUrl}' type='video/webm'>
                    <source src='{$mediaUrl}' type='video/ogg'>
                    Your browser does not support HTML5 video.
                  </video>";
  } else {
    $postHtml .= "<img src='{$mediaUrl}' alt='{$title}' style='max-width: 100%; margin: 1rem 0;'>";
  }

  return $postHtml;
}

4. Additional Tips

  • Video Encoding: For broader browser support, consider adding server-side encoding (using tools like FFmpeg) to convert videos to MP4 (the most universally supported format).
  • Responsive Design: The example uses width='100%' on the video element to ensure it adapts to different screen sizes.
  • Storage & Delivery: Videos are larger than images—ensure your hosting has enough space, and consider using a CDN for faster global delivery.
  • Accessibility: Encourage users to add descriptive captions or transcripts in the description field for video content.

If you can share snippets of your existing plugin code (especially form handling and post generation), I can refine these examples to match your exact setup!

内容的提问来源于stack exchange,提问作者Bas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:23:19