从其他网址获取图片并将其转换为base64格式

4

我正在使用PHP编写代码从其他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='".@$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='".@$images_array[$i]."'  id='".$k."' width='100%'>";

我希望您将它们转换为base64,然后再使用它们。

base64_encode - deceze
我尝试了这个..!! 但它没有正常工作..!! 我不知道哪里出错了..!! - Basic Bridge
@Basic Bridge:我不确定,但是您似乎只获取页面本身而不是图片。在执行其他操作之前,请使用file_get_contents()来获取图像。您所做的只是检查图像URL中的宽度、高度和其他无用信息(这没有意义)。 - Konrad Borowski
2个回答

6
一旦您获得了图像的URL,您只需要使用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);

5

另一个选择是让PHP将图像的二进制数据通过流转换过滤器(文档)转换为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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接