PHP图像大小调整是';t工作


PHP image resize isn't working

我从PHP站点找到了以下脚本,其大小只有图像的一半。我使用数据库来获取图像的链接。其他事情都正常工作,这意味着除了这一个之外,任何地方都没有任何错误。

echo "<img src='".// File and new size
$filename = '$row["image"]';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
"'>"

错误:整个页面被破坏,并且有一个图像的链接被破坏
希望你们能帮助我!

删除这些行后即可工作:

echo "<img src='".// File and new size
"'>"

header帮你做,通知有jpg来了,不需要回显图像标签。

另一个解决方案是删除这条线:

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

并创建新文件,然后将其用作图像的来源:

// Output
$new_filename = 'new_image.jpg';
imagejpeg($thumb,$new_filename);//saves new image to a file, instead of outputting it to the screen
echo "<img src='$new_filename'>";

试试这个,它可以

<?php
$filename = '$row["image"]';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
 $newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
echo "<img src='".imagejpeg($thumb)."'>";
?>