Php 图像大小调整代码在div标记中不起作用

Php 图像大小调整代码在div标记中不起作用,php,Php,图像大小调整代码在div标记中不起作用。我有一个PHP代码,但在调整大小后,它没有在div标记中显示图像。请帮助我解决这个问题。 // The file $filename = 'Pictures/DSC_0039 (2).jpg'; // Set a maximum height and width $width = 200; $height = 200; // Content type header('Content-Type: image/jpeg'); // Get new dim

图像大小调整代码在div标记中不起作用。我有一个PHP代码,但在调整大小后,它没有在div标记中显示图像。请帮助我解决这个问题。
// The file
$filename = 'Pictures/DSC_0039 (2).jpg';

// Set a maximum height and width
$width = 200;
$height = 200;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>
</div>
//文件
$filename='Pictures/DSC_0039(2.jpg';
//设置最大高度和宽度
$width=200;
$height=200;
//内容类型
标题(“内容类型:图像/jpeg”);
//获取新维度
列表($width\u orig,$height\u orig)=getimagesize($filename);
$ratio\u orig=$width\u orig/$height\u orig;
如果($width/$height>$ratio\u orig){
$width=$height*$ratio\u orig;
}否则{
$height=$width/$ratio\u orig;
}
//重采样
$image\u p=imageCreateTureColor($width,$height);
$image=imagecreatefromjpeg($filename);
imagecopyresampled($image\u p,$image,0,0,0,$width,$height,$width\u orig,$height\u orig);
//输出
imagejpeg($image_p,null,100);
?>
您可以设置
标题('Content-Type:image/jpeg')
。因此,您不能再添加html标记了。将代码另存为“resizeimage.php”,并创建另一个类似这样的html文件

<div>
    <img src="resizeimage.php" />
</div>

在将调整大小的图像显示到标记中时,您面临的问题是,如果您希望以这种方式输出图像,即使用标题(“内容类型:图像/jpeg”)条件是,在标题之前和图像JPEG($image\p,null,100)之后,不应向浏览器呈现任何内容(意味着没有HTML标记,甚至没有空格)line,否则您的图像将不会显示,相反,您将遇到如下错误:图像。。。。。无法显示,因为它包含错误

因此,我将为您粘贴以下示例代码:

<?php
// The file
$filename = 'Koala.jpg';

// Set a maximum height and width
$width = 200;
$height = 200;



// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);


// Output
imagejpeg($image_p, "resized_img.jpg", 100);//in this function the second parameter is the filename of the new resized image
?>
<img src="resized_img.jpg" />


您可以显示您的、和CSS代码吗?没有CSS代码和img,但这是//输出图像JPEG($image\p,null,100);如果您从上面删除div标记,则代码将显示图像。如果您设置了标题('Content-Type:image/jpeg')。因此,您无法再添加html标记。请确保您的php文件是以
开头的。当您使用类似
的浏览器打开它时,是否可以看到图像http://localhost/resizeimage.php
?因此,您必须检查您的图片是否位于正确的位置
$filename='Pictures/DSC_0039(2).jpg'