2012-08-29 44 views
2

我已經通過將多個圖像複製到新圖像來創建圖像。在我的程序的最後一步中,我試圖將該文件導出到一個文件夾中。嘗試導出圖像時,file_put_contents()發出錯誤

的代碼如下:

<?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 

問題:我不知道我怎樣才能使$導出一個有效的流資源。

回答

1

還有一個問題,我終於能夠使代碼工作。

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

計劃:我做了一個程序從的webcomic Fredo and 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++; 
} 

>

注:我已經使用了簡單的HTML DOM解析器刮源,並通過DOM看。

乾杯。

+0

所有這些(完全無用的)GD工作的重點是什麼?您從網站獲取圖像,將該圖像的精確副本複製到另一張圖像,然後保存該新圖像。相反,您可以直接保存您提取的原始圖像,而無需解壓縮它。 –

4

$ export將成爲GD圖像句柄。這是東西,你可以簡單地轉儲到一個文件中,並期望得到一個JPG或PNG圖片..

對於這一點,你應該做

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

,這將創造.png文件爲你。

+0

非常感謝。這澄清了我與GD的巨大混淆。再次感謝。 – day0

-1

file_put_contents按照PHP手冊中的第二個參數獲取一個字符串。 圖像文件不是字符串。 請參閱上面的其他兩個答案。 這就是你保存圖像的方式。 嘗試使用手冊多一點。

0

用「imagejpeg($ rotate,$ file_new);」替換「file_put_contents」