Home  >  Article  >  Backend Development  >  Example of remote image download using gd library in PHP, _PHP tutorial

Example of remote image download using gd library in PHP, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:54:10800browse

An example of using the gd library in php to implement remote image downloading.

Because I wanted to write a class for remote image downloading today, I warmed up in advance and wrote a php gd library to implement the remote image downloading function. , of course curl is better implemented. The PHP gd library implements the remote image download function mainly using the two functions of the gd library, ImageCreateFromXXX(), to generate image functions and ImageXXX functions. XXX represents the extensions of different images, so you have to find a way to obtain the remote image download function. The extension of the image is attached, and the php code is attached as follows:

<&#63;php
header("Content-type:text/html ; charset=utf-8");
 
if (!empty($_POST['submit'])){
 $url = $_POST['url'];
 $pictureName = $_POST['pictureName'];
 $img = getPicture($url,$pictureName);
 echo '<pre class="brush:php;toolbar:false"><img src="'.$img.'">
'; } function getPicture($url,$pictureName){ if ($url == "") return false; //获取图片的扩展名 $info = getimagesize($url); $mime = $info['mime']; $type = substr(strrchr($mime,'/'), 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地址:
文件名称:

The running result is as follows: (The picture is automatically saved in the current file directory, if you don’t understand, you can leave a message)


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/998565.htmlTechArticleUsing the gd library in php to implement remote image downloading examples, because today I want to write a remote image downloading class to warm up in advance I wrote a php gd library to implement the remote image download function. Of course, curl implementation is better...
Statement:
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