Upload photos to IMGUR with watermark
Stored your website photo files on Imgur is not a bad idea.First of all, you need an account on Imgur, and you could create an password app for your upload script.
Here is the full code for this feature:
<?php
if(isset($_POST['submit'])){
$img = $_FILES['img'];
$localimg = basename($_FILES['img']['name']);
if($img['name'] == ''){
echo "<h2>Select an Image Please.</h2>";
} else {
// Path to store the uploaded image temporarily
$filename = $img['tmp_name'];
// Load image and add watermark
$watermark = imagecreatefrompng('path_to_your_watermark.png'); // Load your watermark image
$image = imagecreatefromstring(file_get_contents($filename));
// Set watermark position (adjust as needed)
$watermarkX = imagesx($image) - imagesx($watermark) - 10;
$watermarkY = imagesy($image) - imagesy($watermark) - 10;
// Apply watermark to the image
imagecopy($image, $watermark, $watermarkX, $watermarkY, 0, 0, imagesx($watermark), imagesy($watermark));
// Save watermarked image temporarily
$tempWatermarkedFile = tempnam(sys_get_temp_dir(), 'watermarked_img_');
imagepng($image, $tempWatermarkedFile);
imagedestroy($image);
imagedestroy($watermark);
// Prepare image data for upload to Imgur
$client_id = 'YOUR_IMGUR_CLIENT_ID';
$handle = fopen($tempWatermarkedFile, 'r');
$data = fread($handle, filesize($tempWatermarkedFile));
$pvars = array('image' => base64_encode($data));
fclose($handle);
// Upload watermarked image to Imgur
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$out = curl_exec($curl);
curl_close($curl);
// Process Imgur API response
$pms = json_decode($out, true);
$url = $pms['data']['link'];
if($url != ''){
echo "<h4 class='bg-success'>Uploaded With Watermark</h4>";
echo "<input type='text' id='image-link' value='" . substr($url, 0) . "' /><button onclick='copyToClipboard()'>Copy link</button><br/><hr/><h5>Preview : </h5>";
echo "<img id='imgur-image' alt='imgur-image' src='$url' />";
// Append to your URLs list
$fh = fopen("myurls.txt", "a+");
$value = $localimg . "|" . $url . "\r\n";
fwrite($fh, $value);
fclose($fh);
} else {
echo "<h4 class='bg-danger'>There’s a Problem</h4>";
echo "<div>" . $pms['data']['error'] . "</div>";
}
}
}
?>
Here is my demo: https://dulich.club/uppic.php
Page:
[1]