導航:首頁 > 編程語言 > php抓取遠程圖片到本地

php抓取遠程圖片到本地

發布時間:2025-06-17 15:21:00

① 網站下載 php網頁下載

/*
*功能:php多種方式完美實現下載遠程圖片保存到本地
*參數:文件url,保存文件名稱,使用的下載方式
*當保存文件名稱為空時則使用遠程文件原來的名稱
*/
functiongetImage($url,$filename='',$type=0){
if($url==''){returnfalse;}
if($filename==''){
$ext=strrchr($url,'.');
if($ext!='.gif'&&$ext!='.jpg'){returnfalse;}
$filename=time().$ext;
}
//文件保存路徑
if($type){
$ch=curl_init();
$timeout=5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$img=curl_exec($ch);
curl_close($ch);
}else{
ob_start();
readfile($url);
$img=ob_get_contents();
ob_end_clean();
}
$size=strlen($img);
//文件大小
$fp2=@fopen($filename,'a');
fwrite($fp2,$img);
fclose($fp2);
return$filename;
}

② php curl get 下載遠程zip文件保存在本地例子

<?php

if($_POST['submit']){
$url=$_POST['url']; //取得提交過來的地址http://hu60.cn/wap/0wap/addown.php/fetion_sms.zip
$url=urldecode($url);
$fname=basename("$url"); //返迴路徑中的文件名部分 fetion_sms.zip
$str_name=pathinfo($fname); //以數組的形式返迴文件路徑的信息
$extname=strtolower($str_name['extension']); //把擴展名轉換成小寫
//$uptypes=explode(",",$forum_upload); //取得可以上傳的文件格式
//$size=getFileSize($url);

$time=date("Ymd",time());

$upload_dir="./upload/";//上傳的路徑
$file_name=$time.rand(1000,9999).'.'.$fname;
$dir=$upload_dir.$file_name;//創建上傳目錄

//判斷目錄是否存在 不存在則創建
if(!file_exists($upload_dir)){
mkdir($upload_dir,0777,true);
}

$contents=curl_download($url,$dir);

if($contents){
echo "下載成功";
}else{
echo "下載失敗";
}

}

function curl_download($url, $dir) {
$ch = curl_init($url);
$fp = fopen($dir, "wb");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res=curl_exec($ch);
curl_close($ch);
fclose($fp);
return $res;
}

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>遠程下載文件</title>
<form name="upform" method="post" action="" enctype='multipart/form-data'>
<input name='url' type='text' size='20'/>
<input type='submit' name='submit' value='遠程下載'/>
</form>
</body>
</html>

③ curl獲取遠程圖片時,如何設置本地保存路徑

設置保存路徑

define('IMAGE_DIR', 'c:\\xampp\\htdocs\\scraper\\image\\');

保存圖片函數。

$imageUrl = 你要的圖片的url
$imageType = 你要的圖片保存的格式

saveImage($imageUrl, $imageType = 'IMAGETYPE_GIF') {
if (!file_exists(IMAGE_DIR)) {
mkdir(IMAGE_DIR, 0777, true);
}

if( $imageType === IMAGETYPE_JPEG ) {
$fileExt = 'jpg';
} elseif ( $imageType === IMAGETYPE_GIF ) {
$fileExt = 'gif';
} elseif ( $imageType === IMAGETYPE_PNG ) {
$fileExt = 'png';
}

$newImageName = md5($imageUrl). '.' . $fileExt;
$image = new Image();
$image->load($imageUrl);
$image->resizeToWidth(100);
$image->save( IMAGE_DIR . $newImageName, $imageType );
return $newImageName;
}

這是我的圖片類,保存前可轉換格式,圖片大小。

<?php

class Image {
private $_image;
private $_imageFormat;

public function load($imageFile) {
$imageInfo = getImageSize($imageFile);
$this->_imageFormat = $imageInfo[2];
if( $this->_imageFormat === IMAGETYPE_JPEG ) {
$this->_image = imagecreatefromjpeg($imageFile);
} elseif( $this->_imageFormat === IMAGETYPE_GIF ) {
$this->_image = imagecreatefromgif($imageFile);
} elseif( $this->_imageFormat === IMAGETYPE_PNG ) {
$this->_image = imagecreatefrompng($imageFile);
}
}

public function save($imageFile, $_imageFormat=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $_imageFormat == IMAGETYPE_JPEG ) {
imagejpeg($this->_image,$imageFile,$compression);
} elseif ( $_imageFormat == IMAGETYPE_GIF ) {
imagegif($this->_image,$imageFile);
} elseif ( $_imageFormat == IMAGETYPE_PNG ) {
imagepng($this->_image,$imageFile);
}
if( $permissions != null) {
chmod($imageFile,$permissions);
}
}

public function getWidth() {
return imagesx($this->_image);
}

public function getHeight() {
return imagesy($this->_image);
}

public function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}

public function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}

public function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}

private function resize($width, $height) {
$newImage = imagecreatetruecolor($width, $height);
imageresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->_image = $newImage;
}

}

?>

④ php代碼保存遠程圖片到本地,出現重復建立文件夾,是怎麼回事出現嵌套建立文件夾

沒用過php,但如果出現這個問題就一定是if(file_exists($fname))判斷出的錯,試試將
$fname='yong/'.$id;改成
$fname='/yong/'.$id;試試看

或者要檢查的話,就在if(file_exists($fname)){ 上面加一行輸出一下file_exists($fname)看看每次判斷的結果。

閱讀全文

與php抓取遠程圖片到本地相關的資料

熱點內容
壓縮毛巾幹嘛用的 瀏覽:175
億郵如何發加密郵件 瀏覽:957
it程序員炒股 瀏覽:172
伺服器底層架構什麼意思 瀏覽:793
解壓視頻講故事正片 瀏覽:601
二階演算法都比一階演算法快 瀏覽:156
一句話說明是程序員 瀏覽:939
精雕快捷鍵命令大全 瀏覽:874
車子大本解壓後多久可以過戶 瀏覽:332
單片機軟體的編譯過程 瀏覽:434
當地服務商dns伺服器地址 瀏覽:428
星辰影視下載文件夾 瀏覽:605
35X簡便演算法 瀏覽:27
硬碟加密不加密區別 瀏覽:959
築業資料加密鎖哪裡有賣的 瀏覽:683
javaforeach數組 瀏覽:370
安卓如何開發區塊鏈 瀏覽:602
如何封裝自解壓的exe 瀏覽:800
雲主機雲伺服器怎樣收費 瀏覽:926
簡述編譯程序各部分的功能 瀏覽:721