日期:2014-05-17  浏览次数:20559 次

遇到麻烦 求高手
[code=PHP][/code]
 <?php 
include("conn.php");
if(isset($_GET['page'])){
$page=$_GET['page'];
}else{
$page=1;
}
?>
  <?php
if($page)
$page_size=4;
$query="select * from chanpin";
$yue=mysql_query($query);
$message_count=mysql_num_rows($yue);
$page_count=ceil($message_count/$page_size);
$offset=($page-1)*$page_size;
$query="select * from chanpin where id order by id desc limit $offset, $page_size";
$arr=mysql_query($query,$conn);?>
<?php
while($result=mysql_fetch_array($arr)){
?>
  <tr align="center">
  <td width="38%">名字:<?php echo $result['name'] ;?> | 大小:<?php echo $result['size']; ?> </td><td width="15%" align="right" valign="top">上传时间:</td><td width="47%" align="left"><?php echo $result['shij'];?></td></tr>
  <tr align="center">
  <td><img src="<?php echo $result['lujing']; ?>" width="300" height="200" /></td>
  <td align="right" valign="middle">产品说明:</td><td align="left" valign="top"><?php echo $result['chanpsm']; ?></td>
   
  </tr>
  <?php
}
?
我这样调用图片是出来了 但是图片失真了 我应该如果处理呀?怎么样才能才能不要图片失真?

------解决方案--------------------
width="300" height="200" 
你限制了大小,当然会失真。
------解决方案--------------------
写了个JS处理方法,把这段代码放到head标签中试试。(没仔细调试,可能还存在问题,明天再看看)
HTML code
<script type="text/javascript">
function getImgSize(img) {
    var result = {};
    var w = img.width;
    var h = img.height;
    if (w > 300) {
        if (h <= 200) {
            result.width = 300;
            result.height = Math.ceil(parseInt(h) * 200 / 300);
        }
        else {
            if (w / h == 1.5) {
                result.width = 300;
                result.height = 200;
            }
            else if (w / h > 1.5) {
                result.width = 300;
                result.height = 0;
            }
            else {
                result.width = 0;
                result.height = 200;
            }
        }
    }
    else {
        if (h <= 200) {
            result.width = w;
            result.height = h;
        }
        else {
            result.height = 200;
            result.width = Math.ceil(parseInt(w) * 200 / h);
        }
    }
    return result;
}
window.onload = function() {
    var obj = document.getElementsByTagName('table')[0].getElementsByTagName('img');
    for (var i = 0; i < obj.length; i ++) {
        var wh = getImgSize(obj[i]);
        if (wh.width > 0) obj[i].style.width = wh.width + 'px';
        if (wh.height > 0) obj[i].style.width = wh.height + 'px';
    }
}
</script>

------解决方案--------------------
只需对 img 单边控制就能保持原来的比例

list($width, $height) = getimagesize($result['lujing']);//可能需对$result['lujing']中的路径做适当调整

...<img src="<?php echo $result['lujing']; ?>" <?php echo $width>$height*1.5 ? 'width="300"' : 'height="200"' ?> />....

使用 js 也是一样


------解决方案--------------------
<?php
$a_size = getimagesize('1.jpg');
print_r($a_size);
?>
利用该函数得到的结果去对控制图片的大小
------解决方案--------------------
你把PHP生成的表格代码发上来啊。