How to save the image from url directly

Hi, In this tutorial, I am going to teach you how to save the image from url directly.

Suppose in your web application user picture to be taken from the external URL. for example if a user signup / login using facebook in this case user profile picture url is return from the facebook. then you have to save the image from the external source in this situation we can use this logic.

saveimage.php
<?php  
 error_reporting(0);  
 function check_cURL()  
 {  
   if (in_array('curl', get_loaded_extensions())) {  
     return true;  
   } else {  
     return false;  
   }  
 }  
 function mime_content_type($filename)  
 {  
   $mime_types = array(  
     'txt' => 'text/plain',  
     'htm' => 'text/html',  
     'html' => 'text/html',  
     'php' => 'text/html',  
     'css' => 'text/css',  
     'js' => 'application/javascript',  
     'json' => 'application/json',  
     'xml' => 'application/xml',  
     'swf' => 'application/x-shockwave-flash',  
     'flv' => 'video/x-flv',  
     // images  
     'png' => 'image/png',  
     'jpe' => 'image/jpeg',  
     'jpeg' => 'image/jpeg',  
     'jpg' => 'image/jpeg',  
     'gif' => 'image/gif',  
     'bmp' => 'image/bmp',  
     'ico' => 'image/vnd.microsoft.icon',  
     'tiff' => 'image/tiff',  
     'tif' => 'image/tiff',  
     'svg' => 'image/svg+xml',  
     'svgz' => 'image/svg+xml',  
     // archives  
     'zip' => 'application/zip',  
     'rar' => 'application/x-rar-compressed',  
     'exe' => 'application/x-msdownload',  
     'msi' => 'application/x-msdownload',  
     'cab' => 'application/vnd.ms-cab-compressed',  
     // audio/video  
     'mp3' => 'audio/mpeg',  
     'qt' => 'video/quicktime',  
     'mov' => 'video/quicktime',  
     // adobe  
     'pdf' => 'application/pdf',  
     'psd' => 'image/vnd.adobe.photoshop',  
     'ai' => 'application/postscript',  
     'eps' => 'application/postscript',  
     'ps' => 'application/postscript',  
     // ms office  
     'doc' => 'application/msword',  
     'rtf' => 'application/rtf',  
     'xls' => 'application/vnd.ms-excel',  
     'ppt' => 'application/vnd.ms-powerpoint',  
     // open office  
     'odt' => 'application/vnd.oasis.opendocument.text',  
     'ods' => 'application/vnd.oasis.opendocument.spreadsheet'  
   );  
   $ext    = strtolower(array_pop(explode('.', $filename)));  
   if (array_key_exists($ext, $mime_types)) {  
     return $mime_types[$ext];  
   } elseif (function_exists('finfo_open')) {  
     $finfo  = finfo_open(FILEINFO_MIME);  
     $mimetype = finfo_file($finfo, $filename);  
     finfo_close($finfo);  
     return $mimetype;  
   } else {  
     return 'application/octet-stream';  
   }  
 }  
 function getimg($url)  
 {  
   $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';  
   $headers[] = 'Connection: Keep-Alive';  
   $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';  
   $useragent = 'php';  
   $process  = curl_init($url);  
   curl_setopt($process, CURLOPT_HTTPHEADER, $headers);  
   curl_setopt($process, CURLOPT_HEADER, 0);  
   curl_setopt($process, CURLOPT_USERAGENT, $useragent);  
   curl_setopt($process, CURLOPT_TIMEOUT, 30);  
   curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);  
   curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);  
   $return = curl_exec($process);  
   curl_close($process);  
   return $return;  
 }  
 function check_url_exist($url)  
 {  
   $handle = curl_init($url);  
   if (false === $handle) {  
     return false;  
   }  
   curl_setopt($handle, CURLOPT_HEADER, false);  
   curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works  
   curl_setopt($handle, CURLOPT_NOBODY, true);  
   curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);  
   $connectable = curl_exec($handle);  
   curl_close($handle);  
   return $connectable;  
 }  
 if (isset($_POST['link'])) {  
   if (check_cURL()) {  
     $imgurl = $_POST['link'];  
     if (check_url_exist($imgurl)) {  
       $file_type = mime_content_type($imgurl);  
       if (preg_match("/image/i", $file_type)) {  
         $imagename = basename($imgurl);  
         if (file_exists('./tmp/' . $imagename)) {  
           $text_message = 'This file exist!';  
         } else {  
           $image = getimg($imgurl);  
           file_put_contents('tmp/' . $imagename, $image);  
           $text_message = 'This file saved! Click <a href="./tmp/' . $imagename . '" target="_blank">here</a> to see your image.';  
         }  
       } else {  
         $text_message = 'This file is not an image. Please check again!';  
       }  
     } else {  
       $text_message = 'This link is a broken link. Please check again!';  
     }  
   } else {  
     $text_message = "cURL is NOT <span style=\"color:red\">installed</span> on this server";  
   }  
 }  
 ?>  
 <html>  
      <head>  
           <title>Save image from url directly on your server</title>  
           <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
           <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">  
           <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
           <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>  
      </head>  
      <body>  
           <div class="container">  
                <div class="row">  
                     <p align="center"><span style="font-size:30px">SAVE IMAGE</span></p>  
                     <p align="center">We only support for image format.</p>  
                     <div align="center" class="col-sm-16" style="margin: 20px 0px;">  
                          <?php  
 if (isset($_POST['link'])) {  
 ?>                                
                               <p style="color: red;font-size: 20px;"><?php  
   echo $text_message;  
 ?></p>  
                          <?php  
 }  
 ?>  
                          <form method="post" class="link_video">  
                               <label for="link-image">Link:</label> <input style="width:300px;" type="text" name="link" id="link-image" value="" placeholder="image.jpg">  
                               <input type="submit" value="GRAB" class="btn btn-success">  
                          </form>  
                     </div>  
                </div>  
           </div>  
      </body>  
 </html>  

Download
* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff


EmoticonEmoticon