導航:首頁 > 編程語言 > php抓取遠程圖片函數

php抓取遠程圖片函數

發布時間:2025-08-07 15:57:53

A. php怎麼把遠程圖片通過api介面傳到另外一個站點上

本地傳圖片到伺服器叫上傳,伺服器從別的網站獲取圖片,這叫下載,這比上傳還簡單
$content = file_get_contents(圖片地址);
file_put_contents(保存的路徑文件名, $content);

B. 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;
}

}

?>

C. 網站下載 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;
}

D. PHP網路圖片本地化 原圖片轉換成本地化後的圖片

用網路的UEditor其中有個遠程圖片抓取交互,可以實現你的要求。

1、遠程抓取原理

圖片遠程抓取是指在插入本地域名之外的圖片鏈接地址時,由伺服器將這些外部圖片抓取到本地伺服器保存的一個功能。實現原理為在編輯器中向伺服器發送包含所有外域圖片地址的ajax請求,然後由伺服器在後端抓取保存後返回圖片地址給編輯器,再由編輯器完成外域地址和本地地址的替換工作。

//是否開啟遠程圖片抓取
catchRemoteImageEnable:true,

//處理遠程圖片抓取的地址
catcherUrl:URL+"server/submit/php/getRemoteImage.php",

//提交到後台遠程圖片uri合集的表單名
catchFieldName:"upFile",

//圖片修正地址,同imagePath
catcherPath:fixedImagePath,

//本地頂級域名,當開啟遠程圖片抓取時,除此之外的所有其它域名下的
//圖片都將被抓取到本地
localDomain:[".com","10.81.2.114"],

2、注意事項

遠程抓取功能是否開啟可在edicot_config.js中通過配置catchRemoteImageEnable參數實現。與這個功能相關的配置還包括了遠程抓取的處理程序地址,表單域名稱,本地域和「前後端修正地址」。遠程抓取處理程序實現了依據前端提交的地址列表(使用ue_separate_ue標示符進行分隔的字元串)進行圖片抓取,然後返回地址列表給客戶端的功能。

前後台交互數據格式樣例:(URL1,URL2,URL3,URL4)

URL1ue_separate_ueURL2ue_separate_ueURL3ue_separate_ueURL4

E. 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>

閱讀全文

與php抓取遠程圖片函數相關的資料

熱點內容
程序員可以進哪些央企 瀏覽:13
皇上命令王宮大臣齊聚池塘 瀏覽:536
程序員的一切皆可編程 瀏覽:994
蘋果手機怎麼建文件夾分類 瀏覽:35
fb功能塊編程 瀏覽:93
linux編譯路徑怎麼加 瀏覽:4
編譯激活時間 瀏覽:469
java域名正則表達式 瀏覽:577
php和python混合編程 瀏覽:33
程序員可以在線服務嗎 瀏覽:292
打開php郵件 瀏覽:940
ios怎麼打開pdf文件 瀏覽:928
阿里雲建站用什麼伺服器版本 瀏覽:461
java日期獲取月份 瀏覽:106
安卓文件夾右上角圖標 瀏覽:179
伺服器cnd流量是什麼意思 瀏覽:775
安卓手機國外怎麼用優步打車 瀏覽:262
懷孕可以下載什麼app 瀏覽:331
maya建模命令 瀏覽:588
為什麼點了戰錘無法訪問伺服器 瀏覽:458