


thinkphp3.2+cropper implements uploading and cutting multiple pictures (code example)
The content of this article is to introduce thinkphp3.2 cropper to upload multiple pictures and cut pictures (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.
First let’s take a look at the screenshot of the effect achieved:
Click the plus sign to continue uploading the second picture
Code part:
Front-end code:
<--引入cropper相关文件--> <link rel="stylesheet" href="/home/style/cropper.css" /> <link href="/home/tupian/css/bootstrap.min.css" rel="stylesheet"> <link href="/home/tupian/css/cropper.min.css" rel="stylesheet"> <link href="/home/tupian/css/main.css" rel="stylesheet"> <script src="/home/tupian/js/jquery.min.js"></script> <script src="/home/tupian/js/bootstrap.min.js"></script> <script src="/home/tupian/js/cropper.min.js"></script> <script src="/home/tupian/js/main.js"></script> <script src="http://www.jq22.com/jquery/jquery-2.1.1.js"></script> <div class="form-group"> <label class="col-sm-3 control-label">照片:</label> <div class="col-sm-9"> <div class="a-up"> <--是第二张图片加号的效果--> <input type="file" onchange="onChangeFn(this); "name="image[]" accept="image/jpeg,image/png" required /> </div> <div class="error">*请添加图片</div> </div> </div> <--模态框--> <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <div class="modal-header"> <h5 class="modal-title" id="modalLabel">剪裁图片</h5> </div> <div class="modal-body"> <div class="img-container"> <img id="image" style="max-width:100%;max-height:350px;"><--这里的最大高度一定要设置--> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary" id="crop">Crop</button> </div> </div> </div> </div> function onChangeFn (obj) { var _this=$(obj), _upload=_this.parent(); currentUpload=_upload; // <img src="images/erweima.png" class="upload-img"> //截取开始 var files = obj.files; var done = function (url) { image.src = url; $modal.modal('show'); }; var reader; var file; var url; if (files && files.length > 0) { file = files[0]; if (URL) { done(URL.createObjectURL(file)); } else if (FileReader) { reader = new FileReader(); reader.onload = function (e) { done(reader.result); }; reader.readAsDataURL(file); } } } var image = document.getElementById('image'); var $modal = $('#modal'); var currentUpload; var cropper; $modal.on('shown.bs.modal', function () { cropper = new Cropper(image, { aspectRatio: 16/9, zoomable:true, zoomOnWheel:true, viewMode: 0, }); }).on('hidden.bs.modal', function () { cropper.destroy(); cropper = null; }); document.getElementById('crop').addEventListener('click', function () { var canvas; $modal.modal('hide'); if (cropper) { canvas = cropper.getCroppedCanvas({ width: 800, height: 500, }); // avatar.src = canvas.toDataURL(); <--读取图片--> var reader = new FileReader(); reader.onload = function(evt) { currentUpload.before('<div class="divimg"><img src="' + evt.target.result + '" class="upload-img"><input type="hidden" value="' + evt.target.result + '" name="picc[]"><button type="button" class="close" onclick="closeFun(this,1);"></button></div>'); } canvas.toBlob(function (result) {reader.readAsDataURL(result);},"image/jpeg"); // console.log(canvas); if (currentUpload.next().css('display')=='block') { currentUpload.next().css('display','none') } var strHtml='<div class="a-up"><input type="file" onchange="onChangeFn(this);" name="image[]" accept="image/jpeg,image/png" id="pic" /></div>'; currentUpload.after(strHtml); currentUpload.hide(); } });
Backend part:
<--接收过来的是base64的图片,速度较慢,应该是转成blob图片再传给后台,还没做> $base64_image_content = $_POST["picc"]; foreach($base64_image_content as $k=>$v){ $imageName = date("His",time())."_".rand(1111,9999).'.png'; $dir = date('Ymd'); $path = 'uploads/'.$dir; if (!is_dir($path)){ //判断目录是否存在 不存在就创建 mkdir($path,0777,true); } if (strstr($v,",")){ $base64_image_contents = explode(',',$v); $base64_image = $base64_image_contents[1]; $root = $_SERVER['DOCUMENT_ROOT']."/".$path."/". $imageName; $r = file_put_contents($root, base64_decode($base64_image));//返回的是字节数 } $image[] = '/'.$path.'/'. $imageName; } foreach ($image as $kk=>$vv) { $images[] = json_encode($vv, true); } $data["image"] = '['.implode(',',$images).']';
//将base64格式图片转换为文件形式 function dataURLtoBlob(dataurl) { var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while(n--){ u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], {type:mime}); }
Summary: The above is the entire content of this article, I hope it can be helpful to everyone learning helps. For more related video tutorials, please visit: php tutorial!
The above is the detailed content of thinkphp3.2+cropper implements uploading and cutting multiple pictures (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

Steps to implement image upload and display using CakePHP framework Introduction: In modern web applications, image upload and display are common functional requirements. The CakePHP framework provides developers with powerful functions and convenient tools, making it simple and efficient to upload and display images. This article will introduce you to how to use the CakePHP framework to upload and display images. Step 1: Create a file upload form First, we need to create a form in the view file for users to upload images. The following is an example of

How to handle image uploading and compression in Vue technology development In modern web applications, image uploading is a very common requirement. However, due to network transmission and storage reasons, directly uploading original high-resolution images may result in slow upload speeds and a large waste of storage space. Therefore, uploading and compressing images is very important. In Vue technology development, we can use some ready-made solutions to handle image uploading and compression. The following will introduce how to use vue-upload-comone

How to use PHP and Vue to implement the image upload function. In modern web development, the image upload function is a very common requirement. This article will introduce in detail how to use PHP and Vue to implement the image upload function, and provide specific code examples. 1. Front-end part (Vue) First, you need to create a form for uploading images on the front-end. The specific code is as follows:<template><div><inputtype="fil

With the development of the Internet, image uploading has become an essential feature in website and application development. In the field of PHP, ThinkPHP6 has become a very popular development framework. In this article, we will introduce how to use ThinkPHP6 to implement image upload. 1. Create project and controller First, we need to create a new ThinkPHP6 project. You can use Composer to install it, or you can download the latest version from the official website. After the installation is complete, enter in the console

How to use PHP to implement a simple online image upload and display system. Image upload and display system is one of the commonly used functions of modern websites. This function can be quickly implemented using PHP during the development process. This article will introduce how to use PHP to write a simple online image upload and display system, and provide specific code examples. 1. Create database and tables First, we need to create a database and tables to store uploaded image information. Use the following SQL statement to create a table named "images" and set

How to solve the problem of image upload preview modal box display in Vue development. In Vue development, we often encounter the need to upload images and preview them. In this case, a common question is how to display the preview image in the modal box after uploading the image. This article will introduce a way to solve this problem. First, we need to add a file upload input element to the Vue component to select the image file to upload. By listening to the change event of file upload, we can get the image selected by the user

How to implement image uploading and cropping in Vue technology development requires specific code examples. In modern web development, image uploading and image cropping are one of the common requirements. As a popular front-end framework, Vue.js provides a wealth of tools and plug-ins to help us achieve these functions. This article will introduce how to implement image uploading and cropping in Vue technology development, and provide specific code examples. The implementation of image upload can be divided into two steps: selecting images and uploading images. In Vue, you can use third-party plugins to simplify this
