


Use the gd library in php to download all images in the web page, _PHP tutorial
The gd library is used in php to download all the pictures in the web page.
In the early php tutorial, it was mentioned that the php gd library can realize the download of remote pictures, but that is just downloading The principle is the same for a picture. If you want to download all the pictures of a web page, you only need to use regular expressions to judge. Find all the picture URLs and then download them in a loop. I specially wrote the gd library picture download class with reference to network resources. !
The php code is as follows:
<?php header("Content-type:text/html ; charset=utf-8"); if (!empty($_POST['submit'])){ $url = $_POST['url']; //为了获取相对路径的图片所做的操作 $url_fields = parse_url($url); $main_url = $url_fields['host']; $base_url = substr($url,0,strrpos($url, '/')+1); //获取网页内容 //设置代理服务器 $opts = array('http'=>array('request_fulluri'=>true)); $context = stream_context_create($opts); $content = file_get_contents($url,false,$context); //匹配img标签,将所有匹配字符串保存到数组$matches $reg = "/<img src="/static/imghwm/default1.png" data-src="http://www.bkjia.com/uploads/allimg/150513/222Q559A-0.jpg?201541210942?x-oss-process=image/resize,p_40" class="lazy" .*?src=\"(.*?)\".*? alt="Use the gd library in php to download all images in the web page, _PHP tutorial" >/i"; preg_match_all($reg, $content, $matches); $count = count($matches[0]); for ($i=0; $i<$count; $i++){ /*将所有图片的url转换为小写 *$matches[1][$i] = strtolower($matches[1][$i]); */ //如果图片为相对路径就转化为全路径 if (!strpos('a'.$matches[1][$i], 'http')){ //因为'/'是第0个位置 if (strpos('a'.$matches[1][$i], '/')){ $matches[1][$i] = 'http://'.$main_url.$matches[1][$i]; }else{ $matches[1][$i] = $base_url.$matches[1][$i]; } } } //过滤重复的图片 $img_arr = array_unique($matches[1]); //实例化图片下载类 $getImg = new DownImage(); $url_count = count($img_arr); for ($i=0; $i<$url_count; $i++){ $getImg->source = $img_arr[$i]; $getImg->save_address = './pic/'; $file = $getImg->download(); } echo "下载完成!哈哈,简单吧!"; } class DownImage{ public $source;//远程图片URL public $save_address;//保存本地地址 public $set_extension; //设置图片扩展名 public $quality; //图片的质量(0~100,100最佳,默认75左右) //下载方法(选用GD库图片下载) public function download(){ //获取远程图片信息 $info = @getimagesize($this->source); //获取图片扩展名 $mime = $info['mime']; $type = substr(strrchr($mime, '/'), 1); //不同的图片类型选择不同的图片生成和保存函数 switch($type){ case 'jpeg': $img_create_func = 'imagecreatefromjpeg'; $img_save_func = 'imagejpeg'; $new_img_ext = 'jpg'; $image_quality = isset($this->quality) ? $this->quality : 100; 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 (isset($this->set_extension)){ $ext = strrchr($this->source,"."); $strlen = strlen($ext); $newname = basename(substr($this->source,0,-$strlen)).'.'.$new_img_ext; }else{ $newname = basename($this->source); } //生成本地文件路径 $save_address = $this->save_address.$newname; $img = @$img_create_func($this->source); if (isset($image_quality)){ $save_img = @$img_save_func($img,$save_address,$image_quality); }else{ $save_img = @$img_save_func($img,$save_address); } return $save_img; } } ?> <form method="POST" action=""> 远程url地址:<input type="text" name="url" size=30 /> <input type="submit" name="submit" value="下载该页面所有图片" /> </form>
The running results are as shown below:

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools