【问题标题】:file_put_contents() is issuing an error when trying to export an image尝试导出图像时 file_put_contents() 发出错误
【发布时间】:2014-09-10 16:42:50
【问题描述】:

我通过将许多图像复制到新图像中来创建图像。在我程序的最后一步中,我试图将此文件导出到一个文件夹中。

代码如下:

<?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 成为有效的流资源。

【问题讨论】:

    标签: php


    【解决方案1】:

    $export 将是一个 GD 图像句柄。 不是,您可以简单地将其转储到文件中并期望获得 JPG 或 PNG 图像..

    为此,你应该这样做

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

    这将为您创建 .PNG 文件。

    【讨论】:

    • 非常感谢。这澄清了我对 GD 的巨大困惑。再次感谢。
    【解决方案2】:

    有了另一个问题,我终于可以让代码正常工作了。

    解决方案:问题是我试图使用 file_put_contents 转储一个 GD 句柄,但事实证明,它并不是那么简单。我被定向到imagepng 函数,该函数将目录作为导出文件的第二个参数。

    程序:我制作了一个程序来从网络漫画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++;
    }
    

    ?>

    注意:我使用了 Simple HTML DOM Parser 来抓取源代码并查看 DOM。

    干杯。

    【讨论】:

    • 所有这些(完全没用的)GD 工作的意义何在?您从站点获取图像,将该图像的精确副本复制到另一个图像,保存该新图像。相反,您可以直接保存获取的原始图像,而无需解压缩。
    【解决方案3】:

    刚刚将“file_put_contents”替换为“imagejpeg($rotate, $file_new);”

    【讨论】:

      【解决方案4】:

      file_put_contents 根据 PHP 手册,第二个参数是一个字符串。 图像文件不是字符串。 请参阅上面的其他两个答案。 这就是您保存图像的方式。 多尝试使用手册。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-23
        • 2021-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-25
        • 2021-11-11
        相关资源
        最近更新 更多