Skip to main content

How to Import and Compress Image from URL with PHP

Import and Compress Image  from URL

How to import image image from URL then compress
Demo-Import and compress

Step1-create a index.php file

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <title>Save Image and Resize</title>
  </head>
  <body>
    <div class="container">
      <div class="card mt-3">
        <div class="card-header">
          <h3>Save Image</h3>
        </div>
        <div class="card-body">
          <form action="<?=$_SERVER['PHP_SELF']; ?>" method="POST">
            <div class="form-group">
              <label for="url">Image URL</label>
              <input type="text" id="url" name="url" class="form-control">
            </div>
            <div class="form-group">
              <label for="url">Size Ratio</label>
              <input type="text" id="ratio" name="ratio" class="form-control" value="90">
            </div>
            <button type="submit" name="submit" class="btn btn-info rounded-0">Save and Compress</button>
          </form>
        </div>
        <?php
        if(isset($_POST['submit'])):
        ?>
        <div class="card-body">
          <?php
          function compress($source, $destination, $quality = 50) {
          $info = getimagesize($source);
          if ($info['mime'] == 'image/jpeg')
          $image = imagecreatefromjpeg($source);
          elseif ($info['mime'] == 'image/gif')
          $image = imagecreatefromgif($source);
          elseif ($info['mime'] == 'image/png')
          $image = imagecreatefrompng($source);
          imagejpeg($image, $destination, $quality);
          return $destination;
          }
          $error = false; // flag
          $allowed_extenstions = ['jpg', 'jpeg', 'png', 'gif']; // array
          $local_path  = 'images/';
          if( !file_exists( $local_path)) {
          mkdir($local_path);
          }
          $image_url = $_POST['url'] ?? false;
          $image_args = explode('/', $image_url);
          $image_args = explode('.', end($image_args));
          $image_name = $image_args[0] ?? time() . 'Untitled';
          $image_extention = $image_args[1] ?? false;
          if( !in_array($image_extention, $allowed_extenstions)){
          $error = true;
          echo 'This url doesn\'t contain any image';
          }
          if($error === false){
          $new_name = file_exists($local_path . $image_name . '.' . $image_extention) ?  time() . $image_name : $image_name;
          $ch = curl_init($image_url);
          $fp = fopen($local_path . $new_name . '.' . $image_extention, 'wb');
          curl_setopt($ch, CURLOPT_FILE, $fp);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_exec($ch);
          if($ch){
          ?>
          <h3>Image Uploaed Successfully!</h3>
          <img src="<?php echo $local_path . $new_name . '.'. $image_extention; ?>" alt="" class="img-fluid w-100"><br>
          <?php
          echo 'Size : ' . round(filesize($local_path . $new_name . '.'. $image_extention) / 1024, 2 ). ' kb <br>';
          } else {
          echo 'Couldn\'t be Uploaded';
          }
          curl_close($ch);
          fclose($fp);
          // resize
          if(file_exists( $local_path . $new_name . '.' . $image_extention)) {
          $ratio = $_POST['ratio'] ?? 80;
          $is_compress = compress( $local_path . $new_name . '.' . $image_extention , $local_path . $new_name . '.min.'. $image_extention, $ratio);
          if($is_compress == true) {
          echo '<br>Compress Successfully<br>';
          echo 'New Size is ' . round(filesize($local_path . $new_name . '.min.'. $image_extention) / 1024, 2 ). ' kb';
          } else {
          echo 'Couldn\'t be compressed';
          }
          }
          }
          ?>
        </div>
        <?php endif; ?>
      </div>
    </div>
    <footer>
      <p class="text-muted text-center p-2">&copy; Bhupender</p>
    </footer>
  </body>
</html

Import and Compress Image From URL

Comments

Popular posts from this blog

How to Download Vimeo Thumbnail With PHP

Vimeo Thumbnail Downloader Tool – Vimeo Video Ka Thumbnail Download Karein Aaj ke is tutorial mein hum Vimeo Thumbnail Downloader Tool ke baare mein jaanenge. Is tool ki help se hum Vimeo video URL se us video ki thumbnail image obtain kar sakte hain. Agar aap PHP, Web Development, HTML, CSS aur Online Tools banana seekh rahe hain, to ye ek useful PHP project example hai. Vimeo Thumbnail Downloader Kya Hai? Vimeo Thumbnail Downloader ek simple tool hai jisme user Vimeo video ka URL enter karta hai. Tool Vimeo video ID ko identify karta hai aur available thumbnail image ko display karta hai. Example Vimeo URL: https://vimeo.com/389063954 Is URL mein: 389063954 Vimeo video ID hai. Vimeo Thumbnail Downloader Kaise Work Karta Hai? Is PHP script mein mainly do functions use kiye gaye hain: 1. getVimeoId() Ye function Vimeo URL se video ID find karta hai. 2. getVimeoThumb() Ye function Vimeo video ID ke basis par thumbnail information retrieve karta hai aur available thumbnail...

How To Create Contact Form With Validation-Php & Html

How To Create Contact Form - Hello Friends Ajj Me Apko Btaunga   Ki Kis Prakar   Ham Apni Website Ke Liye Contact Form Bnana Sakta Hai.( By Using   Php Contact Form With Validation Script ) Contact Form Hmare Website Ke Liye Bhut He Jrruri Hota Hai.Contact Form Ke Help Se He User Apna Message Website Owner(Admin) Ko Bhej Sakte Hai. Ajj Me Apko Btane Wala Hu Ki Kis Parkar Ham Contact Form Banakar Apne Website Me Add Kar Sakte Hai Or Apni Website Ke Liye Contact Form Bana Sakte Hai Is Contact Form Me   Ham Php Mail Function() Ka Use Karege . Ab Jab Be Koi User Contact Form Me Apni Information ,Message Details Fill Karke Submit Karega To Uski Sare Information Website Admin Ke Email Id Par A Jaiyegi Jo Be Admin Dwara   Niche Diye Gye Code Me Setup Ki Hogi. Used Language- ·          HTML ·          Jquery ·          Php ...

How To Send Sms From Website Using Php-Textlocal SMS

Hello Friends Ajj Me Apko Btaunga Ki Kis Prakar Aap Apni Website Me Free Sms Send Ka Option Integrate Kar Sakte Ho. Bulk sms in India-Best company Iske Liye Apko Sabse Pehle   Https://Www.Textlocal.In/Signup Par Signup Karke Account Create Karna Hai.Ye Ek Achi bulk sms provider company hai ·          Account Create Hone Ke Bad Aap Apna Username And Password Enter Karke Apne Dashboard Me Login Ho Jaiye( textlocal login) ·          Yanha Apko Text Local Company Dwara 10 Meassge Free Testing Ke Liye Diye Jayege ·          Aap Sms Plan Buy Kar Sakte Ho Is Company Dwara Bhut He Kam Prize Me Sms Pakage,plan provide  Kiye Jate Hai Textlocal Price Plans –  index.php <?php if(isset($_POST['code']) &&  isset($_POST['mobile'])){ // Authorisation details. $username = "enter your textlocal account email id here"; $hash = "...