关于网站后台缩略图综合解决方法

这两天给一个iphone 的应用程序写支持数据时遇到的关于缩略图的问题!
写出来分享一下. 直接上代码.

我们要达到这样的效果!

功能点
1. 图片上传的格式与大小尺寸是变化的,所以我们需要一个程序来实缩略图(php)
2. 常用的功能封装成jquery插件形式来使用。

html结构代码

编号 标题 来源 日期 缩略图
12 为什么是黄小明' 新浪 2011-01-15 upload/thumbnail/2011/01/15/1295081305.jpg
11 测度2730211069556' 新浪 2011-01-15 upload/thumbnail/2011/01/15/1295095094.jpg
编号 标题 来源 日期 缩略图

jquery插件代码

(function($){
    /**
     * 显示缩略图插件
     * 接收缩略图php地址
     * @author  mark.chen maxtank@gmail.com www.simcn.com
     * @date 2011-1-17
     **/
    $.fn.thumb = function(thumb){
        $(this).hover(function(){
            var url = $(this).text();
            var isAppend = $(this).find('span')[0];
            if(!isAppend){
                $(this).append($(''));
            }else{
                $(this).find('span').show();
            }
            //console.log("url的值为: %s", url);
        },
        function(){
            $(this).find('span').hide();
        });
    }
})(jQuery);

//使用方法
//$('.pw').thumb('thumb.php');

php生成缩略图代码(部分代码来源于互联网,也不知道是谁发表的)

/**
 * 生成缩略图
 * http://www.simcn.com
 * @param $_GET['f'] 图片地址
 * @param $_GET['w'] 图片宽度 默认为200
 * @param $_GET['h'] 图片高度 默认为200
 * @author mark.chen maxtank@gmail.com
 * @version 1.0
 **/

ini_set('memory_limit', '-1');

//传入参数
$width  = isset($_GET['w']) ? intval($_GET['w']) : '200' ;
$height = isset($_GET['h']) ? intval($_GET['h']) : '200' ;
$f		= trim($_GET['f']);

if(empty($f)){
	exit('需要传入f');
}

$info_orig = getimagesize($f);
$width_orig  = $info_orig[0];
$height_orig = $info_orig[1];

header('Content-type: '.$info_orig['mime']);
//header('Content-type: image/jpeg');
//header('Content-type: image/png');

$ratio_orig = $width_orig/$height_orig;
if($width/$height > $ratio_orig) {
   $width_ = $width;
   $width = $height*$ratio_orig;
   $pos_x = intval(($width_ - $width)/2); //位移X轴
   $pos_y = 0;
} else {
   $height_ = $height;
   $height = $width/$ratio_orig;
   $pos_x = 0;
   $pos_y = intval(($height_ - $height)/2); //位移Y轴
}

// 建立画布
$im = imagecreatetruecolor(200, 200);
$color = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $color);		//填充背景为白色
//imagecolortransparent($im,$color);	//将背景透明化

switch($info_orig['mime']){
	case 'image/jpeg':
		$image  = imagecreatefromjpeg($f);
		imagecopyresampled($im, $image, $pos_x, $pos_y, 0, 0, $width, $height, $width_orig, $height_orig);
		imagejpeg($im, null, 100);
		break;
	case 'image/png':
		$image  = imagecreatefrompng($f);
		imagecopyresampled($im, $image, $pos_x, $pos_y, 0, 0, $width, $height, $width_orig, $height_orig);
		imagepng( $im );
		break;
	case 'image/gif':
		$image  = imagecreatefromgif($f);
		imagecopyresampled($im, $image, $pos_x, $pos_y, 0, 0, $width, $height, $width_orig, $height_orig);
		imagegif($im, null, 100);
		break;
}

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>