96编辑器旗下产品
找图
终身
ID:0
到期时间:永久
退出登录
您的位置:首页 > 百科管理 > 详情

php上传图片并压缩-thinkphp如何做图片压缩呢?

原创:找图网 2023-05-03 17:21:57
  • php压缩图片

  • 比如(什么?)上面有不同大小的图片--------语文表达缺主语,含糊导致无法理解。

    服务器上面?客户机上面?具体什么软件环境上面?

    “有不同大小的图片”,已经存在的图片通常已经压缩过的,像JPEG更是有损压缩。再次压缩必定再次会降低画质。PHP可以再次处理图片,但画质和存储大小不能兼得,画质好就存储大,要存储小就画质差,根据自己的画质需求处理。

  • thinkphp如何做图片压缩呢?

  • 在上传图片的时候先看看图片有多大,一般来说导航幻灯片的图片单张大小尽量不超100k,产品图不超过20k,这样加载还慢的话就用ajax后加载方法,可以是滚动加载之类,但是对蜘蛛抓取页面并不是很友好。

    至于你说的用tp把图片压缩,那只能是将图片的尺寸改成你想要的尺寸,大小的话是web所用格式大小,等页面加载完你又换原图,这样相当于又加载了一遍,还不如做ajax滚动加载。

  • PHP网站上传图片自动压缩,怎么编程啊,求指

  • 这里会使用到三个文件:

    :连接数据库

    test_:执行SQL语句

    upload_:上传图片并压缩

    三个文件代码如下:

    连接数据库:

    <?php

    $db_host = '';

    $db_user = '';

    $db_psw = '';

    $db_name = '';

    $db_port = '';

    $sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name);

    $q="set names utf8;";

    $result=$sqlconn->query($q);

    if (mysqli_connect_errno()) {

     printf("Connect failed: %s\n", mysqli_connect_error());

     exit();

    }

    ?>

    当然使用一些封装的数据库类也是可以的。

    执行SQL语句:test_

    <?php

    require ("");

    require ("upload_");

    $real_img=$uploadfile; 

    $small_img=$uploadfile_resize;

    $insert_sql = "insert into img (real_img,small_img) values (?,?)";

    $result = $sqlconn -> prepare($insert_sql);

    $result -> bind_param("ss", $real_img,$small_img);

    $result -> execute();

    ?>

    上传图片并压缩:upload_

    <?php 

    //设置文件保存目录

    $uploaddir = "upfiles/"; 

    //设置允许上传文件的类型

    $type=array("jpg","gif","bmp","jpeg","png"); 

     

    //获取文件后缀名函数 

    function fileext($filename) 

     return substr(strrchr($filename, '.'), 1); 

     

    //生成随机文件名函数 

    function random($length) 

     $hash = 'CR-'; 

     $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; 

     $max = strlen($chars) - 1; 

     mt_srand((double)microtime() * 1000000); 

     for($i = 0; $i < $length; $i++) 

     { 

      $hash .= $chars[mt_rand(0, $max)]; 

     } 

     return $hash; 

     

    $a=strtolower(fileext($_FILES['filename']['name'])); 

     

    //判断文件类型 

    if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type)) 

     $text=implode(",",$type); 

     $ret_code=3;//文件类型错误

     $page_result=$text;

     $retArray = array('ret_code' => $ret_code,'page_result'=>$page_result);

     $retJson = json_encode($retArray);

     echo $retJson;

     return;

     

    //生成目标文件的文件名 

    else

     $filename=explode(".",$_FILES['filename']['name']); 

     do

     { 

      $filename[0]=random(10); //设置随机数长度 

      $name=implode(".",$filename); 

      //$name1=$name.".Mcncc"; 

      $uploadfile=$uploaddir.$name; 

     } 

     

     while(file_exists($uploadfile)); 

     

     if (move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile)) 

     { 

      if(is_uploaded_file($_FILES['filename']['tmp_name'])) 

      {

       $ret_code=1;//上传失败

      } 

     else

     {//上传成功

      $ret_code=0;

     } 

     } 

    $retArray = array('ret_code' => $ret_code);

    $retJson = json_encode($retArray);

    echo $retJson;

    }

     

    //压缩图片

     

    $uploaddir_resize="upfiles_resize/";

    $uploadfile_resize=$uploaddir_resize.$name;

     

    //$pic_width_max=120;

    //$pic_height_max=90;

    //以上与下面段注释可以联合使用,可以使图片根据计算出来的比例压缩

     

    $file_type=$_FILES["filename"]['type'];

     

    function ResizeImage($uploadfile,$maxwidth,$maxheight,$name)

    {

     //取得当前图片大小

     $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);

       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,$name);

      ImageDestroy ($uploaddir_resize);

     }

     else

     {

      ImageJpeg ($uploadfile,$name);

     }

    }

     

     

     

    if($_FILES["filename"]['size'])

    {

     if($file_type == "image/pjpeg"||$file_type == "image/jpg"|$file_type == "image/jpeg")

     {

      //$im = imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);

      $im = imagecreatefromjpeg($uploadfile);

     }

     elseif($file_type == "image/x-png")

     {

      //$im = imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);

      $im = imagecreatefromjpeg($uploadfile);

     }

     elseif($file_type == "image/gif")

     {

      //$im = imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);

      $im = imagecreatefromjpeg($uploadfile);

     }

     else//默认jpg

     {

      $im = imagecreatefromjpeg($uploadfile);

     }

     if($im)

     {

      ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);

      

      ImageDestroy ($im);

     }

    ?>

    请按照现实情况更改,test_中对应的信息。

    望采纳,谢谢。

    最新文章 更多
    最新素材 更多
    公司介绍 网站地图 图片资讯
    Copyright © 2010-2022 山东找图网络科技有限公司  鲁ICP备18007836号-3  邮箱:15653358549@163.com