2011-08-27 5 views
3

나는 다른 URL에서 이미지와 데이터를 가져 오기 위해 PHP를 사용하고 있습니다. 하지만 이미지를 base64 문자열로 변환해야합니다 .. !! 코드는이 라인이 표준 확장에변환 이미지 다른 URL에서 가져 오는 동안 base64하려면

<?php 

function getMetaTitle($content){ 
    $pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui"; 
    if(preg_match($pattern, $content, $match)) 
    return $match[1]; 
    else 
    return false; 
} 
function fetch_record($path) 
{ 
    $file = fopen($path, "r"); 
    if (!$file) 
    { 
     exit("Problem occured"); 
    } 
    $data = ''; 
    while (!feof($file)) 
    { 
     $data .= fgets($file, 1024); 
    } 
    return $data; 
} 

$url = $_POST['url']; 

$data = array(); 

// get url title 
$content = @file_get_contents($url); 
$data['title'] = getMetaTitle($content); 

// get url description from meta tag 
$tags = @get_meta_tags($url); 
$data['description'] = $tags['description']; 

$string = fetch_record($url); 
// fetch images 
$image_regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui'; 
preg_match_all($image_regex, $content, $img, PREG_PATTERN_ORDER); 
$images_array = $img[1]; 
$k=1; 
    for ($i=0;$i<=sizeof($images_array);$i++) 
    { 
     if(@$images_array[$i]) 
     { 
      if(@getimagesize(@$images_array[$i])) 
      { 
       list($width, $height, $type, $attr) = getimagesize(@$images_array[$i]); 
       if($width > 50 && $height > 50){ 

       $data['images'] = "<img src='"[email protected]$images_array[$i]."' id='".$k."' width='100%'>"; 

       $k++; 

       } 
      } 
     } 
    } 

$data['form'] = '<input type="hidden" name="images" value="'.$data['images'].'"/> 
       <input type="hidden" name="title" value="'.$data['title'].'"/> 
       <input type="hidden" name="description" value="'.$data['description'].'"/>'; 


$dom = new domDocument; 

@$dom->loadHTML($content); 

$dom->preserveWhiteSpace = false; 

$images = $dom->getElementsByTagName('img'); 

foreach($images as $img) 
{ 
    $url = $img->getAttribute('src'); 
    $alt = $img->getAttribute('alt'); 
    $pos = strpos($url, 'http://'); 

if ($pos === false) { 
    // $data['images'] = '<img src="'.$_POST['url'].''.$url.'" title="'.$alt.'"/>'; 
} else { 
    // $data['images'] = '<img src="'.$url.'" title="'.$alt.'"/>'; 
}  



} 


echo json_encode($data); 

?> 

이 코드 사용 이미지

$data['images'] = "<img src='"[email protected]$images_array[$i]."' id='".$k."' width='100%'>"; 

내가 64 기수로 변환 할 당신이 URL을 일단 그들이

+0

[base64_encode] (http://php.net/base64_encode) – deceze

+0

나는 이것을 시도했다 !! !! 하지만 제대로 작동하지 않습니다 .. !! 내가 어디 잘못된지 모르겠다 .. !! –

+0

@Basic Bridge : 잘 모르겠지만 이미지를 가져 오지 않고 페이지 자체 만 가져옵니다. 다른 것을하기 전에'file_get_contents()'를 사용하여 이미지를 가져 오십시오. 당신이 할 일은 너비 높이와 이미지 URL에서 다른 쓰레기를 확인하는 것입니다 (이해가 안되네요). – xfix

답변

4

를 사용 이미지의 경우 curl로 잡고 base64_encode를 호출하면됩니다. chunk_split은 단지 그것을 삐걱 거리게 만듭니다.

$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$ret_val = curl_exec($curl); 
// TODO: error checking!!! 

$b64_image_data = chunk_split(base64_encode($ret_val)); 
curl_close($curl); 
4

또 다른 옵션은 PHP 스트림 변환 필터 (docs)와 Base64로 인코딩 된 값으로 이미지의 이진 데이터를 필터링하기 위해 취득하기위한 것이다.

$img_url = 'http://www.php.net/images/php.gif'; 
$b64_url = 'php://filter/read=convert.base64-encode/resource='.$img_url; 
$b64_img = file_get_contents($b64_url); 
echo $b64_img; 
관련 문제