+ -
当前位置:首页 → 问答吧 → 用GD库做缩略图

用GD库做缩略图

时间:2011-11-16

来源:互联网

要求:做一个缩略图上传页面,上传完成,在页面显示缩略图,点击缩略图能看到
原来的大图片。

作者: myl315824   发布时间: 2011-11-16

为嘛不用div+css直接控制
<div>
  <img style="width:100px; height:auto;" onclick="this.style.width='auto';" src="food.jpg" />
</div>

作者: dream1206   发布时间: 2011-11-16

div这个我很熟悉,不过现在学的是GD库,所以要用GD库做缩略图!!原因你懂的,帮帮忙吧!!先谢啦!!哈哈

作者: myl315824   发布时间: 2011-11-16

PHP code

<?php
header('Content-type: image/jpeg');

$filename = 'food.jpg';
$maxSize = 200; // 缩略图最大高度或宽度

// 读取源图像
$source = imagecreatefromjpeg($filename);

// 源图像的宽和高
$width  = imagesx( $source );
$height = imagesy( $source );

// 设置缩略图的大小
if( $width >= $height ){
     $newWidth  = $maxSize;
     $newHeight = (int)( $height * ( $maxSize/$width ));
}
else{
     $newHeight = $maxSize;
     $newWidth  = (int)( $width * ( $maxSize/$height ) );
}

// 创建缩略图
$thumb  = imagecreatetruecolor($newWidth, $newHeight);
// 拷贝 $source 并到 $thumb 
imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// Output
imagejpeg($thumb);
?>


手册上的例子 http://www.php.net/manual/zh/function.imagecopyresampled.php

作者: dream1206   发布时间: 2011-11-16

相关阅读 更多