Table of Contents
Jcrop介绍
Demo介绍
Home Backend Development PHP Tutorial PHP调整Jcrop截取的上传头像功能

PHP调整Jcrop截取的上传头像功能

Jun 13, 2016 pm 12:22 PM
gt lt name preview

PHP整合Jcrop截取的上传头像功能

先来看看简单Demo效果图
这里写图片描述
这里写图片描述

Jcrop介绍

Jcrop是一个jQuery插件,它能为你的WEB应用程序快速简单地提供图片裁剪的功能。
特点:
1、对所有图片均unobtrusively(无侵入的,保持DOM简洁)
2、支持宽高比例锁定
3、支持 minSize / maxSize设置
4、支持改变选区或移 动选区时的回调(Callback)
5、支持用键盘微调选 区
6、通过API创建互 动,比如动画效果
7、支持CSS样式

详细请自行百度jcrop

Demo介绍

这个Demo选择整合了jcrop的截取图片插件,
上传图片还是使用file表单提交,php后台处理截图保存。

模块目录如下:
├─controller (控制器)
├─uppict (上传图片暂存位置)
├─userpic (截图保存位置)
└─views (视图)
这里写图片描述

视图代码croppic.php如下:

<code class=" hljs xml"><span class="php"><span class="hljs-preprocessor"><?php</span><span class="hljs-comment">/*** 20150520 11:50* 作者:Ro* 修改时间 20150522 13:50* 修改内容 合并上传和截取图片功能*/</span><span class="hljs-comment">//上传文件类型列表</span><span class="hljs-variable">$uptypes</span>=<span class="hljs-keyword">array</span>(    <span class="hljs-string">'image/jpg'</span>,    <span class="hljs-string">'image/jpeg'</span>,    <span class="hljs-string">'image/png'</span>    );<span class="hljs-preprocessor">?></span></span><span class="hljs-tag"><<span class="hljs-title">html</span>></span><span class="hljs-tag"><<span class="hljs-title">head</span>></span>    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"Content-Type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html; charset=utf-8"</span> /></span>    <span class="hljs-tag"><<span class="hljs-title">link</span> <span class="hljs-attribute">rel</span>=<span class="hljs-value">"stylesheet"</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"css/main.css"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/css"</span> /></span>    <span class="hljs-tag"><<span class="hljs-title">link</span> <span class="hljs-attribute">rel</span>=<span class="hljs-value">"stylesheet"</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"css/demos.css"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/css"</span> /></span>    <span class="hljs-tag"><<span class="hljs-title">link</span> <span class="hljs-attribute">rel</span>=<span class="hljs-value">"stylesheet"</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"css/jquery.Jcrop.css"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/css"</span> /></span>    <span class="hljs-tag"><<span class="hljs-title">link</span> <span class="hljs-attribute">rel</span>=<span class="hljs-value">"stylesheet"</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"css/uppic.css"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/css"</span> /></span>    <span class="hljs-tag"><<span class="hljs-title">title</span>></span>头像上传<span class="hljs-tag"></<span class="hljs-title">title</span>></span><span class="hljs-tag"></<span class="hljs-title">head</span>></span><span class="hljs-tag"><<span class="hljs-title">body</span>></span>    <span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"container"</span>></span>        <span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"row"</span>></span>            <span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"span12"</span>></span>                <span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"jc-demo-box"</span>></span>                    <span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"clear_float"</span>></span>                        <span class="php"><span class="hljs-preprocessor"><?php</span>                         <span class="hljs-keyword">if</span>(<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$_GET</span>[<span class="hljs-string">'name'</span>]))                        {                            <span class="hljs-keyword">echo</span><span class="hljs-string">'                            <img  class="float lazy"  src="/static/imghw/default1.png"  data-src="/uppic/uppict/default.jpg" </span alt="PHP调整Jcrop截取的上传头像功能" >.<span>$_GET</span>[<span>'name'</span>].<span>'"  id="target"/>                            <img  class="float lazy"  src="/static/imghw/default1.png"  data-src="/uppic/uppict/default.jpg" </span alt="PHP调整Jcrop截取的上传头像功能" >.<span>$_GET</span>[<span>'name'</span>].<span>'"  id="real_img"   style="max-width:90%"/>                            <div id="preview-pane">                                <div>                                    <img  class="float lazy"  src="/static/imghw/default1.png"  data-src="/uppic/uppict/default.jpg" </span alt="PHP调整Jcrop截取的上传头像功能" >.<span>$_GET</span>[<span>'name'</span>].<span>'" alt="Preview" />                                </div>                            </div>                            '</span>;                        }                        <span>else</span>                        {                            <span>echo</span><span>'                            <img  class="float lazy"  src="/static/imghw/default1.png"  data-src="/uppic/uppict/default.jpg"   id="target"/ alt="PHP调整Jcrop截取的上传头像功能" >                            <img  src="/static/imghw/default1.png"  data-src="/uppic/uppict/default.jpg"  class="lazy"  id="real_img"   style="max-width:90%"/ alt="PHP调整Jcrop截取的上传头像功能" >                            <div id="preview-pane">                                <div class="preview-container">                                    <img class="jcrop-preview float lazy"  src="/static/imghw/default1.png"  data-src="/uppic/uppict/default.jpg"  alt="Preview" />                                </div>                            </div>                            '</span>;                        }                        <span class="hljs-preprocessor">?></span></span>                    <span class="hljs-tag"></<span class="hljs-title">div</span>></span>                    <span class="hljs-tag"><<span class="hljs-title">form</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">"http://<?php echo $_SERVER['HTTP_HOST'];?>/uppic/controller/croppic.php?mothed=up"</span> <span class="hljs-attribute">enctype</span>=<span class="hljs-value">"multipart/form-data"</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">"post"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"upform"</span>></span>                        上传文件:                        <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"upfile"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"file"</span>></span>                        <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"submit"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"上传图片"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"btn btn-large btn-inverse"</span>></span><span class="hljs-tag"><<span class="hljs-title">br</span>></span>                        允许上传的文件类型为:<span class="php"><span class="hljs-preprocessor"><?</span>=implode(<span class="hljs-string">', '</span>,<span class="hljs-variable">$uptypes</span>)<span class="hljs-preprocessor">?></span></span>                    <span class="hljs-tag"></<span class="hljs-title">form</span>></span>                    <span class="php"><span class="hljs-preprocessor"><?php</span>                     <span class="hljs-keyword">if</span>(<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$_GET</span>[<span class="hljs-string">'name'</span>]))                    {                        <span class="hljs-keyword">echo</span> <span class="hljs-string">'                        <form action="http://'</span>.<span class="hljs-variable">$_SERVER</span>[<span class="hljs-string">'HTTP_HOST'</span>].<span class="hljs-string">'/uppic/controller/croppic.php?mothed=crop&name='</span>.<span class="hljs-variable">$_GET</span>[<span class="hljs-string">'name'</span>].<span class="hljs-string">'" method="post" onsubmit="return checkCoords();">                        '</span>;                    }                    <span class="hljs-keyword">else</span>                    {                        <span class="hljs-keyword">echo</span> <span class="hljs-string">'                        <form action="http://'</span>.<span class="hljs-variable">$_SERVER</span>[<span class="hljs-string">'HTTP_HOST'</span>].<span class="hljs-string">'/uppic/controller/croppic.php?mothed=crop&name=default.jpg" method="post" onsubmit="return checkCoords();">                        '</span>;                    }                    <span class="hljs-preprocessor">?></span></span>                        <span class="hljs-comment"><!-- 记录截图坐标和宽高 --></span>                        <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"hidden"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"x"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"x"</span> /></span>                        <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"hidden"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"y"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"y"</span> /></span>                        <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"hidden"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"w"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"w"</span> /></span>                        <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"hidden"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"h"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"h"</span> /></span>                        <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"submit"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"保存"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"btn btn-large btn-inverse"</span>/></span>                    <span class="hljs-tag"></<span class="hljs-title">form</span>></span>                <span class="hljs-tag"></<span class="hljs-title">div</span>></span>            <span class="hljs-tag"></<span class="hljs-title">div</span>></span>        <span class="hljs-tag"></<span class="hljs-title">div</span>></span>    <span class="hljs-tag"></<span class="hljs-title">div</span>></span><span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"js/jquery.min.js"</span>></span><span class="javascript"></span><span class="hljs-tag"></<span class="hljs-title">script</span>></span><span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"js/jquery.Jcrop.js"</span>></span><span class="javascript"></span><span class="hljs-tag"></<span class="hljs-title">script</span>></span><span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"js/crop.js"</span>></span><span class="javascript"></span><span class="hljs-tag"></<span class="hljs-title">script</span>></span><span class="hljs-tag"></<span class="hljs-title">body</span>></span><span class="hljs-tag"></<span class="hljs-title">html</span>></span></code>
Copy after login

视图js代码crop.js如下:

<code class=" hljs javascript"><span class="hljs-comment">/** * 20150518 15:35 * author : Ro * changeDate: 20150519 10:25 * changeContext:修改计算坐标算法 */</span><span class="hljs-comment">/***检测是否有选取一个区域**/</span><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkCoords</span><span class="hljs-params">()</span>{</span>    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">parseInt</span>($(<span class="hljs-string">'#w'</span>).val()))       <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;    alert(<span class="hljs-string">"请截取一个区域再提交保存!"</span>);    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;};jQuery(<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">($)</span>{</span>    <span class="hljs-keyword">var</span> jcrop_api,        boundx,        boundy,    <span class="hljs-comment">// Grab some information about the preview pane</span>    $preview = $(<span class="hljs-string">'#preview-pane'</span>),    $pcnt = $(<span class="hljs-string">'#preview-pane .preview-container'</span>),    $pimg = $(<span class="hljs-string">'#preview-pane .preview-container img'</span>),    <span class="hljs-comment">//这里获取的是装img的div宽高</span>    xsize = $pcnt.width(),    ysize = $pcnt.height();    <span class="hljs-comment">//这里可以设置jcrop的属性,</span>    <span class="hljs-comment">//如当改变截取区域时激活onChange: updatePreview动作等</span>    $(<span class="hljs-string">'#target'</span>).Jcrop({      onChange: updatePreview,      onSelect: updatePreview,      aspectRatio: xsize / ysize    },<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span>{</span>      <span class="hljs-comment">// 用jcrop的getBounds()方法获取真实尺寸</span>      <span class="hljs-keyword">var</span> bounds = <span class="hljs-keyword">this</span>.getBounds();      boundx = bounds[<span class="hljs-number">0</span>];      boundy = bounds[<span class="hljs-number">1</span>];      <span class="hljs-comment">// Store the API in the jcrop_api variable</span>      jcrop_api = <span class="hljs-keyword">this</span>;      <span class="hljs-comment">// Move the preview into the jcrop container for css positioning</span>      $preview.appendTo(jcrop_api.ui.holder);    });    <span class="hljs-comment">//更新jcrop预览视图</span>    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updatePreview</span><span class="hljs-params">(c)</span>    {</span>      <span class="hljs-keyword">if</span> (<span class="hljs-built_in">parseInt</span>(c.w) > <span class="hljs-number">0</span>)      {        <span class="hljs-comment">//下面的比例是div的宽高与截图坐标比</span>        <span class="hljs-keyword">var</span> rx = xsize / c.w;        <span class="hljs-keyword">var</span> ry = ysize / c.h;        <span class="hljs-comment">//改变预览图的大小和显示位置</span>        $pimg.css({          width: <span class="hljs-built_in">Math</span>.round(rx * boundx) + <span class="hljs-string">'px'</span>,          height: <span class="hljs-built_in">Math</span>.round(ry * boundy) + <span class="hljs-string">'px'</span>,          marginLeft: <span class="hljs-string">'-'</span> + <span class="hljs-built_in">Math</span>.round(rx * c.x) + <span class="hljs-string">'px'</span>,          marginTop: <span class="hljs-string">'-'</span> + <span class="hljs-built_in">Math</span>.round(ry * c.y) + <span class="hljs-string">'px'</span>        });        <span class="hljs-keyword">var</span> realWidth =  $(<span class="hljs-string">"#real_img"</span>).width();        <span class="hljs-keyword">var</span> realHeight = $(<span class="hljs-string">"#real_img"</span>).height();    <span class="hljs-comment">//记录位置和宽高</span>        $(<span class="hljs-string">'#x'</span>).val(<span class="hljs-built_in">Math</span>.round( c.x * realWidth / boundx ));        $(<span class="hljs-string">'#y'</span>).val(<span class="hljs-built_in">Math</span>.round( c.y * realHeight / boundy));        $(<span class="hljs-string">'#w'</span>).val(<span class="hljs-built_in">Math</span>.round( c.w * realWidth / boundx ));        $(<span class="hljs-string">'#h'</span>).val(<span class="hljs-built_in">Math</span>.round( c.w * realWidth / boundx ));      }    };});</code>
Copy after login

控制器代码croppic.php如下:

<code class=" hljs xml"><span class="php"><span class="hljs-comment"><span class="hljs-preprocessor"><?php</span>/** * 20150520 11:50 * 作者:Ro * 修改时间 20150522 13:50 * 修改内容 合并上传和截取图片功能 * 修改时间 20150527 15:23 * 修改内容 判断png和jpg而作对应操作 */</span><span class="hljs-comment">/******************************************************************************参数说明:$max_file_size  : 上传文件大小限制, 单位BYTE$destination_folder : 上传文件路径使用说明:1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;2. 将extension_dir =改为你的php_gd2.dll所在目录;******************************************************************************/</span><span class="hljs-comment">//上传文件类型列表</span><span class="hljs-variable">$uptypes</span>=<span class="hljs-keyword">array</span>(    <span class="hljs-string">'image/jpg'</span>,    <span class="hljs-string">'image/jpeg'</span>,    <span class="hljs-string">'image/png'</span>    );<span class="hljs-comment">//上传文件大小限制, 单位BYTE</span><span class="hljs-variable">$max_file_size</span>=<span class="hljs-number">2000000</span>;     <span class="hljs-comment">//上传文件路径'../uppict/'</span><span class="hljs-variable">$destination_folder</span>=<span class="hljs-string">"../uppict/"</span>; <span class="hljs-comment">//请求上传图片操作</span><span class="hljs-keyword">if</span> (<span class="hljs-variable">$_SERVER</span>[<span class="hljs-string">'REQUEST_METHOD'</span>] == <span class="hljs-string">'POST'</span> && <span class="hljs-string">'up'</span>==<span class="hljs-variable">$_GET</span>[<span class="hljs-string">'mothed'</span>]){    <span class="hljs-comment">//是否存在文件</span>    <span class="hljs-keyword">if</span> (!is_uploaded_file(<span class="hljs-variable">$_FILES</span>[<span class="hljs-string">"upfile"</span>][<span class="hljs-string">"tmp_name"</span>]))    {       <span class="hljs-keyword">echo</span> <span class="hljs-string">"图片不存在!"</span>;       <span class="hljs-keyword">exit</span>;   }   <span class="hljs-variable">$file</span> = <span class="hljs-variable">$_FILES</span>[<span class="hljs-string">"upfile"</span>];    <span class="hljs-comment">//检查文件大小</span>   <span class="hljs-keyword">if</span>(<span class="hljs-variable">$max_file_size</span> < <span class="hljs-variable">$file</span>[<span class="hljs-string">"size"</span>])   {    <span class="hljs-keyword">echo</span> <span class="hljs-string">"文件太大!"</span>;    <span class="hljs-keyword">exit</span>;}    <span class="hljs-comment">//检查文件类型</span><span class="hljs-keyword">if</span>(!in_array(<span class="hljs-variable">$file</span>[<span class="hljs-string">"type"</span>], <span class="hljs-variable">$uptypes</span>)){    <span class="hljs-keyword">echo</span> <span class="hljs-string">"文件类型不符!"</span>.<span class="hljs-variable">$file</span>[<span class="hljs-string">"type"</span>];    <span class="hljs-keyword">exit</span>;}<span class="hljs-keyword">if</span>(!file_exists(<span class="hljs-variable">$destination_folder</span>)){    mkdir(<span class="hljs-variable">$destination_folder</span>);}    <span class="hljs-comment">//获取信息</span><span class="hljs-variable">$filename</span>=<span class="hljs-variable">$file</span>[<span class="hljs-string">"tmp_name"</span>];<span class="hljs-variable">$image_size</span> = getimagesize(<span class="hljs-variable">$filename</span>);<span class="hljs-variable">$pinfo</span>=pathinfo(<span class="hljs-variable">$file</span>[<span class="hljs-string">"name"</span>]);<span class="hljs-variable">$ftype</span>=<span class="hljs-variable">$pinfo</span>[<span class="hljs-string">'extension'</span>];    <span class="hljs-comment">//可以在这修改上传后图片的名字,这里以time()为命名</span><span class="hljs-variable">$destination</span> = <span class="hljs-variable">$destination_folder</span>.time().<span class="hljs-string">"."</span>.<span class="hljs-variable">$ftype</span>;    <span class="hljs-comment">//检查是否已经存在同名文件</span><span class="hljs-keyword">if</span> (file_exists(<span class="hljs-variable">$destination</span>) && <span class="hljs-variable">$overwrite</span> != <span class="hljs-keyword">true</span>){    <span class="hljs-keyword">echo</span> <span class="hljs-string">"同名文件已经存在了"</span>;    <span class="hljs-keyword">exit</span>;}    <span class="hljs-comment">//上传图片操作</span><span class="hljs-keyword">if</span>(!move_uploaded_file (<span class="hljs-variable">$filename</span>, <span class="hljs-variable">$destination</span>)){    <span class="hljs-keyword">echo</span> <span class="hljs-string">"移动文件出错"</span>;    <span class="hljs-keyword">exit</span>;}    <span class="hljs-comment">//获取信息</span><span class="hljs-variable">$pinfo</span>=pathinfo(<span class="hljs-variable">$destination</span>);<span class="hljs-variable">$fname</span>=<span class="hljs-variable">$pinfo</span>[<span class="hljs-string">'basename'</span>];    <span class="hljs-comment">//重定向浏览器 </span>header(<span class="hljs-string">'Location: http://'</span>.<span class="hljs-variable">$_SERVER</span>[<span class="hljs-string">'HTTP_HOST'</span>].<span class="hljs-string">'/uppic/views/croppic.php?name='</span>.<span class="hljs-variable">$fname</span>);     <span class="hljs-comment">//确保重定向后,后续代码不会被执行 </span><span class="hljs-keyword">exit</span>;}<span class="hljs-comment">//请求截图保存操作</span><span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-variable">$_SERVER</span>[<span class="hljs-string">'REQUEST_METHOD'</span>] == <span class="hljs-string">'POST'</span> && <span class="hljs-string">'crop'</span>==<span class="hljs-variable">$_GET</span>[<span class="hljs-string">'mothed'</span>]){    <span class="hljs-comment">//获取图片名</span>    <span class="hljs-variable">$name</span>=<span class="hljs-variable">$_GET</span>[<span class="hljs-string">'name'</span>];    <span class="hljs-comment">//高宽</span>    <span class="hljs-variable">$targ_w</span> = <span class="hljs-variable">$targ_h</span> = <span class="hljs-number">150</span>;    <span class="hljs-comment">/**     *范围从 0(最差质量,文件更小)     *到 100(最佳质量,文件最大)。     *默认为 IJG 默认的质量值(大约 75)     */</span>    <span class="hljs-variable">$jpeg_quality</span> = <span class="hljs-number">90</span>;    <span class="hljs-comment">//图片暂放地址'../uppict/'</span>    <span class="hljs-variable">$src</span> = <span class="hljs-string">"../uppict/"</span>.<span class="hljs-variable">$_GET</span>[<span class="hljs-string">'name'</span>];    <span class="hljs-comment">//分开图片名和图片后缀</span>    <span class="hljs-variable">$arr_name</span> = explode ( <span class="hljs-string">"."</span>, <span class="hljs-variable">$name</span> );    <span class="hljs-comment">//判断图片后缀选择新建图片方式</span>    <span class="hljs-variable">$img_r</span> =<span class="hljs-string">''</span>;    <span class="hljs-keyword">if</span> (<span class="hljs-string">'png'</span> == <span class="hljs-variable">$arr_name</span> [<span class="hljs-number">1</span>])    {        <span class="hljs-variable">$img_r</span> = imagecreatefrompng ( <span class="hljs-variable">$src</span> );    } <span class="hljs-keyword">else</span>    {        <span class="hljs-variable">$img_r</span> = imagecreatefromjpeg ( <span class="hljs-variable">$src</span> );    }    <span class="hljs-variable">$dst_r</span> = ImageCreateTrueColor( <span class="hljs-variable">$targ_w</span>, <span class="hljs-variable">$targ_h</span> );    <span class="hljs-comment">//截取图片</span>    imagecopyresampled(<span class="hljs-variable">$dst_r</span>,<span class="hljs-variable">$img_r</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-variable">$_POST</span>[<span class="hljs-string">'x'</span>],<span class="hljs-variable">$_POST</span>[<span class="hljs-string">'y'</span>],<span class="hljs-variable">$targ_w</span>,<span class="hljs-variable">$targ_h</span>,<span class="hljs-variable">$_POST</span>[<span class="hljs-string">'w'</span>],<span class="hljs-variable">$_POST</span>[<span class="hljs-string">'h'</span>]);    <span class="hljs-comment">//判断图片后缀选择生成图片</span>    <span class="hljs-comment">//保存位置'../userpic/'// 生成图片</span>    <span class="hljs-keyword">if</span> (<span class="hljs-string">'png'</span> == <span class="hljs-variable">$arr_name</span> [<span class="hljs-number">1</span>])    {        imagepng ( <span class="hljs-variable">$dst_r</span>, <span class="hljs-string">'../userpic/'</span> . <span class="hljs-variable">$name</span> );    } <span class="hljs-keyword">else</span>    {        imagejpeg ( <span class="hljs-variable">$dst_r</span>, <span class="hljs-string">'../userpic/'</span> . <span class="hljs-variable">$name</span>, <span class="hljs-variable">$jpeg_quality</span> );    }    <span class="hljs-comment">//显示保存后的图片</span>    <span class="hljs-keyword">echo</span> <span class="hljs-string">'<img  src="../userpic/'</span alt="PHP调整Jcrop截取的上传头像功能" >.<span class="hljs-variable">$name</span>.<span class="hljs-string">'" />'</span>;    <span class="hljs-keyword">exit</span>;}<span class="hljs-preprocessor">?></span></span></code>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

EcoFlow Delta Pro 3: Powerful and particularly quiet power station with 4,000 W coming soon EcoFlow Delta Pro 3: Powerful and particularly quiet power station with 4,000 W coming soon Jun 14, 2024 pm 12:44 PM

FollowingEcoFlow'slaunchofthenew800WbatterychargerandanewweatherproofbatteryforPowerStreamthenextexcitingnewproductwillfollowshortly.ThemanufacturerhasannouncedtheEcoFlowDeltaPro3launchonJune24.Sofar,however,thepro

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

How to optimize iPad battery life with iPadOS 17.4 How to optimize iPad battery life with iPadOS 17.4 Mar 21, 2024 pm 10:31 PM

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers

请教怎么修改url某一参数的参数值呢?是要拆开了再拼回去吗 请教怎么修改url某一参数的参数值呢?是要拆开了再拼回去吗 Jun 13, 2016 am 10:24 AM

请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?那么请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?http://127.0.0.1/myo/newuser.php?mod=search&type=fastone比如现在我要修改mod=new要怎么做呢?------解决方案--------------------发送了请求

See all articles