

Steps to Run
- Server Setup:
- Place upload.php in your PHP server's root directory (e.g., /var/www/html/ or htdocs/).
- Create an uploads/ folder in the same directory with write permissions (e.g., chmod 755 uploads).
- Ensure your PHP server (e.g., Apache) is running.
- Verify the uploads/ folder is accessible (e.g., http://localhost/uploads/).
- Client Setup:
- Save index.html in the same directory as upload.php.
- Replace the placeholder image1Src and image2Src in index.html with your actual base64 image strings.
- Open index.html in a browser (e.g., http://localhost/index.html).
- Click the "Insert HTML" button to upload images and insert the HTML into CKEditor.
- Verify:
- Check the CKEditor content to confirm the HTML uses image URLs (e.g., http://localhost/uploads/image1-123456.png).
- Open the browser console to see the inserted HTML (editor.getData()).
- Visit the image URLs to ensure the images are accessible.
Notes
- Base64 Images: Replace the example base64 strings in image1Src and image2Src with your actual base64 data.
- Base URL: The PHP script generates the imageUrl based on the server's host and path. Adjust $baseUrl in upload.php if your server uses a different structure (e.g., a subdirectory).
- Permissions: Ensure the uploads/ folder is writable by the web server (e.g., chown www-data:www-data uploads or chmod 755 uploads).
- Security:
- The PHP script accepts all base64 images. In production, add validation (e.g., file size limits, allowed extensions like png, jpg).
- Example validation:
php
$allowedExtensions = ['png', 'jpg', 'jpeg', 'gif']; if (!in_array($extension, $allowedExtensions)) { echo json_encode(['error' => 'Invalid image format']); exit; }
- CSS: Ensure your CSS for classes like image-anime and reveal is included in the editor's context (e.g., add a <link> tag in index.html or inject styles into CKEditor).
html
<link rel="stylesheet" href="styles.css"> - Editor: If you're using TinyMCE, replace editor.insertHtml(html) with editor.insertContent(html) and update the initialization:
html
<script src="https://cdn.tinymce.com/4/tinymce.min.js"></script> <script> tinymce.init({ selector: '#editor', extended_valid_elements: 'div[class|style],figure[class],img[src|alt]' }); let editor = tinymce.get('editor'); </script>
Troubleshooting
- Images Not Saving:
- Check the PHP error log (e.g., /var/log/apache2/error.log) for issues.
- Verify the uploads/ folder exists and is writable.
- Ensure file_put_contents is not blocked by server permissions.
- HTML Incorrect:
- Log editor.getData() to compare the inserted HTML with your template.
- Example:
javascript
console.log('Inserted HTML:', editor.getData());
- Design Broken:
- Confirm that the CSS for image-anime, reveal, etc., is loaded in CKEditor.
- Inject CSS into CKEditor if needed:
javascript
CKEDITOR.addCss('.image-anime { /* your styles */ }');
- 404 on Image URLs:
- Verify the $baseUrl in upload.php matches your server's URL structure.
- Test the image URL directly in the browser (e.g., http://localhost/uploads/image1-123456.png).
- CORS Issues:
- If upload.php is on a different domain, add CORS headers:
php
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST'); header('Access-Control-Allow-Headers: Content-Type');
- If upload.php is on a different domain, add CORS headers:
Example Output
After clicking "Insert HTML":
- Images are saved to uploads/ (e.g., uploads/image1-66f7b8c9.png).