{"id":1146,"date":"2012-10-24T23:37:16","date_gmt":"2012-10-24T23:37:16","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1146"},"modified":"2012-10-24T23:40:40","modified_gmt":"2012-10-24T23:40:40","slug":"how-to-save-remote-image-in-server-or-save-image-from-a-url","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2012\/10\/how-to-save-remote-image-in-server-or-save-image-from-a-url\/","title":{"rendered":"How to save remote image in server or save image from a url?"},"content":{"rendered":"<p>Saving images from any live url is very easy with php.<br \/>\nHere  are three ways to do this.<br \/>\n1.\tUsing Curl<br \/>\n2.\tUsing File functions<br \/>\n3.\tUsing GD library functions. <\/p>\n<p>Condition is in your server php.ini should have \u201callow_url_fopen\u201d On. check you php setting using phpinfo() function.<\/p>\n<p>Create a folder name &#8220;images_saved&#8221; to save your images.<\/p>\n<p><strong>Using Curl <\/strong><\/p>\n<pre lang=\"php\">\r\n<?php\r\n$my_img[]='http:\/\/images.askmen.com\/galleries\/cobie-smulders\/picture-1-134686094208.jpg';\r\n \r\n$my_img[]='http:\/\/images.askmen.com\/galleries\/demi-lovato\/picture-1-134212249378.jpg';\r\n \r\n $fullpath = \"images_saved\";\r\n\r\nforeach($my_img as $i){\r\n    image_save_from_url($i,$fullpath);\r\n\r\n    if(getimagesize($fullpath.\"\/\".basename($i))){\r\n        echo '<h3 style=\"color: green;\">Image ' . basename($i) . ' Downloaded Successfully<\/h3>';\r\n    }else{\r\n        echo '<h3 style=\"color: red;\">Image ' . basename($i) . ' Download Failed<\/h3>';\r\n    }\r\n}\r\n \r\nfunction image_save_from_url($my_img,$fullpath){\r\n    if($fullpath!=\"\" && $fullpath){\r\n        $fullpath = $fullpath.\"\/\".basename($my_img);\r\n    }\r\n    $ch = curl_init ($my_img);\r\n    curl_setopt($ch, CURLOPT_HEADER, 0);\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);\r\n    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);\r\n    $rawdata=curl_exec($ch);\r\n    curl_close ($ch);\r\n    if(file_exists($fullpath)){\r\n        unlink($fullpath);\r\n    }\r\n    $fp = fopen($fullpath,'x');\r\n    fwrite($fp, $rawdata);\r\n    fclose($fp);\r\n}\r\n?>\r\n<\/pre>\n<p><strong>Using File functions<\/strong><\/p>\n<pre lang=\"php\">\r\n<?php \r\nfunction save_from_url($inPath,$outPath)\r\n{ \/\/Download images from remote server\r\n    $in=    fopen($inPath, \"rb\");\r\n    $out=   fopen($outPath, \"wb\");\r\n    while ($chunk = fread($in,8192))\r\n    {\r\n        fwrite($out, $chunk, 8192);\r\n    }\r\n    fclose($in);\r\n    fclose($out);\r\n}\r\n\r\nsave_from_url('http:\/\/yousite.com\/image1.jpg','your_new_image.jpg');\r\n?>\r\n<\/pre>\n<p><strong>Using GD library functions. <\/strong><\/p>\n<pre lang=\"php\">\r\n<?php\r\n$my_server_img = 'http:\/\/images.askmen.com\/galleries\/cobie-smulders\/picture-1-134686094208.jpg';\r\n$img = imagecreatefromjpeg($my_server_img);\r\n$path = 'images_saved\/';\r\nimagejpeg($img, $path);\r\n?>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Saving images from any live url is very easy with php. Here are three ways to do this. 1. Using Curl 2. Using File functions 3. Using GD library functions. Condition is in your server php.ini should have \u201callow_url_fopen\u201d On. check you php setting using phpinfo() function. Create a folder name &#8220;images_saved&#8221; to save your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1146","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1146"}],"version-history":[{"count":3,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1146\/revisions"}],"predecessor-version":[{"id":1149,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1146\/revisions\/1149"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}