【发布时间】:2013-04-12 02:19:54
【问题描述】:
我找到并修改了一个用于生成缩略图的小 php 脚本
$src = (isset($_GET['file']) ? $_GET['file'] : "");
$width = (isset($_GET['maxwidth']) ? $_GET['maxwidth'] : 73);
$thname = "xxx";
$file_extension = substr($src, strrpos($src, '.')+1);
switch(strtolower($file_extension)) {
case "gif": $content_type="image/gif"; break;
case "png": $content_type="image/png"; break;
case "bmp": $content_type="image/bmp"; break;
case "jpeg":
case "jpg": $content_type="image/jpg"; break;
default: $content_type="image/png"; break;
}
if (list($width_orig, $height_orig, $type, $attr) = @getimagesize($src)) {
$height = ($width / $width_orig) * $height_orig;
}
$tn = imagecreatetruecolor($width, $height) ;
$image = imagecreatefromjpeg($src) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($tn, './media/'.$thname.'.'.$file_extension, 90);
完美地生成和保存缩略图。
如何即时显示这些缩略图?
我尝试将其添加到脚本的底部
header('Content-Type: image/jpeg');
imagegd($image);
但上面写着The image cannot be displayed because it contains errors。我做错了什么?
【问题讨论】:
-
使用文本编辑器查看图像的源代码;您可能会在其中看到一条 PHP 错误消息。
-
正如我所说:“它完美地生成和保存缩略图”
-
啊,所以您将该代码添加到 HTML 页面。那是行不通的;您需要将每个结果嵌入
<img>标记中。您可以使用 DATA URI 动态显示它们,但这在 Internet Explorer 中效果不佳,在旧版本中根本无法使用 -
脚本顶部的空格是问题所在。非常感谢您的努力
标签: php image header thumbnails