Home  >  Article  >  Backend Development  >  php和js对数据库图片进行等比缩放

php和js对数据库图片进行等比缩放

WBOY
WBOYOriginal
2016-06-23 13:58:03799browse

JS 对某图片的等比缩放

代码


Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->



最新javascript自动按比例显示图片,按比例压缩图片显示





原图显示(534 X 800)

onload="AutoResizeImage(0,0,this)

534 X 800


3.按高度250宽度250 按比例压缩

onload="AutoResizeImage(250,250,this)"

php和js对数据库图片进行等比缩放


6.如果图片本来的高度和宽度小于压缩的最大高度和宽度,则不会拉大显示图片(按原图显示)

原图444 x 207,压缩为 500 x 600,将保持原图显示

onload="AutoResizeImage(500,600,this)"

php和js对数据库图片进行等比缩放





php对数据库图片的等比缩放

class ImgSF{
 function make_img($img_address){
      //图片的等比缩放
      
      //因为PHP只能对资源进行操作,所以要对需要进行缩放的图片进行拷贝,创建为新的资源
      $src=imagecreatefromjpeg($img_address);
     
      //取得源图片的宽度和高度
      $size_src=getimagesize($img_address);
      $w=$size_src['0'];
      $h=$size_src['1'];
      
     //指定缩放出来的最大的宽度(也有可能是高度)
      $max=300;
      
      //根据最大值为300,算出另一个边的长度,得到缩放后的图片宽度和高度
      if($w > $h){
          $w=$max;
          $h=$h*($max/$size_src['0']);
      }else{
         $h=$max;
          $w=$w*($max/$size_src['1']);
      }
     
     
     //声明一个$w宽,$h高的真彩图片资源
      $image=imagecreatetruecolor($w, $h);
     
     
     //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
      imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']);
     
     //告诉浏览器以图片形式解析
     header('content-type:image/png');
      imagepng($image);
      
     //销毁资源
     imagedestroy($image);
 }
}
$obj=new ImgSF();
$obj->make_img("./img/IMG_20140424_200722.jpg");

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn