Php 图像不';t显示
我有这个代码,我想调整图片的大小,它设法做到第一个100x100,但不是其他。我不明白为什么它不工作,它没有任何错误。我是一个php新手,不知道到底出了什么问题,但正如您所看到的,代码对第一个起作用,但为什么对其他人不起作用Php 图像不';t显示,php,Php,我有这个代码,我想调整图片的大小,它设法做到第一个100x100,但不是其他。我不明白为什么它不工作,它没有任何错误。我是一个php新手,不知道到底出了什么问题,但正如您所看到的,代码对第一个起作用,但为什么对其他人不起作用 <?php function thumbnail($image, $width, $height) { $image_properties = getimagesize($image); $image_width = $image_prop
<?php
function thumbnail($image, $width, $height) {
$image_properties = getimagesize($image);
$image_width = $image_properties[0];
$image_height = $image_properties[1];
$image_ratio = $image_width / $image_height;
$type = $image_properties["mime"];
if(!$width && !$height) {
$width = $image_width;
$height = $image_height;
}
if(!$width) {
$width = round($height * $image_ratio);
}
if(!$height) {
$height = round($width / $image_ratio);
}
if($type == "image/jpeg") {
header('Content-type: image/jpeg');
$thumb = imagecreatefromjpeg($image);
} elseif($type == "image/png") {
header('Content-type: image/png');
$thumb = imagecreatefrompng($image);
} elseif($type == "image/gif") {
header('Content-type: image/gif');
$thumb = imagecreatefromgif($image);
}
else {
return false;
}
$temp_image = imagecreatetruecolor($width, $height);
imagecopyresampled($temp_image, $thumb, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
$thumbnail = imagecreatetruecolor($width, $height);
imagecopyresampled($thumbnail, $temp_image, 0, 0, 0, 0, $width, $height, $width, $height);
if($type == "image/jpeg") {
imagejpeg($thumbnail);
}
elseif($type == "image/jpeg") {
imagepng($thumbnail);
}
elseif($type == "image/gif") {
imagegif($thumbnail);
}
imagedestroy($temp_image);
imagedestroy($thumbnail);
}
$pic_size = array();
// Adjust size
$pic_size['height'][0] = 100;
$pic_size['width'][0] = 100;
$pic_size['height'][1] = 200;
$pic_size['width'][1] = 200;
$pic_size['height'][2] = 300;
$pic_size['width'][2] = 300;
$pic_size['height'][3] = 400;
$pic_size['width'][3] = 400;
$pic_size['height'][4] = 500;
$pic_size['width'][4] = 500;
$total_pic_size= count($pic_size['height']);
$x = 0;
foreach(array_keys($pic_size['height']) as $x) {
thumbnail($_GET["img"], $pic_size['width'][$x], $pic_size['height'][$x]);
echo '<img src="index.php?w='.$pic_size['width'][$x].'&h='.$pic_size['height'][$x].'&img='.$_GET["img"].'" />';
$x++;
}
?>
您的
$total\u pic\u size
始终为2
$total_pic_size = count( $pic_size['width'] );
这将为您提供正确的项数,循环不应该只运行一次
还应更改循环条件:
while ($x <= $total_pic_size) {
while($x您只迭代了两次,因为
$total_pic_size= count($pic_size);// is 2
改为:
$total_pic_size= count($pic_size['height']);
或使用foreach:
foreach(array_keys($pic_size['height']) as $x)
不要在这里使用COUNT\u RECURSIVE
,它会再次返回错误的数字,在width
和height
键中计数
另外,缩略图
函数中有标题
调用,不能在echo
之后调用,在您的情况下,在您的循环中,在echo之后调用。您应该看到警告说“标题已发送”
您可以将图像保存到文件并以文件路径作为源回显图像。删除标题
调用,让缩略图
功能保存图像并返回文件名,例如:
imagejpeg($thumbnail, $filename);
return $filename;
并将输出用作回声图像的src
。将错误报告添加到文件顶部例如,在打开PHP标记之后,我没有报告任何错误。我甚至检查了PHP错误日志,但没有任何错误。您创建了图像,但从未将其保存到服务器中,函数没有返回值。count($total\u pic\u size)
返回2,但现在不重要了。@panther我该如何保存它?我已经更新了递归计数。所以现在它循环5:)很好!我已经用count recursive更新了。谢谢!我想我是从这里得到的。好的接球!我已使用count recursive进行了更新。使用count_recursive(10而不是5!!)时,您的计数太高。更新了我的代码,但仍然没有解决问题。@user1544809 updated-很高兴看到您正在解决问题的路上。
foreach(array_keys($pic_size['height']) as $x)
imagejpeg($thumbnail, $filename);
return $filename;