簡體   English   中英

file_put_contents()在嘗試導出圖像時發出錯誤

[英]file_put_contents() is issuing an error when trying to export an image

我通過將一些圖像復制到一個新圖像中來創建圖像。 在我的程序的最后一步,我試圖將此文件導出到一個文件夾中。

代碼如下:

<?php

        require_once "../shdp/simple_html_dom.php";

        $next = "http://www.pidjin.net/2012/08/28/of-my-own/";
        $html = file_get_html($next);
        $imageList = $html->find('div[class=episode] p img');

        $newHeight = 0;

        for($iii=0; $iii<count($imageList); $iii++){
            $storage[$iii] = $imageList[$iii]->src;
            $img[$iii] = imagecreatefromstring(file_get_contents($storage[$iii]));

            $width[$iii] = imagesx($img[$iii]);
            $height[$iii] = imagesy($img[$iii]);

            $newHeight += ($height[$iii] + 30);
        }

        $newWidth = max($width);
        $cummDestHeight = 0;

        $export = imagecreatetruecolor($newWidth, $newHeight);
        imagefill($export, 0,0, 0xFFFFFF);

        for($iii=0;$iii<count($img);$iii++){
            imagecopy($export, $img[$iii], 0, $cummDestHeight, 0, 0, $width[$iii], $height[$iii]);
            $cummDestHeight += $height[$iii] + 30;
        }


        $bits = explode('/',$next);
        file_put_contents("../pidjin/$bits[5]-$bits[4]-$bits[3].png",$export);
?>

我收到的錯誤是這樣的:

Warning: file_put_contents(): supplied resource is not a valid stream resource in E:\Web\Comics\pidjin.php on line 54

問題 :我不確定如何使$ export導出有效的流資源。

$ export將成為GD圖像句柄。 不是你可以簡單地轉儲到文件並期望獲得JPG或PNG圖像的東西。

為此,你應該這樣做

imagepng($export, "../pidjin/$bits etc...");

這將為您創建.PNG文件。

另一個問題是,我終於能夠使代碼正常工作了。

解決方案 :問題是我試圖使用file_put_contents來轉儲GD句柄,結果證明它並不那么簡單。 我被引導到imagepng函數,該函數將目錄作為導出文件的第二個參數。

程序 :我制作了一個程序,從webcomic Fredo和Pidjin下載條帶,這樣我以后可以在無法訪問互聯網時閱讀它。 該計划如下。

<?php

require_once "../shdp/simple_html_dom.php";

$next = "http://www.pidjin.net/2006/02/19/goofy-monday/";

$counter = 1;
while($next){

    $html = file_get_html($next);

    $imageList = $html->find('div[class=episode] p img');

    $newHeight = 0;

    for($iii=0; $iii<count($imageList); $iii++){
        $storage[$iii] = $imageList[$iii]->src;
        $img[$iii] = imagecreatefromstring(file_get_contents($storage[$iii]));

        $width[$iii] = imagesx($img[$iii]);
        $height[$iii] = imagesy($img[$iii]);

        $newHeight += ($height[$iii] + 30);
    }

    $newWidth = max($width);
    $cummDestHeight = 0;

    $export = imagecreatetruecolor($newWidth, $newHeight);
    imagefill($export, 0,0, 0xFFFFFF);

    for($iii=0;$iii<count($img);$iii++){
        imagecopy($export, $img[$iii], 0, $cummDestHeight, 0, 0, $width[$iii], $height[$iii]);
        $cummDestHeight += $height[$iii] + 30;
    }


    $bits = explode('/',$next);

    imagepng($export, "../pidjin/$counter ($bits[5]-$bits[4]-$bits[3]).png");

    $nextUrl = $html->find('span[class=next] a[rel=next]');
    $next = $nextUrl[0]->href;
    $counter++;
}

?>

注意:我使用Simple HTML DOM Parser來抓取源代碼並瀏覽DOM。

干杯。

剛剛將“file_put_contents”替換為“imagejpeg($ rotate,$ file_new);”

根據PHP手冊的file_put_contents將第二個參數作為字符串。 圖像文件不是字符串。 請參閱上面的另外兩個回答。 這就是你保存圖像的方式。 請多嘗試使用本手冊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM