㈠ php 把圖片下載本地保存到指定目錄中
/*
*@$urlstring遠程圖片地址
*@$dirstring目錄,可選,默認當前目錄(相對路徑)
*@$filenamestring新文件名,可選
*/
functionGrabImage($url,$dir='',$filename=''){
if(empty($url)){
returnfalse;
}
$ext=strrchr($url,'.');
if($ext!='.gif'&&$ext!=".jpg"&&$ext!=".bmp"){
echo"格式不支持!";
returnfalse;
}
//為空就當前目錄
if(empty($dir))$dir='./';
//
$dir=realpath($dir);
//目錄+文件
$filename=$dir.(empty($filename)?'/'.time().$ext:'/'.$filename);
//開始捕捉
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;
}
//測試
GrabImage("此處網址/1.jpg","as.gif");
//PS:目錄存在,許可權判斷,自創建等自己應該知道!
//個人喜歡絕對路徑所以就那麼寫了
㈡ 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 函數的遠程保存圖片問題
我一般不用,直接使用file_get_contents()取得文件內容,再寫入本地文件中去
㈣ php怎麼把遠程圖片通過api介面傳到另外一個站點上
本地傳圖片到伺服器叫上傳,伺服器從別的網站獲取圖片,這叫下載,這比上傳還簡單
$content = file_get_contents(圖片地址);
file_put_contents(保存的路徑文件名, $content);
㈤ php保存遠程圖片到本地
$img = file_get_contents('http://www.91cici.com/images/logo.gif');
file_put_contents('test.gif',$img);
㈥ 網站下載 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下載遠程圖片jpg 格式,
<?php
header("Content-type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Accept-Length:".filesize($path));
header("Content-Disposition:attachment;filename=".basename($path));
readfile($path);
?>
㈧ thinkphp用file_put_contents()保存遠程圖片到伺服器
你在逗我,怎麼可能直接用程序直接寫入另一個伺服器。
我的思路是(沒有試驗過)
轉換為圖片為二進制文件進行上傳到另一個伺服器進行處理保存
轉換圖片為base64 用curl請求伺服器進行處理
模擬表單請求
㈨ 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
㈩ 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>