Because today want to write a remote download pictures of the class, in advance to write a PHP GD library to achieve remote image download function, of course, curl to achieve better, PHP GD library remote image download function mainly using the GD library two function imagecreatefromxxx () Used to generate picture functions and imagexxx functions, XXX represents the extension of different pictures, so you have to find a way to get the extension of the remote image, with the following PHP code:
<?phpheader ("content-type:text/html; Charset=utf-8 "); if (!empty ($_post[' submit ')) {$url = $_post[' url '); $pictureName = $_post[' Picturename ']; $img = Getpicture ($url, $ Picturename); Echo '
'; }function getpicture ($url, $pictureName) {if ($url = = "") return false; Gets the image extension $info = getimagesize ($url); $mime = $info [' MIME ']; $type = substr (STRRCHR ($mime, '/'), 1); Different picture types choose different picture generation and save function switch ($type) {case ' jpeg ': $img _create_func = ' imagecreatefromjpeg '; $img _save_func = ' imagejpeg '; $new _img_ext = ' jpg '; Break Case ' png ': $img _create_func = ' imagecreatefrompng '; $img _save_func = ' imagepng '; $new _img_ext = ' png '; Break Case ' BMP ': $img _create_func = ' imagecreatefrombmp '; $img _save_func = ' imagebmp '; $new _img_ext = ' bmp '; Break Case ' gif ': $img _create_func = ' imagecreatefromgif '; $img _save_func = ' imagegif '; $new _img_ext = ' gif '; Break Case ' vnd.wap.wbmp ': $img _create_func = ' imagecreatefromwbmp '; $img _save_func = ' imagewbmp '; $new _img_ext = ' bmp '; Break Case ' XBM ': $img _create_func = ' IMAGECREATEFROMXBM '; $img _save_func = ' IMAGEXBM '; $new _img_ext = ' XBM '; Break Default: $img _create_func = ' imagecreatefromjpeg '; $img _save_func = ' imagejpeg '; $new _img_ext = ' jpg '; } if ($pictureName = = "") {$pictureName = time (). ". {$new _img_ext} "; }else{$pictureName = $pictureName. ". {$new _img_ext} "; } $src _im = $img _create_func ($url); Create a new picture by URL $img _save_func ($src _im, $pictureName); Output file to file return $pictureName;} ?>
Running results such as: (Picture automatically saved in the current file directory, do not understand can leave a message)