Php 文件\u put\u contents()在尝试导出图像时出错
我通过将多个图像复制到一个新的图像中创建了一个图像。在程序的最后一步中,我正试图将此文件导出到一个文件夹中 代码如下:Php 文件\u put\u contents()在尝试导出图像时出错,php,Php,我通过将多个图像复制到一个新的图像中创建了一个图像。在程序的最后一步中,我正试图将此文件导出到一个文件夹中 代码如下: <?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
<?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);
?>
问题:我不确定如何使$export成为有效的流资源。$export将成为GD图像句柄。它是而不是你可以简单地转储到一个文件中,并期望得到一个JPG或PNG图像 为此,你应该这样做
imagepng($export, "../pidjin/$bits etc...");
这将为您创建.PNG文件。还有一个问题,我终于可以让代码正常工作了 解决方案:问题是我试图使用file\u put\u contents来转储GD句柄,结果证明,它并没有那么简单。我被引导到
imagepng
函数,该函数将目录作为导出文件的第二个参数
程序:我制作了一个程序,从网络喜剧中下载这些片段,以便在以后无法上网时阅读。程序如下所示
<?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++;
}
注意:我使用了简单的HTMLDOM解析器来获取源代码并查看DOM
干杯。根据PHP手册,文件内容将第二个参数作为字符串。
图像文件不是字符串。
参见上面的另外两个答案。
这就是保存图像的方式。
请多尝试一下使用手册。刚刚将“文件内容”替换为“图像JPEG($rotate,$file\u new);”非常感谢。这澄清了我对GD的巨大困惑。再次感谢。所有这些(完全无用的)GD工作有什么意义?你从一个站点获得一张图片,你将该图片复制到另一张图片上,保存新图片。相反,您可以直接保存获取的原始图像,而无需解压缩。
<?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++;
}