Last active
July 20, 2023 19:25
-
-
Save antxd/9fa72983d5197c166e441276e23bf786 to your computer and use it in GitHub Desktop.
Simple example of case use imgbb API
This file contains 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 | |
function save_record_image($image,$name = null){ | |
$API_KEY = 'YOUR_CLIENT_API_KEY'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://api.imgbb.com/1/upload?key='.$API_KEY); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); | |
$extension = pathinfo($image['name'],PATHINFO_EXTENSION); | |
$file_name = ($name)? $name.'.'.$extension : $image['name'] ; | |
$data = array('image' => base64_encode(file_get_contents($image['tmp_name'])), 'name' => $file_name); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
$result = curl_exec($ch); | |
if (curl_errno($ch)) { | |
return 'Error:' . curl_error($ch); | |
}else{ | |
return json_decode($result, true); | |
} | |
curl_close($ch); | |
} | |
if (!empty($_FILES['record_image'])) { | |
$return = save_record_image($_FILES['record_image'],'test'); | |
echo $return['data']['url']; | |
} | |
?> | |
<form method="post" enctype="multipart/form-data"> | |
<input type="file" name="record_image" accept="image/*"> | |
<button type="submit">Guardar</button> | |
</form> |
It worked for what I needed, just a note is that in php 7 you no longer need to put "curl_setopt ($ ch, CURLOPT_SAFE_UPLOAD, false);" in the code
wow, its working...thanks
It is working .... Thanks
How to rename the uploaded file with original filename and mentioned suffix?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it does not work