zoukankan      html  css  js  c++  java
  • PHP.18-图片等比例缩放

                                 图片等比例缩放

    自定义函数ImageUpdateSize($pricname, $maxx, $maxy, $pre) 

      1、$pricname:被缩放的图片源(路径);2、$maxx,$maxy:缩放后最大的宽度、高度;3、$pre:缩放后图片的前缀名

    思路与部分代码
    //1、获取图片基本信息
      $info = getimagesize($pricname);
      $w = $info[0]; //源图片宽度
      $h = $info[1]; //源图片高度
    // $info['2']存储的是图片的类型:1=GIF, 2=JPG, 3=PNG

    //2、获取图片类型并为此创建对应类型的新图片资源(JPG GIF PNG) <= imagecopyresampled()进行缩放处理的函数语法要求
    //imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
      //参数:dst_image目标图象连接资源 src_image源图象连接资源
      //dst_x目X 坐标点 dst_y目标Y坐标点 src_x源的X坐标点 src_y源的Y坐标点
      //dst_w目标宽度 dst_h目标高度 src_w源图象的宽度 src_h源图象的高度

      switch($info[2]){
        imagecreatefromgif($pricname);
      
        default:
        die("图片类型错误!");

    //3、计算出缩放比例
      if(($maxx/$w) > ($maxy/$h)){ //假设原宽高(600:300) 固定最大缩放宽高=100 (100/600)(100/300)取值最小那个
        $b = $maxy/$h;
      }else{
        $b = $maxx/$w;
         }
    //4、计算缩放后尺寸,floor()舍去小数部分取整
       $nw = floor($w * $b);
       $nh = floor($h * $b);

    //5、创建一个新的图片源作为目标图像
      $nim = imagecreatetruecolor($nw, $nh);

    //6、执行等比例缩放,(结果为:拷贝成新图像nim)
      $npricname = imagecopyresampled($nim, $im, 0,0,0,0, $nw, $nh, $w, $h);

    //7、输出图像,根据源图像的类型,输出对应的类型
      $picinfo = pathinfo($pricname);
      $new_name = $picinfo["dirname"]."/".$pre.$picinfo["basename"]; //文件名
      switch($info[2])

    //8、销毁图像
      imagedestroy($im);
      imagedestroy($nim);

    echo ImageUpdateSize("./image/1.jpg", 100, 100, "s_");

     1 <?php
     2     function ImageUpdateSize($pricname, $maxx, $maxy, $pre)
     3     {
     4         //1、获取图片基本信息
     5         $info = getimagesize($pricname);    
     6         $w = $info[0];            //源图片宽度
     7         $h = $info[1];            //源图片高度
     8     //    var_dump($info);
     9     //  $info['2']存储的是图片的类型:1=GIF, 2=JPG, 3=PNG
    10     
    11         //2、获取图片类型并为此创建对应类型的新图片资源(JPG GIF PNG)    <= imagecopyresampled()进行缩放处理的函数语法要求
    12         //imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
    13         //参数:dst_image目标图象连接资源 src_image源图象连接资源 
    14         //dst_x目X 坐标点  dst_y目标Y坐标点 src_x源的X坐标点 src_y源的Y坐标点 
    15         //dst_w目标宽度  dst_h目标高度 src_w源图象的宽度 src_h源图象的高度
    16         
    17         switch($info[2]){
    18             case 1:
    19                 $im = imagecreatefromgif($pricname);
    20                 break;
    21             case 2:
    22                 $im = imagecreatefromjpeg($pricname);
    23                 break;
    24             case 3:
    25                 $im = imagecreatefrompng($pricname);
    26                 break;
    27             default:
    28                 die("图片类型错误!");
    29         }
    30         
    31         //3、计算出缩放比例
    32         if(($maxx/$w) > ($maxy/$h)){    //假设原宽高(600:300) 固定最大缩放宽高=100  (100/600)(100/300)取值最小那个
    33             $b = $maxy/$h;
    34         }else{
    35             $b = $maxx/$w;
    36         }
    37         //4、计算缩放后尺寸,floor()舍去小数部分取整
    38         $nw = floor($w * $b);
    39         $nh = floor($h * $b);
    40         
    41         //5、创建一个新的图片源作为目标图像
    42         $nim = imagecreatetruecolor($nw, $nh);
    43         
    44         //6、执行等比例缩放,(结果为:拷贝成新图像nim)
    45         $npricname = imagecopyresampled($nim, $im, 0,0,0,0, $nw, $nh, $w, $h);
    46         
    47         //7、输出图像,根据源图像的类型,输出对应的类型
    48         $picinfo = pathinfo($pricname);
    49         $new_name = $picinfo["dirname"]."/".$pre.$picinfo["basename"];    //文件名
    50         switch($info[2]){
    51             case 1:
    52                 imagegif($nim, $new_name);
    53                 break;
    54             case 2:
    55                 imagejpeg($nim, $new_name);
    56                 break;
    57             case 3:
    58                 imagepng($nim, $new_name);
    59                 break;
    60         }
    61         
    62         //8、销毁图像
    63         imagedestroy($im);
    64         imagedestroy($nim);
    65     }
    66     
    67     echo ImageUpdateSize("./image/1.jpg", 100, 100, "s_");
    68 ?>
    View Code
  • 相关阅读:
    Spring Cloud Hystrix Dashboard的使用 5.1.3
    Spring Cloud Hystrix 服务容错保护 5.1
    Spring Cloud Ribbon 客户端负载均衡 4.3
    Spring Cloud 如何实现服务间的调用 4.2.3
    hadoop3.1集成yarn ha
    hadoop3.1 hdfs的api使用
    hadoop3.1 ha高可用部署
    hadoop3.1 分布式集群部署
    hadoop3.1伪分布式部署
    KVM(八)使用 libvirt 迁移 QEMU/KVM 虚机和 Nova 虚机
  • 原文地址:https://www.cnblogs.com/zixuanfy/p/6701518.html
Copyright © 2011-2022 走看看