ThinkPHP 上传图片压缩原图片?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ThinkPHP 上传图片压缩原图片?相关的知识,希望对你有一定的参考价值。

public function save()
// 获取参数
$data = $_POST["data"];
// 去掉前缀
$data = substr($data, 22);
// 图片解码
$image = base64_decode($data);
// 图片标识
$token = md5($image);
// 保存到文件
$root = APP_PATH."Public/upload/".date("Ymd")."/";
if(!is_dir($root))
mkdir($root);

$file = $root.$token.".png";
$len = file_put_contents($file, $image);
// 输出结果
if($len)
$this->ajaxReturn(array(
"status" => "1",
"url" => "/Public/upload/".date("Ymd")."/".$token.".png",
"token" => $token
));
else
$this->ajaxReturn(array(
"status" => "0"
));

exit;


public function getImageUrl($data,$type)
if($type=="image/jpeg")
$type_ext = '.jpg';
else if($type == "image/png")
$type_ext = '.png';
else
return false;

try
$data = substr($data,22);
$image = base64_decode($data);
$token = md5($image);
$root = APP_PATH."Public/upload/".date("Ymd")."/";
if(!is_dir($root))
mkdir($root);

$file = $root.$token.$type_ext;
if(file_put_contents($file,$image))
return "/Public/upload/".date("Ymd")."/".$token.$type_ext;
else
return "";

catch(\Exception $exception)
var_dump($exception->getMessage());
exit();




怎么增加一个压缩上传图片的功能?
谢谢用的是
Thinkphp3.1.3

参考技术A

来直接上代码,基本上能懂!

上传+压缩

本回答被提问者和网友采纳

thinkphp 多图片上传 单图片上传

不管是单图片上传还是多图片上传都必须要引用这两个js

下载地址

链接:http://pan.baidu.com/s/1eStkUt0 密码:asvo

<script src="Public/static/plupload-2.1.9/plupload.full.min.js"></script>
 <script src="Public/static/plupload-2.1.9/i18n/zh_CN.js"></script>

 

多图片上传

HTML模板

                         
  
                          <
div class="controls"> <td><img src="/Public/Liu/images/tujia.png" id="logo"> <ul class="tuji"> <if condition="$good[‘imagelist‘][0] neq ‘‘ "> <volist name="good[‘imagelist‘]" id="vo"> <li> <img class="img" src="{$vo}"> <img class="ico" onclick="del_func(this)" src="/Public/Admin/images/del_btn.png" /> <input type="hidden" name="image[]" value="{$vo}"> </li> </volist> </if> </ul> </td> </div>

js代码

       <script>

        var uploader = new plupload.Uploader({
            "runtimes":"html5,flash,silverlight,html4",
            "flash_swf_url" : "/Public/static/plupload-2.1.9/Moxie.swf",
            "silverlight_xap_url" : "/Public/static/plupload-2.1.9/Moxie.xap",
            "browse_button":$("#logo")[0],//点击触发事件
            "url":"{:U(‘Fileupload/img_upload‘)}",//控制器地址
            "filters" : {
                max_file_size : ‘3M‘,
                mime_types: [
                    {title : "Image files", extensions : "jpg,gif,png,jpeg"}
                ]
            },
            "multi_selection":false,// 多选
            "multipart_params":{"dir":"pic"},

            init:{

                FilesAdded: function(up, files) {

                    this.start();
                },
                FileUploaded:function(up,files,res){
                    var data = $.parseJSON(res.response);
                    var html = $(‘.tuji‘).html();
                    if(data.status == 1){
                        html += ‘<li><img class="img" src="‘+data.data+‘">‘;
                        html += ‘<img class="ico" onclick="del_func(this)" src="/Public/Admin/images/del_btn.png">‘;
                        html += ‘<input type="hidden" name="image[]" value="‘+data.data+‘"></li>‘;

                        $(‘.tuji‘).html(html);
                    }
                },
                Error:function(up,err){
                    alert(err.message);

                }
            }
        });
        uploader.init();
    </script>
    <script>
        function del_func(dom){
                    $(dom).parent(‘li‘).remove();
        }
    </script>

 

 

 

 

单图片上传

HTML模板

                       <div class="form-group">
                                            <label class="control-label"> 尾部显示图 <span class="symbol required"></span> <span class="check-tips"></span></label>
                                            <div class="controls">
                                            <if condition="$info[‘bjt_2‘] eq ‘‘">
                                               <img src="Public/Home/images/jia.png" id="bjt_2" class="ks_img">
                                               <input type="hidden" name="bjt_2" value="" id="yci_2">
                                           <else/>
                                               <img src="{$info[‘bjt_2‘]}" id="bjt_1" class="ks_img">
                                               <input type="hidden" name="bjt_2" value="{$info[‘bjt_2‘]}" id="yci_1">
                                            </if>
                                            </div>
                                        </div>

 

 

js 代码

<script>

      var uploader = new plupload.Uploader({
            "runtimes":"html5,flash,silverlight,html4",
            "flash_swf_url" : "/Public/static/plupload-2.1.9/Moxie.swf",
            "silverlight_xap_url" : "/Public/static/plupload-2.1.9/Moxie.xap",
            "browse_button":$("#bjt_2")[0],//点击触发事件
            "url":"{:U(‘Fileupload/img_upload‘)}",//控制器地址
            "filters" : {
                max_file_size : 3M,
                mime_types: [
                    {title : "Image files", extensions : "jpg,gif,png,jpeg"}
                ]
            },
            "multi_selection":false,// 多选
            "multipart_params":{"dir":"pic"},

            init:{

                FilesAdded: function(up, files) {

                    this.start();
                },
                FileUploaded:function(up,files,res){
                    var data = $.parseJSON(res.response);
                    var html = $(.controls).html();
                if(data.status == 1){
                    $(#bjt_2).attr(src,data.data);
                    $(#yci_2).val(data.data);
                    /*html += ‘<img class="img" src="‘+data.data+‘">‘;
                    html += ‘<img class="ico" onclick="del_func(this)" src="Public/Home/images/del_btn.png">‘;
                    html += ‘<input type="hidden" name="images[]" value="‘+data.data+‘">‘;*/
                    

//                    $(‘.controls‘).html(html);
                }
                },
                Error:function(up,err){
                    alert(err.message);

                }
            }
        });
        uploader.init();
    </script>

 



以上是关于ThinkPHP 上传图片压缩原图片?的主要内容,如果未能解决你的问题,请参考以下文章

thinkphp 关于图片问题

Java实现的上传并压缩图片功能可等比例压缩或原尺寸压缩

我的论坛上传图片为啥压缩太厉害?

springboot整合thumbnailator实现图片压缩

php图片上传为啥要base64上传

flutter 图片压缩