Please selectSwitch to Mobile Version | Continue to Visit the Desktop Version

 Forgot Password
 Register
View: 2253|Reply: 0

Upload photos to IMGUR with watermark

[Copy Link]
Posted on 2024-04-10 22:57:24 | Show all floors |Read Mode
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:

  1. <?php
  2. if(isset($_POST['submit'])){
  3.     $img = $_FILES['img'];
  4.     $localimg = basename($_FILES['img']['name']);

  5.     if($img['name'] == ''){  
  6.         echo "<h2>Select an Image Please.</h2>";
  7.     } else {
  8.         // Path to store the uploaded image temporarily
  9.         $filename = $img['tmp_name'];

  10.         // Load image and add watermark
  11.         $watermark = imagecreatefrompng('path_to_your_watermark.png'); // Load your watermark image
  12.         $image = imagecreatefromstring(file_get_contents($filename));

  13.         // Set watermark position (adjust as needed)
  14.         $watermarkX = imagesx($image) - imagesx($watermark) - 10;
  15.         $watermarkY = imagesy($image) - imagesy($watermark) - 10;

  16.         // Apply watermark to the image
  17.         imagecopy($image, $watermark, $watermarkX, $watermarkY, 0, 0, imagesx($watermark), imagesy($watermark));

  18.         // Save watermarked image temporarily
  19.         $tempWatermarkedFile = tempnam(sys_get_temp_dir(), 'watermarked_img_');
  20.         imagepng($image, $tempWatermarkedFile);
  21.         imagedestroy($image);
  22.         imagedestroy($watermark);

  23.         // Prepare image data for upload to Imgur
  24.         $client_id = 'YOUR_IMGUR_CLIENT_ID';
  25.         $handle = fopen($tempWatermarkedFile, 'r');
  26.         $data = fread($handle, filesize($tempWatermarkedFile));
  27.         $pvars = array('image' => base64_encode($data));
  28.         fclose($handle);

  29.         // Upload watermarked image to Imgur
  30.         $timeout = 30;
  31.         $curl = curl_init();
  32.         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  33.         curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
  34.         curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  35.         curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
  36.         curl_setopt($curl, CURLOPT_POST, 1);
  37.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  38.         curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);

  39.         $out = curl_exec($curl);
  40.         curl_close($curl);

  41.         // Process Imgur API response
  42.         $pms = json_decode($out, true);
  43.         $url = $pms['data']['link'];

  44.         if($url != ''){
  45.             echo "<h4 class='bg-success'>Uploaded With Watermark</h4>";
  46.             echo "<input type='text' id='image-link' value='" . substr($url, 0) . "' /><button onclick='copyToClipboard()'>Copy link</button><br/><hr/><h5>Preview : </h5>";
  47.             echo "<img id='imgur-image' alt='imgur-image' src='$url' />";
  48.             
  49.             // Append to your URLs list
  50.             $fh = fopen("myurls.txt", "a+");
  51.             $value = $localimg . "|" . $url . "\r\n";
  52.             fwrite($fh, $value);
  53.             fclose($fh);

  54.         } else {
  55.             echo "<h4 class='bg-danger'>There’s a Problem</h4>";
  56.             echo "<div>" . $pms['data']['error'] . "</div>";  
  57.         }
  58.     }
  59. }
  60. ?>
Copy Code
Here is my demo: https://dulich.club/uppic.php
Reply

Use Props Report

You need to log in before you can reply Login | Register

Forum Credit Rules

Close

Editors SelectedPrevious /2 Next

Archiver|Mobile|Darkroom|Privacy|DSC

GMT+7, 2024-12-05 08:19 , Processed in 0.012145 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

Quick Reply Back to Top Return to List