Last active
June 25, 2021 05:16
-
-
Save supernovaplus/800a99b2ce2ecb05b3389b1e305e44b6 to your computer and use it in GitHub Desktop.
PHP - Compress and upload image to Discord via Webhook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ini_set('display_errors', 1); | |
// ini_set('display_startup_errors', 1); | |
// error_reporting(E_ALL); | |
//from https://stackoverflow.com/questions/65133851/uploading-file-to-discord-with-webhook-in-php | |
//file is being called by a POST request in my case | |
//print_r for debugging | |
//default max size discord accepts is 8MB | |
echo "<pre>"; | |
$webhookurl = "https://discord.com/api/webhooks/x/x"; | |
print_r($_FILES); | |
//compress start================================ | |
$compressed_image = false; | |
$file_name = $_FILES['file']['name']; | |
$file_size = $_FILES['file']['size']; | |
$before_compress_size = $_FILES['file']['size']; | |
$file_type = $_FILES['file']['type']; | |
if ($file_type == 'image/jpeg') { | |
$compressed_image = imagecreatefromjpeg($_FILES['file']['tmp_name']); | |
} elseif ($file_type == 'image/gif') { | |
// $compressed_image = imagecreatefromgif($_FILES['file']['tmp_name']); | |
} elseif ($file_type == 'image/png') { | |
$compressed_image = imagecreatefrompng($_FILES['file']['tmp_name']); | |
$file_name = str_replace(".png", ".jpg", $file_name); | |
} elseif ($file_type == 'image/webp') { | |
$compressed_image = imagecreatefromwebp($_FILES['file']['tmp_name']); | |
$file_name = str_replace(".webp", ".jpg", $file_name); | |
} else{ | |
die("not an image"); | |
} | |
if($compressed_image !== false){ | |
imagejpeg($compressed_image, $_FILES['file']['tmp_name'], 85); | |
imagedestroy($compressed_image); | |
$file_type = "image/jpeg"; | |
$file_size = filesize($_FILES['file']['tmp_name']); | |
} | |
//compress end================================ | |
$json_data = [ | |
"content" => "filename = $file_name \ntype = $file_type \nsize = " . ($file_size / 1000000) . "MB\n" . ($before_compress_size !== $file_size ? "before compress size = " . ($before_compress_size/1000000) . "MB" : ""), | |
"tts" => "false", | |
"file" => curl_file_create($_FILES["file"]["tmp_name"], $file_type, $file_name) | |
]; | |
print_r($json_data); | |
$curl = curl_init( $webhookurl ); | |
curl_setopt($curl, CURLOPT_TIMEOUT, 10); // 5 seconds | |
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); // 5 seconds | |
curl_setopt($curl, CURLOPT_POST, 1); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data')); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data); | |
$returned_fileName = curl_exec( $curl ); | |
curl_close( $curl ); | |
echo "---response---\n"; | |
print_r(json_decode($returned_fileName)); | |
echo "</pre><a href=\"upload.html\">upload.html</a>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment