本文目錄一覽:
- 1、PHP CURL 獲取遠程數據下載
- 2、PHP獲取遠程圖片尺寸的問題
- 3、php怎麼高效獲取遠程圖片尺寸
- 4、curl獲取遠程圖片時,如何設置本地保存路徑?
- 5、PHP使用curl驗證遠程圖片是否有效
- 6、php 用CURL 抓取圖片
PHP CURL 獲取遠程數據下載
這樣做肯定是用的你的帶寬,是把文件下載到你的服務器上,然後再下載給客戶端。
有兩條路你可以去試試看,我沒做過:一是setcookie指定域名是那個網站,然後轉向:
setcookie ($cname ,$cvalue ,$expire ,$path , $host);
header(‘location: $url”);
另外一個方法類似,好像有個P3P可以傳遞COOKIE,需要你自己查資料:
setcookie ($cname ,$cvalue);
header(‘P3P: ….’);
header(‘location: $url”);
第二個辦法應該是可以的,陶寶和開心網都在用這樣的技術,陶寶有許多域名,一次登錄後都可以使用,就是利用P3P實現的COOKIE傳遞。
PHP獲取遠程圖片尺寸的問題
如果是批量的獲取尺寸,建議用curl獲取圖片加載到本機內存或者硬盤,然後再處理。
如果是單張的,沒什麼好方法。獲取速度取決於網速。
php怎麼高效獲取遠程圖片尺寸
/**
* 獲取遠程圖片的寬高和體積大小
*
* @param string $url 遠程圖片的鏈接
* @param string $type 獲取遠程圖片資源的方式, 默認為 curl 可選 fread
* @param boolean $isGetFilesize 是否獲取遠程圖片的體積大小, 默認false不獲取, 設置為 true 時 $type 將強製為 fread
* @return false|array
*/
function myGetImageSize($url, $type = ‘curl’, $isGetFilesize = false)
{
// 若需要獲取圖片體積大小則默認使用 fread 方式
$type = $isGetFilesize ? ‘fread’ : $type;
if ($type == ‘fread’) {
// 或者使用 socket 二進制方式讀取, 需要獲取圖片體積大小最好使用此方法
$handle = fopen($url, ‘rb’);
if (! $handle) return false;
// 只取頭部固定長度168字節數據
$dataBlock = fread($handle, 168);
}
else {
// 據說 CURL 能緩存DNS 效率比 socket 高
$ch = curl_init($url);
// 超時設置
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
// 取前面 168 個字符 通過四張測試圖讀取寬高結果都沒有問題,若獲取不到數據可適當加大數值
curl_setopt($ch, CURLOPT_RANGE, ‘0-167’);
// 跟蹤301跳轉
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// 返回結果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$dataBlock = curl_exec($ch);
curl_close($ch);
if (! $dataBlock) return false;
}
// 將讀取的圖片信息轉化為圖片路徑並獲取圖片信息,經測試,這裡的轉化設置 jpeg 對獲取png,gif的信息沒有影響,無須分別設置
// 有些圖片雖然可以在瀏覽器查看但實際已被損壞可能無法解析信息
$size = getimagesize(‘data://image/jpeg;base64,’. base64_encode($dataBlock));
if (empty($size)) {
return false;
}
$result[‘width’] = $size[0];
$result[‘height’] = $size[1];
// 是否獲取圖片體積大小
if ($isGetFilesize) {
// 獲取文件數據流信息
$meta = stream_get_meta_data($handle);
// nginx 的信息保存在 headers 里,apache 則直接在 wrapper_data
$dataInfo = isset($meta[‘wrapper_data’][‘headers’]) ? $meta[‘wrapper_data’][‘headers’] : $meta[‘wrapper_data’];
foreach ($dataInfo as $va) {
if ( preg_match(‘/length/iU’, $va)) {
$ts = explode(‘:’, $va);
$result[‘size’] = trim(array_pop($ts));
break;
}
}
}
if ($type == ‘fread’) fclose($handle);
return $result;
}
// 測試的圖片鏈接
echo ‘pre’;
$result = myGetImageSize(”, ‘curl’);
print_r($result);
echo ‘hr /’;
$result = myGetImageSize(”, ‘fread’);
print_r($result);
echo ‘hr /’;
$result = myGetImageSize(”, ‘fread’, true);
print_r($result);
echo ‘hr /’;
$result = myGetImageSize(”, ‘curl’, true);
print_r($result);
echo ‘hr /’;
$result = myGetImageSize(”, ‘fread’);
print_r($result);
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);
imagecopyresampled($newImage, $this-_image, 0, 0, 0, 0, $width, $height, $this-getWidth(), $this-getHeight());
$this-_image = $newImage;
}
}
?
PHP使用curl驗證遠程圖片是否有效
try {
$ch = curl_init();
$url = “”;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200) {
echo “正常訪問”;
} else {
echo “非正常訪問”;
}
curl_close($ch);
} catch (Exception $exception) {
var_dump($exception-getMessage());
}
php 用CURL 抓取圖片
preg_match(‘@p\.php\?p=(.*)@Ui’, $url, $url);//獲取圖片地址
if(isset($url[1]))
$url=$url[1];
else
$url=”;
if($url):
//curl抓取圖片過程
$ch = curl_init();
if (defined(‘CURLOPT_IPRESOLVE’) defined(‘CURL_IPRESOLVE_V4’)) {
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info[‘http_code’] != 200)
$content = NULL;
if($content)//保存圖片到本地
@file_put_contents (‘存放地址’, $content);
endif;
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/160551.html