图像调整大小代码不在div标签中工作

Image resize code not working in div tag. I have a code in PHP but it did not show image in div tag after resizing kindly help me to resolve this problem.

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

You set the header('Content-Type: image/jpeg'). So, you cannot add html tags anymore. Save the code as a "resizeimage.php" and create another html file like this

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

The problem you are facing in displaying the resized image into the tag is that, if you want to output the image this way i.e. by using header('Content-Type: image/jpeg'); the condition is that nothing (means no HTML tags or even blank spaces) should be rendered to the browser before the header and after the imagejpeg($image_p, null, 100); line, otherwise your image will not be displayed instead you will encounter an error something like this : The image..... cannot be displayed because it contains errors.

So for you I am pasting sample code below:

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