zoukankan      html  css  js  c++  java
  • php上传图片后,如果图片大于50kb则进行压缩

    if($size > 51200){
      //压缩图片
      $uploadfile = $imgDir.$filename;

      //图片路径
      $uploaddir_resize = 'account_imgs/';
      $uploadfile_resize = $uploaddir_resize.$filename;
      //echo $uploaddir_resize;
      function resizeImage($uploadfile,$maxwidth,$maxheight,$filename)
      {
        //取得当前图片大小
        $width = imagesx($uploadfile);
        $height = imagesy($uploadfile);

        //压缩比值

        $i=0.5;
        //生成缩略图的大小
        if(($width > $maxwidth) || ($height > $maxheight))
        {
        /*
          $widthratio = $maxwidth/$width;
          $heightratio = $maxheight/$height;

          if($widthratio < $heightratio)
          {
            $ratio = $widthratio;
          }
          else
          {
            $ratio = $heightratio;
          }

          $newwidth = $width * $ratio;
          $newheight = $height * $ratio;
          */
          $newwidth = $width * $i;
          $newheight = $height * $i;
          if(function_exists("imagecopyresampled"))
          {
            $uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);
            //echo $uploaddir_resize;
            imagecopyresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
          }
          else
          {
            $uploaddir_resize = imagecreate($newwidth, $newheight);
            imagecopyresized($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
          }

          ImageJpeg ($uploaddir_resize,$filename);
          ImageDestroy ($uploaddir_resize);
        }
        else
        {
          ImageJpeg ($uploadfile,$filename);
        }
      }
      $pic_width_max = 120;
      $pic_height_max = 90;
      $file_type = $_FILES["file"]['type'];
      if($_FILES["file"]['size']){
        if($file_type == "image/pjpeg"||$file_type == "image/jpg"|$file_type == "image/jpeg")
        {
          //$im = imagecreatefromjpeg($_FILES['file']['tmp_name']);
          $im = imagecreatefromjpeg($uploadfile);
        }
        else if($file_type == "image/x-png")
        {
          //$im = imagecreatefrompng($_FILES['file']['tmp_name']);
          $im = imagecreatefromjpeg($uploadfile);
        }
        else if($file_type == "image/gif")
        {
          //$im = imagecreatefromgif($_FILES['file']['tmp_name']);
          $im = imagecreatefromjpeg($uploadfile);
        }
        else//默认jpg
        {
          $im = imagecreatefromjpeg($uploadfile);
        }

        if($im)
        {
          ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);
          ImageDestroy ($im);
        }
      }
    }

  • 相关阅读:
    web前端(4)—— 常用标签1
    web前端(3)—— html标签及web页面结构
    web前端(2)—— 前端技术介绍
    web前端(1)——了解什么是前端,以及与后端的关系
    数据库之redis篇(2)—— redis配置文件,常用命令,性能测试工具
    数据库之redis篇(1)—— redis数据库安装,简单使用
    数据库之mysql篇(6)—— mysql常用函数函数/自定义函数
    洗礼灵魂,修炼python(91)-- 知识拾遗篇 —— pymysql模块之python操作mysql增删改查
    LINUX新建和增加SWAP分区
    Configuring SSL for SAP Host Agent on UNIX
  • 原文地址:https://www.cnblogs.com/nullman/p/6293242.html
Copyright © 2011-2022 走看看