从字符串中获取图像,上载它们并替换为新的链接


get images from an string upload them and replace with new links

我有一个包含文本和照片的字符串,如下所示。我的代码到目前为止得到所有的图像,并将它们上传到一个文件夹。我需要用正确的顺序替换新上传的链接。

$nextstep = "Hello there this is image 1 <img src='http://www.demosite.com/wp-content/uploads/2015/01.jpg' width='653' height='340' alt='xxx' title='xxx'> !! And Now you can see image number 2 <img src='http://www.demosite.com/wp-content/uploads/2015/02.jpg' width='653' height='340' alt='xxx' title='xxx'>";
$string = $nextstep;
$doc = new DOMDocument();
$doc->loadHTML($string);
$images = $doc->getElementsByTagName('img');
foreach ($images as $image) { //STARTING LOOP
    echo "</br>";
    echo $image->getAttribute('src') . "'n";
    echo "</br>";
    $urlimg = $image->getAttribute('src'); //IMAGE URL
    $URL = urldecode($urlimg);
    $image_name = (stristr($URL,'?',true))?stristr($URL,'?',true):$URL;
    $pos = strrpos($image_name,'/');
    $image_name = substr($image_name,$pos+1);
    $extension = stristr($image_name,'.');
    if($extension == '.jpg' || $extension == '.png' || $extension == '.gif' || $extension == '.jpeg'){
        $img = '../images/' . $image_name;
        file_put_contents($img, file_get_contents($url)); //UPLOAD THEM ONE BY ONE
    }
}

这里不清楚期望的结果是什么。这听起来像你想改变你的现有字符串的src URL到一个你已经保存的图像。如果不是这样,请尝试更新问题以更清晰。

这里有一个简单的方法来分解这个问题…

步骤1 -使用源字符串

从DOM中提取img标签
$html = <<<'HTML'
Hello there this is image 1 <img src="http://www.demosite.com/wp-content/uploads/2015/01.jpg" width="653" height="340" alt="xxx" title="xxx"> !! 
And Now you can see image number 2 <img src="http://www.demosite.com/wp-content/uploads/2015/02.jpg" width="653" height="340" alt="xxx" title="xxx">
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html);
$imgs = $dom->getElementsByTagName('img');
// Store the list of image urls in an array - this will come in handy later
$imgURLs = [];
foreach($imgs as $img) {
    if (!$img->hasAttribute('src')) {
        continue;
    }
    $imgURLs[] = $img->getAttribute('src');
}

步骤2 -将图像保存在不同的位置

$newImgURLs = [];          // new modified URLs where images were moved
$newPath    = '../images'; // wherever you're saving the images
foreach($imgURLs as $imgURL) {
    /**
     *  Use parse_url and pathinfo to break down the URL parts and extract the
     *  filename/extension instead of the fragile implementation you used above
     */
    $URLparts     = parse_url($imgURL);
    $file         = pathinfo($URLparts['path']);
    $fileName     = $file['filename'] . '.' . $file['extension'];
    $newFileName  = $newPath . '/' . $fileName;
    $newImgURLs[] = $URLparts['scheme'] . '://' .
                    $URLparts['host'] . $file['dirname'] . '/' . $newFileName .
                    (isset($URLparts['query']) ? ('?' . $URLparts['query']) : null) .
                    (isset($URLparts['fragment']) ? ('#' . $URLparts['fragment']) : null);
    // download image and save to new location
    file_put_contents($newFileName, file_get_contents($imgURL));
}

步骤3 -将img src url修改为新路径

foreach($imgs as $i => $img) {
    $img->setAttribute('src', $newImgURLs[$i]);
}
echo $dom->saveHTML(); // new updated DOM
// or just create a new $html string from scratch using the new URLs.