PHP生成并显示图像缩略图
我发现并修改了一个用于生成缩略图的小php脚本PHP生成并显示图像缩略图,php,image,header,thumbnails,Php,Image,Header,Thumbnails,我发现并修改了一个用于生成缩略图的小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)) {
$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);
但是它说,图像无法显示,因为它包含错误。我做错了什么?
请尝试在文件末尾取消关闭?>并确保文件顶部没有空格。只需在换行符上,图像就会中断。请尝试在文件末尾取消关闭?>并确保文件顶部没有空格。只需换行,图像就会中断。在php中,最简单的方法是使用imagejpeg()
函数
在我的一个解决方案中,我使用这个函数创建了图像缩略图,在这个函数中我可以指定高度和宽度
下面是相同的代码段:
<?php
/*www.ashishrevar.com*/
/*Function to create thumbnails*/
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the “desired height” of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, “virtual” image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
}
make_thumb($src, $dest, $desired_width);
?>
在php中,最简单的方法是使用imagejpeg()
函数
在我的一个解决方案中,我使用这个函数创建了图像缩略图,在这个函数中我可以指定高度和宽度
下面是相同的代码段:
<?php
/*www.ashishrevar.com*/
/*Function to create thumbnails*/
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the “desired height” of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, “virtual” image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
}
make_thumb($src, $dest, $desired_width);
?>
使用文本编辑器查看图像的源代码;你可能会看到一条PHP错误消息,就像我说的:“它完美地生成并保存了缩略图”啊,所以你把代码添加到了一个HTML页面中。那是行不通的;您需要将每个结果嵌入到
标记中。您可以使用数据URI动态地显示它们,但在Internet Explorer中这不会很好地工作,在旧版本中也不会工作。脚本顶部的空白是问题所在。非常感谢您使用文本编辑器查看图像的源代码;你可能会看到一条PHP错误消息,就像我说的:“它完美地生成并保存了缩略图”啊,所以你把代码添加到了一个HTML页面中。那是行不通的;您需要将每个结果嵌入到
标记中。您可以使用数据URI动态地显示它们,但在Internet Explorer中这不会很好地工作,在旧版本中也不会工作。脚本顶部的空白是问题所在。非常感谢你的努力,还是同样的错误图像无法显示,因为它包含错误
@Goldie然后如前所述,查看图像的源代码以查看错误还是相同的错误图像无法显示,因为它包含错误
@Goldie然后如前所述,查看图像的源代码,看看有什么错误我无法相信!脚本顶部有一个空格。我现在感到羞愧:)我真不敢相信!脚本顶部有一个空格。我现在感到惭愧:)