当前位置:网站首页>What are the ways to download network pictures with PHP
What are the ways to download network pictures with PHP
2022-07-06 07:46:00 【Brother Jing】
Method 1
advantage : Simple .
shortcoming : Relatively inefficient .
This method mainly uses two functions file_get_contents as well as file_put_contents This method is very simple , Generally speaking, first get To picture content , then put To a file .
The code is as follows :<?php
$imgFile = 'http://***.***.***/Uploads/20200424/1587710975CZlGeB.jpg';
// Get the file name
$imgName = trim(strrchr($imgFile, '/'), '/');
file_put_contents($imgName, file_get_contents($imgFile));
Method 2
The method most used by individuals .
shortcoming : A little more complicated , To master curl Usage of , Need to know curl The meaning of some options .
advantage : Efficient , It's easy to support https;
curl Very powerful , It's also phper Skills that must be mastered , Therefore, it is recommended that you use this method . The idea of this method is , Use curl Tools to get information about remote pictures , Then save the information to a local file . I'm not right here curl_exec The result of is verified , Steal laziness . About curl Usage of , If you want to know more , Can be in Gxlcms Mid search curl.$imgFile = 'https://***.***.***/uploads/20200121/bd873d945595.png';
function down_img($imgUrl)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imgUrl); // Request address
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Do not output information directly
// You can visit https request
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// perform curl And get the data
$results = curl_exec($ch);
$imgName = trim(strrchr($imgUrl, '/'), '/');
$fp = fopen($imgName, 'w');
fwrite($fp, $results);
// After execution , Remember to close resources
curl_close($ch);
}
down_img($imgFile);
Method 3
It's also very simple , What is used is php Knowledge of document management . First Create two file handles , A binary information for reading remote pictures , Another one is used to save picture information .<?php
$imgFile = '
$imgName = trim(strrchr($imgFile, '/'), '/');
// Read remote image resources
$source = fopen($imgFile, 'rb');
// Create a local file resource handle , Used to save picture information
$down = fopen($imgName, 'wb');
while ($chunk = fread($source,1024)){
fwrite($down, $chunk, 1024);
}
fclose($source);
fclose($down);
边栏推荐
- Iterator Foundation
- Simulation of Teman green interferometer based on MATLAB
- (lightoj - 1410) consistent verbs (thinking)
- 数字经济时代,如何保障安全?
- Typescript function definition
- The way to learn go (I) the basic introduction of go to the first HelloWorld
- CF1036C Classy Numbers 题解
- shu mei pai
- Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics
- Hackathon ifm
猜你喜欢
Solution: système de surveillance vidéo intelligent de patrouille sur le chantier
[nonlinear control theory]9_ A series of lectures on nonlinear control theory
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
TS 类型体操 之 循环中的键值判断,as 关键字使用
Google可能在春节后回归中国市场。
Pangolin Library: control panel, control components, shortcut key settings
[computer skills]
esRally国内安装使用避坑指南-全网最新
数据治理:主数据的3特征、4超越和3二八原则
Leecode-c language implementation -15 Sum of three ----- ideas to be improved
随机推荐
shu mei pai
Redis list detailed explanation of character types yyds dry goods inventory
合规、高效,加快药企数字化转型,全新打造药企文档资源中心
MEX有关的学习
. Net 6 learning notes: what is NET Core
js對象獲取屬性的方法(.和[]方式)
TS 体操 &(交叉运算) 和 接口的继承的区别
链表面试题(图文详解)
opencv学习笔记九--背景建模+光流估计
Scala语言学习-08-抽象类
[MySQL learning notes 30] lock (non tutorial)
[count] [combined number] value series
octomap averageNodeColor函数说明
edge瀏覽器 路徑獲得
Mise en œuvre du langage leecode - C - 15. Somme des trois chiffres - - - - - idées à améliorer
Document 2 Feb 12 16:54
HTTP cache, forced cache, negotiated cache
QT color is converted to string and uint
Typescript interface and the use of generics
Sharing of source code anti disclosure scheme under burning scenario