加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_丽江站长网 (http://www.0888zz.com/)- 科技、建站、数据工具、云上网络、机器学习!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php上传图片并压缩的实现技巧

发布时间:2022-07-18 14:53:35 所属栏目:PHP教程 来源:互联网
导读:本文实例讲解了php上传图片并压缩的实现方法,之前一篇《PHP实现图片上传并压缩》已经为大家进行了简单介绍,此次实现上传图片然后按照比例缩略图,指定缩略图的最大高度或者最大宽度,具体内容如下。 实现代码: ?php function _UPLOADPIC($upfile, $maxsiz
  本文实例讲解了php上传图片并压缩的实现方法,之前一篇《PHP实现图片上传并压缩》已经为大家进行了简单介绍,此次实现上传图片然后按照比例缩略图,指定缩略图的最大高度或者最大宽度,具体内容如下。
 
  实现代码:
 
  <?php  
  function _UPLOADPIC($upfile, $maxsize, $updir, $newname = 'date') {  
      
   if ($newname == 'date')  
   $newname = date ( "Ymdhis" ); //使用日期做文件名  
   $name = $upfile ["name"];  
   $type = $upfile ["type"];  
   $size = $upfile ["size"];  
   $tmp_name = $upfile ["tmp_name"];  
     break;  
   }  
   if (emptyempty ( $extend )) {  
   echo ( "警告!只能上传图片类型:GIF JPG PNG" );  
   exit ();  
   }  
   if ($size > $maxsize) {  
   $maxpr = $maxsize / 1000;  
   echo ( "警告!上传图片大小不能超过" . $maxpr . "K!" );  
   exit ();  
   }  
   if (move_uploaded_file ( $tmp_name, $updir . $newname . $extend )) {  
   return $updir . $newname . $extend;  
   }  
  }  
     
  function show_pic_scal($width, $height, $picpath) {  
   $imginfo = GetImageSize ( $picpath );  
   $imgw = $imginfo [0];  
   $imgh = $imginfo [1];  
      
   $ra = number_format ( ($imgw / $imgh), 1 ); //宽高比  
   $ra2 = number_format ( ($imgh / $imgw), 1 ); //高宽比  
      
     
   if ($imgw > $width or $imgh > $height) {  
   if ($imgw > $imgh) {  
    $newWidth = $width;  
    $newHeight = round ( $newWidth / $ra );  
      
   } elseif ($imgw < $imgh) {  
    $newHeight = $height;  
    $newWidth = round ( $newHeight / $ra2 );  
   } else {  
    $newWidth = $width;  
    $newHeight = round ( $newWidth / $ra );  
   }  
   } else {  
   }  
  /**  
  * 创建图片,返回资源类型  
  * @param string $src 图片路径  
  * @return resource $im 返回资源类型  
  * **/
  function create($src)  
  {  
   $info=getImageInfo($src);  
   switch ($info[2])  
   {  
   case 1:  
    $im=imagecreatefromgif($src);  
    break;  
   case 2:  
    $im=imagecreatefromjpeg($src);  
    break;  
   case 3:  
    $im=imagecreatefrompng($src);  
      
  function resize($src,$w,$h)  
  {  
   $temp=pathinfo($src);  
   $name=$temp["basename"];//文件名  
   $dir=$temp["dirname"];//文件所在的文件夹  
   $extension=$temp["extension"];//文件扩展名  
   $savepath="{$dir}/{$name}";//缩略图保存路径,新的文件名为*.thumb.jpg  
     
   //获取图片的基本信息  
   $info=getImageInfo($src);  
   $width=$info[0];//获取图片宽度  
   $height=$info[1];//获取图片高度  
   $per1=round($width/$height,2);//计算原图长宽比  
   $per2=round($w/$h,2);//计算缩略图长宽比  

(编辑:应用网_丽江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读