Home > php教程 > php手册 > php中使用gd库实现远程图片下载实例

php中使用gd库实现远程图片下载实例

WBOY
Release: 2016-06-06 20:04:15
Original
1464 people have browsed it

这篇文章主要介绍了php中使用gd库实现远程图片下载实例,本文直接给出实现代码,需要的朋友可以参考下

因为今天想写个远程下载图片的类,提前热身写了个php gd库实现远程图片下载功能,当然curl实现更好,,php gd库实现远程图片下载功能主要使用gd库的两个函数ImageCreateFromXXX()用于生成图片函数和ImageXXX函数,XXX表示不同图片的扩展名,所以你要想办法获取远程图片的扩展名了,附上php代码如下:

php中使用gd库实现远程图片下载实例'; } function getPicture($url,$pictureName){ if ($url == "") return false; //获取图片的扩展名 $info = getimagesize($url); $mime = $info['mime']; $type = substr(strrchr($mime,'http://www.jb51.net/'), 1); //不同的图片类型选择不同的图片生成和保存函数 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); //由url创建新图片 $img_save_func($src_im, $pictureName); //输出文件到文件 return $pictureName; } ?>

远程url地址:
文件名称:

运行结果如下图:(图片自动保存在当前文件目录,不明白的可以留言)

php中使用gd库实现远程图片下载实例


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template