PHP图像调整大小不起作用

时间:2015-11-21 08:47:48

标签: php

我发现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);
"'>"

错误: 整个页面被破坏,图像链接断开 希望你们能帮助我!

2 个答案:

答案 0 :(得分:1)

删除这些行后它会起作用:

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'>";

答案 1 :(得分:1)

试试这个有效

<?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)."'>";
?>