p img { max-width:600px; width: expression(this.width > 600 ? “600px” : true); height:auto; }
$(document).ready(function(){ $('div').autoResize({height:750}); }); jQuery.fn.autoResize = function(options) { var opts = { 'width' : 700, 'height': 750 } var opt = $.extend(true, {},opts,options || {}); width = opt.width; height = opt.height; $('img',this).each(function(){ var image = new Image(); image.src = $(this).attr('src');   if(image.width > 0 && image.height > 0 ){ var image_rate = 1; if( (width / image.width) < (height / image.height)){ image_rate = width / image.width ; }else{ image_rate = height / image.height ; } if ( image_rate <= 1){ $(this).width(image.width * image_rate); $(this).height(image.height * image_rate); } } }); }
/*
* @param String $name 要缩放的文件路径
* @param String $filename 生成的文件路径
* @param Int $new_w 新图片宽度
* @param Int $new_h 新图片高度
*/
function createthumb($name,$filename,$new_w,$new_h) { $file_extension = strtolower(pathinfo($name, PATHINFO_EXTENSION)); if (preg_match("/jpg|jpeg/",$file_extension)){$src_img=imagecreatefromjpeg($name);} if (preg_match("/png/",$file_extension)){$src_img=imagecreatefrompng($name);} if (preg_match("/gif/",$file_extension)){$src_img=imagecreatefromgif($name);} $old_x=imageSX($src_img); $old_y=imageSY($src_img); $old_per = $old_x/$old_y; $new_per = $new_w/$new_h; if($new_per >= $old_per){ $thumb_w = $old_x * ($new_h/$old_y); $thumb_h = $new_h; $pst_x = ($new_w-$thumb_w)/2; $pst_y = 0; }else{ $thumb_w=$new_w; $thumb_h = $old_y * ($new_w/$old_x); $pst_y = ($new_h-$thumb_h)/2; $pst_x = 0; } $dst_img=ImageCreateTrueColor($new_w,$new_h); //颜色 $backgroung_color = imagecolorallocate($dst_img, 0xFF, 0xFF, 0xFF); if($new_per >= $old_per){ imagefilledrectangle ($dst_img , 0 , 0 , $pst_x , $new_h , $backgroung_color); imagefilledrectangle ($dst_img , $pst_x+$thumb_w , 0 , $new_w , $new_h , $backgroung_color); }else{ imagefilledrectangle ($dst_img , 0 , 0 , $new_w , $pst_y , $backgroung_color); imagefilledrectangle ($dst_img , 0 , $new_h-$pst_y, $new_w , $new_h , $backgroung_color); } imagecopyresampled($dst_img,$src_img,$pst_x,$pst_y,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if (preg_match("/png/",$file_extension)) { imagepng($dst_img,$filename); } elseif (preg_match("/gif/",$file_extension)) { imagegif($dst_img,$filename); } else { imagejpeg($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); }