gd ライブラリの画像ダウンロード クラスは、Web ページ上のすべての画像をダウンロードするための PHP コードを実装します。
Jun 13, 2016 am 11:59 AM
PHP コードは次のとおりです:
コードをコピー コードは次のとおりです:
<?php
header ("コンテンツタイプ: 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);
//Web コンテンツを取得
//プロキシ サーバーを設定
$opts = array('http'=>array('request_fulluri'= >true) );
$context = stream_context_create($opts);
$content = file_get_contents($url,false,$context);
//img タグを照合し、一致する文字列をすべて保存します配列 $matches
$reg = "//i";
preg_match_all($reg, $content, $matches) ;
$count = count($matches[0]);
for ($i=0; $i/*すべての画像 URL を小文字に変換します
*$matches[1][$i] = strto lower($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$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';
$new_img_ext = 'jpg';
$image_quality = isset($this->quality); $this-> 品質 : 100;
break;
ケース 'png':
$img_save_func = 'imagepng'; ';
ブレーク;
ケース 'bmp':
$img_save_func = 'imagebmp';
ケース ' gif':
$img_create_func = 'imagegif';
$new_img_ext = 'gif';
ケース 'vnd. wap.wbmp':
$img_create_func = 'imagecreatefromwbmp';
$new_img_ext = 'bmp';
case 'xbm'; >$img_create_func = '
$img_save_func = 'imagexbm';
$new_img_ext = 'xbm';
デフォルト:
$img_save_func = ' imagejpeg';
$new_img_ext = 'jpg';
}
//拡張子が設定されているかどうかに応じてローカル ファイル名を合成します
if (isset($this->set_extension) ){
$ext = strrchr($this->source,".");
$strlen = strlen($ext); ,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="このページのすべての画像をダウンロードします" /> >
実行結果は図に示すとおりです。
この例では、ダウンロードした写真は現在のディレクトリの pic フォルダーに保存されます。

人気の記事

人気の記事

ホットな記事タグ

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法
