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
Previous
Next Post »
Thanks for your comment