- 浏览: 1045200 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
nieanan:
感谢,很有帮助。
eclipse 改变JAVA_HOME路径 -
Orange_killer:
写的什么东西,文不对题
Hibernate Search大数据量手动建立索引 -
xiaoasha:
org.eclipse.equinox.servlet.api ...
《OSGI实战》遇到的问题 -
powertech:
写的挺细,有用!
SyntaxError: Non-ASCII character Python、Unicode和中文 -
huang_yong:
public class XMLUtil {
pri ...
XStream 去除生成的XML节点的class="list"
This a PHP Class useful if you need to download images from a remote location. You can choose between 2 methods to retrieve the image: using GD or cURL.
Here is the class (I will explain you how it works below):
- <?php
- class GetImage {
- var $source ;
- var $save_to ;
- var $set_extension ;
- var $quality ;
- function download( $method = 'curl' ) // default method: cURL
- {
- $info = @ GetImageSize ( $this ->source);
- $mime = $info [ 'mime' ];
- // What sort of image?
- $type = substr ( strrchr ( $mime , '/' ), 1);
- switch ( $type )
- {
- case 'jpeg' :
- $image_create_func = 'ImageCreateFromJPEG' ;
- $image_save_func = 'ImageJPEG' ;
- $new_image_ext = 'jpg' ;
- // Best Quality: 100
- $quality = isSet( $this ->quality) ? $this ->quality : 100;
- break ;
- case 'png' :
- $image_create_func = 'ImageCreateFromPNG' ;
- $image_save_func = 'ImagePNG' ;
- $new_image_ext = 'png' ;
- // Compression Level: from 0 (no compression) to 9
- $quality = isSet( $this ->quality) ? $this ->quality : 0;
- break ;
- case 'bmp' :
- $image_create_func = 'ImageCreateFromBMP' ;
- $image_save_func = 'ImageBMP' ;
- $new_image_ext = 'bmp' ;
- break ;
- case 'gif' :
- $image_create_func = 'ImageCreateFromGIF' ;
- $image_save_func = 'ImageGIF' ;
- $new_image_ext = 'gif' ;
- break ;
- case 'vnd.wap.wbmp' :
- $image_create_func = 'ImageCreateFromWBMP' ;
- $image_save_func = 'ImageWBMP' ;
- $new_image_ext = 'bmp' ;
- break ;
- case 'xbm' :
- $image_create_func = 'ImageCreateFromXBM' ;
- $image_save_func = 'ImageXBM' ;
- $new_image_ext = 'xbm' ;
- break ;
- default :
- $image_create_func = 'ImageCreateFromJPEG' ;
- $image_save_func = 'ImageJPEG' ;
- $new_image_ext = 'jpg' ;
- }
- if (isSet( $this ->set_extension))
- {
- $ext = strrchr ( $this ->source, "." );
- $strlen = strlen ( $ext );
- $new_name = basename ( substr ( $this ->source, 0, - $strlen )). '.' . $new_image_ext ;
- }
- else
- {
- $new_name = basename ( $this ->source);
- }
- $save_to = $this ->save_to. $new_name ;
- if ( $method == 'curl' )
- {
- $save_image = $this ->LoadImageCURL( $save_to );
- }
- elseif ( $method == 'gd' )
- {
- $img = $image_create_func ( $this ->source);
- if (isSet( $quality ))
- {
- $save_image = $image_save_func ( $img , $save_to , $quality );
- }
- else
- {
- $save_image = $image_save_func ( $img , $save_to );
- }
- }
- return $save_image ;
- }
- function LoadImageCURL( $save_to )
- {
- $ch = curl_init ( $this ->source);
- $fp = fopen ( $save_to , "wb" );
- // set URL and other appropriate options
- $options = array (CURLOPT_FILE => $fp ,
- CURLOPT_HEADER => 0,
- CURLOPT_FOLLOWLOCATION => 1,
- CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough)
- curl_setopt_array($ch , $options );
- curl_exec ( $ch );
- curl_close ( $ch );
- fclose($fp );
- }
- }
- ?>
<?php class GetImage { var $source; var $save_to; var $set_extension; var $quality; function download($method = 'curl') // default method: cURL { $info = @GetImageSize($this->source); $mime = $info['mime']; // What sort of image? $type = substr(strrchr($mime, '/'), 1); switch ($type) { case 'jpeg': $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; // Best Quality: 100 $quality = isSet($this->quality) ? $this->quality : 100; break; case 'png': $image_create_func = 'ImageCreateFromPNG'; $image_save_func = 'ImagePNG'; $new_image_ext = 'png'; // Compression Level: from 0 (no compression) to 9 $quality = isSet($this->quality) ? $this->quality : 0; break; case 'bmp': $image_create_func = 'ImageCreateFromBMP'; $image_save_func = 'ImageBMP'; $new_image_ext = 'bmp'; break; case 'gif': $image_create_func = 'ImageCreateFromGIF'; $image_save_func = 'ImageGIF'; $new_image_ext = 'gif'; break; case 'vnd.wap.wbmp': $image_create_func = 'ImageCreateFromWBMP'; $image_save_func = 'ImageWBMP'; $new_image_ext = 'bmp'; break; case 'xbm': $image_create_func = 'ImageCreateFromXBM'; $image_save_func = 'ImageXBM'; $new_image_ext = 'xbm'; break; default: $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; } if(isSet($this->set_extension)) { $ext = strrchr($this->source, "."); $strlen = strlen($ext); $new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext; } else { $new_name = basename($this->source); } $save_to = $this->save_to.$new_name; if($method == 'curl') { $save_image = $this->LoadImageCURL($save_to); } elseif($method == 'gd') { $img = $image_create_func($this->source); if(isSet($quality)) { $save_image = $image_save_func($img, $save_to, $quality); } else { $save_image = $image_save_func($img, $save_to); } } return $save_image; } function LoadImageCURL($save_to) { $ch = curl_init($this->source); $fp = fopen($save_to, "wb"); // set URL and other appropriate options $options = array(CURLOPT_FILE => $fp, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough) curl_setopt_array($ch, $options); curl_exec($ch); curl_close($ch); fclose($fp); } } ?>Variables Info var $source - The URL to the image (ex: http://www.domain.com/images/logo.jpg).
var $save_to - The path to the folder where the image will be saved (with trailing slash at the end)
var $set_extension - Are there any cases when the images you’re downloading have a wrong extension or do not have an extension at all? Consider turning this to “true”. Based on the mime type of the image, an extension is automatically assigned to the file.
var $quality - This is the 3rd argument that is passed to the image saver function. It is used for JPEG (from 0 to 100) or PNG (from 0 to 9) images.
How it works?
After initializing the class and the necessary variables are set, download() is called. Here the GetImageSize() function is used to obtain information regarding the image. We are interested in the mime-type from which he have to extract the type of image. Based on $type we will set some variables such: $image_create_func, $image_save_func, $new_image_ext (useful if $set_extension is set to ‘true’) and even $quality for ‘JPEG’ and ‘PNG’. The first 2 variables are needed in case we will use GD to download the image.
If $this->set_extension is not set, the saved image will have exactly the same name and extension (if any) as the remote image. If it is set, then we add $new_image_ext to the file name (could be the same as the remote one):
- if (isSet( $this ->set_extension))
- {
- $ext = strrchr ( $this ->source, "." );
- $strlen = strlen ( $ext );
- $new_name = basename ( substr ( $this ->source, 0, - $strlen )). '.' . $new_image_ext ;
- }
- else
- {
- $new_name = basename ( $this ->source);
- }
if(isSet($this->set_extension)) { $ext = strrchr($this->source, "."); $strlen = strlen($ext); $new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext; } else { $new_name = basename($this->source); }
Set the full path where the image will be saved (filename + save folder):
- $save_to = $this ->save_to. $new_name ;
$save_to = $this->save_to.$new_name;
Now, some functions would be called based on the $method value (could be ‘gd or ‘curl’):
- if ( $method == 'curl' )
- {
- $save_image = $this ->LoadImageCURL( $save_to );
- }
- elseif ( $method == 'gd' )
- {
- $img = $image_create_func ( $this ->source);
- if (isSet( $quality ))
- {
- $save_image = $image_save_func ( $img , $save_to , $quality );
- }
- else
- {
- $save_image = $image_save_func ( $img , $save_to );
- }
- }
if($method == 'curl') { $save_image = $this->LoadImageCURL($save_to); } elseif($method == 'gd') { $img = $image_create_func($this->source); if(isSet($quality)) { $save_image = $image_save_func($img, $save_to, $quality); } else { $save_image = $image_save_func($img, $save_to); } }
If we choose ‘gd’ the LoadImageCURL() function will be called with one argument, $save_to, representing the full path where the image would be saved.
The curl_unit() is called with the ‘url’ argument ($this->source). The session is initialized and returns a cURL handle for use with curl_setopt_array(), curl_exec() and curl_close() functions. The next thing to do is to open the remote image for writing using fopen():
- $ch = curl_init ( $this ->source);
- $fp = fopen ( $save_to , "wb" );
$ch = curl_init($this->source); $fp = fopen($save_to, "wb");
The function curl_setopt_array() sets multiple options for a cURL transfer without repetitively calling curl_setopt():
- // set URL and other appropriate options
- $options = array (CURLOPT_FILE => $fp ,
- CURLOPT_HEADER => 0,
- CURLOPT_FOLLOWLOCATION => 1,
- CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough)
- curl_setopt_array($ch , $options );
// set URL and other appropriate options $options = array(CURLOPT_FILE => $fp, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough) curl_setopt_array($ch, $options);
Now we need to grab the URL and pass it to the browser:
- curl_exec ( $ch );
curl_exec($ch);
The last things to do is closing both the cURL resource (to free up system resources) and the open file pointer:
- curl_close ( $ch );
- fclose($fp );
curl_close($ch); fclose($fp);
The image is now saved in the specified folder.
Now, let’s see how we can download the image using the ‘gd’ method! We’ll call the function needed to create the image from URL ($image_create_func). It’s used to return an image identifier representing the image obtained from the given URL:
- // possible values: ImageCreateFromJPEG, ImageCreateFromPNG etc.
- $img = $image_create_func ( $this ->source);
// possible values: ImageCreateFromJPEG, ImageCreateFromPNG etc. $img = $image_create_func($this->source);
The next function used ($image_save_func) is used to create the image from the given image ($img). Another aspect here is the $quality variable. For JPEG images, the value ranges from 0 (worst quality, smaller file) to 100 (best quality). The default is the default IJG quality value (about 75). As for the PNG images, the range is from 0 (no compression) to 9.
- if (isSet( $quality ))
- {
- // possible values: ImageJPEG, ImagePNG etc.
- $save_image = $image_save_func ( $img , $save_to , $quality );
- }
- else
- {
- $save_image = $image_save_func ( $img , $save_to );
- }
if(isSet($quality)) { // possible values: ImageJPEG, ImagePNG etc. $save_image = $image_save_func($img, $save_to, $quality); } else { $save_image = $image_save_func($img, $save_to); }
Here’s an example of how you can use this class:
- <?php
- include_once 'class.get.image.php' ;
- // initialize the class
- $image = new GetImage;
- $image ->source = 'http://l.yimg.com/a/i/ww/beta/y3.gif' ;
- $image ->save_to = 'images/' ; // with trailing slash at the end
- $get = $image ->download( 'gd' ); // using GD
- if ( $get )
- {
- echo 'The image has been saved.' ;
- }
- ?>
<?php include_once 'class.get.image.php'; // initialize the class $image = new GetImage; $image->source = 'http://l.yimg.com/a/i/ww/beta/y3.gif'; $image->save_to = 'images/'; // with trailing slash at the end $get = $image->download('gd'); // using GD if($get) { echo 'The image has been saved.'; } ?>
NOTE: Make sure the folder where the image will be saved is writable (chmod 0777).
Happy coding!
Tracback: http://www.bitrepository.com/web-programming/php/download-image.html发表评论
-
php文件保存
2009-11-07 22:29 1192今天安装了php5发现,访问php页面时浏览器把php当做文件 ... -
php多线程
2009-04-02 09:50 7569一直没有找到PHP有像JAVA一样的多线程机制,网上有的也只是 ... -
PHP获取文件创建时间和修改时间相关函数
2009-03-31 14:01 3067filemtime ( string filename ) ... -
PHP中合并数组的一个简单方法
2009-03-29 18:03 1579引用:http://www.21andy.com/blog/2 ... -
解决PHP file_exists 函数不支持中文文件
2009-03-25 15:59 3988今天想使用这一句php来判断一个文件是否存在: echo ... -
php读取二进制流(C语言结构体struct数据文件)
2009-03-24 19:14 13051尽管php是用C语言开发 ... -
使用php simple html dom parser解析html标签
2009-03-20 17:12 11320用了一下 PHP Simple HTML DOM Parser ... -
php过滤HTML代码
2009-03-20 17:06 7477strip_tags --- 去除字串中的HTML和PHP标签 ... -
CURL写的post、get类
2009-03-20 17:04 2304<?php class cURL { var $head ... -
让 php curl_exec直接返回字符串
2009-03-19 20:49 7809如果想php 的curl函数直接返回字符串,而不是存储到某个文 ... -
【转载】PHP CURL 参数详解
2009-03-05 15:35 28902008-03-26 18:27 curl用法:cook ... -
在PHP中使用Curl
2009-03-05 15:34 2789摘要: 在这篇文章中主要讲解php_curl库的知识,并 ... -
使用Eclipse PDT开发php
2008-11-19 11:58 1804首先到http://www.zend.com/ptd下载PDT ...
相关推荐
Spring REST is a practical guide for designing and developing RESTful APIs using the Spring Framework. This book walks you through the process of designing and building a REST application while taking...
c++封装了curl的get,post,download
经过多版本尝试,CURL 最后支持WINDOWS XP的版本是7.56.1,此资源包为SRC包,与编译BIN包,并且带有LIBSSH2与OPENSSL 1.1.1q,openssl 0.9.8, zlib1.2.12,在7.56.1以后的版本都已经不支持WINDOWS XP,因为其已经...
curl下载curl包下载
Windows users can download cURL from http://curl.haxx.se/download.html. You'll have to download the appropriate executable for your operating system, and I recommend putting it in the same directory ...
windows版curl命令 Startup: -V, --version display the version of Wget and exit. -h, --help print this help. -b, --background go to background after startup. -e, --execute=COMMAND execute a `....
curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令详解curl命令...
curl-8.4.0.tar.gz curl-8.4.0.zip
curl windows版,和linux下效果一样,可方便快速地进行http协议的调试,包括get、post、文件上传、下载等。
Curl命令可以通过命令行的方式,执行Http请求。...在官网处下载工具包:http://curl.haxx.se/download.html 下载好后解压 配置环境变量 CURL_HOME指到文件目录 path指到bin目录 在cmd中输入curl --help
curl_docker_image如有任何疑问,疑问或疑虑,请发送至: curldocker@blairjames.com 功能齐全的Curl实现。 精益求精。 CI / CD的构建,监视和维护。 从最新的稳定来源最新编译: : 像本地curl一样docker run ,只需...
Everything curl is an extensive guide to everything there is to know about curl, the project, the command-line tool, the library, how everything started and how it came to be what it is today. How we ...
curl工具,可以方便调试url,简单易用的url调试工具
windows下C开发CURL的Demo
Elasticsearch in Action teaches you how to build scalable search applications using Elasticsearch. You'll ramp up fast, with an informative overview and an engaging introductory example. Within the ...
Everything curl is an extensive guide to everything there is to know about curl, the project, the command-line tool, the library, how everything started and how it came to be what it is today. How we ...
4.15的版本的www-curl,老一点的版本,根据自己选择下载
curl7.52.1库
Curl is a command-line tool for transferring data specified with URL syntax. Find out how to use curl by reading [the curl.1 man page](https://curl.haxx.se/docs/manpage.html) or [the MANUAL document]...