برش تصاویر قبل از آپلود (Crop)
نویسنده: علی یگانه مقدم
تاریخ: ۱۳۹۵/۱۲/۰۵ ۱۳:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<div>
<!-- upload form -->
<!-- hidden crop params -->
<input type="hidden" id="x1" name="x1" />
<input type="hidden" id="y1" name="y1" />
<input type="hidden" id="x2" name="x2" />
<input type="hidden" id="y2" name="y2" />
<h2>ابتدا تصویر خود را انتخاب کنید</h2>
<div><input type="file" name="postedFileBase" data-buttonText="انتخاب تصویر" id="image_file" onchange="fileSelectHandler()" /></div>
<div></div>
<div>
<h2>قسمتی از تصویر را انتخاب نمایید</h2>
<img id="preview" />
<div>
<label>حجم فایل </label> <input type="text" id="filesize" name="filesize" />
<label>نوع فایل</label> <input type="text" id="filetype" name="filetype" />
<label>ابعاد فایل</label> <input style="direction: ltr;" type="text" id="filedim" name="filedim" />
</div>
</div>
</div> .bheader {
background-color: #DDDDDD;
border-radius: 10px 10px 0 0;
padding: 10px 0;
text-align: center;
}
.bbody {
color: #000;
overflow: hidden;
padding-bottom: 20px;
text-align: center;
background: -moz-linear-gradient(#ffffff, #f2f2f2);
background: -ms-linear-gradient(#ffffff, #f2f2f2);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ffffff), color-stop(100%, #f2f2f2));
background: -webkit-linear-gradient(#ffffff, #f2f2f2);
background: -o-linear-gradient(#ffffff, #f2f2f2);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#f2f2f2');
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#f2f2f2')";
background: linear-gradient(#ffffff, #f2f2f2);
}
.bbody h2, .info, .error {
margin: 10px 0;
}
.step2, .error {
display: none;
}
.error {
color: red;
}
.info {
}
label {
margin: 0 5px;
}
.roundinput {
border: 1px solid #CCCCCC;
border-radius: 10px;
padding: 4px 8px;
text-align: center;
width: 150px;
}
.jcrop-holder {
display: inline-block;
}
input[type=submit] {
background: #e3e3e3;
border: 1px solid #bbb;
border-radius: 3px;
-webkit-box-shadow: inset 0 0 1px 1px #f6f6f6;
box-shadow: inset 0 0 1px 1px #f6f6f6;
color: #333;
padding: 8px 0 9px;
text-align: center;
text-shadow: 0 1px 0 #fff;
width: 150px;
}
input[type=submit]:hover {
background: #d9d9d9;
-webkit-box-shadow: inset 0 0 1px 1px #eaeaea;
box-shadow: inset 0 0 1px 1px #eaeaea;
color: #222;
cursor: pointer;
}
input[type=submit]:active {
background: #d0d0d0;
-webkit-box-shadow: inset 0 0 1px 1px #e3e3e3;
box-shadow: inset 0 0 1px 1px #e3e3e3;
color: #000;
} function bytesToSize(bytes) {
var sizes = ['بایت', 'کیلو بایت', 'مگابایت'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};
function updateInfo(e) {
$('#x1').val(e.x);
$('#y1').val(e.y);
$('#x2').val(e.x2);
$('#y2').val(e.y2);
};
var jcrop_api, boundx, boundy;
function fileSelectHandler() {
var oFile = $('#image_file')[0].files[0];
$('.error').hide();
var rFilter = /^(image\/jpeg|image\/png)$/i;
if (!rFilter.test(oFile.type)) {
$('.error').html('فقط تصویر معتبر انتخاب نمایید').show();
return;
}
var oImage = document.getElementById('preview');
var oReader = new FileReader();
oReader.onload = function (e) {
oImage.src = e.target.result;
oImage.onload = function () {
$('.step2').fadeIn(500);
var sResultFileSize = bytesToSize(oFile.size);
$('#filesize').val(sResultFileSize);
$('#filetype').val(oFile.type);
$('#filedim').val(oImage.naturalWidth + ' x ' + oImage.naturalHeight);
if (typeof jcrop_api != 'undefined') {
jcrop_api.destroy();
jcrop_api = null;
$('#preview').width(oImage.naturalWidth);
$('#preview').height(oImage.naturalHeight);
}
$('#preview').Jcrop({
aspectRatio: 2,
bgFade: true,
bgOpacity: .3,
onChange: updateInfo,
onSelect: updateInfo
}, function () {
//var bounds = this.getBounds();
//var boundx = bounds[0];
//var boundy = bounds[1];
// Store the Jcrop API in the jcrop_api variable
jcrop_api = this;
});
};
};
oReader.readAsDataURL(oFile);
} function fileSelectHandler() {
var oFile = $('#image_file')[0].files[0];
$('.error').hide();
var rFilter = /^(image\/jpeg|image\/png)$/i;
if (!rFilter.test(oFile.type)) {
$('.error').html('فقط تصویر معتبر انتخاب نمایید').show();
return;
} var oImage = document.getElementById('preview');
var oReader = new FileReader();
oReader.onload = function (e) {
oImage.src = e.target.result;
oImage.onload = function () {
$('.step2').fadeIn(500);
var sResultFileSize = bytesToSize(oFile.size);
$('#filesize').val(sResultFileSize);
$('#filetype').val(oFile.type);
$('#filedim').val(oImage.naturalWidth + ' x ' + oImage.naturalHeight);
if (typeof jcrop_api != 'undefined') {
jcrop_api.destroy();
jcrop_api = null;
$('#preview').width(oImage.naturalWidth);
$('#preview').height(oImage.naturalHeight);
}
$('#preview').Jcrop({
aspectRatio: 2,
bgFade: true,
bgOpacity: .3,
onChange: updateInfo,
onSelect: updateInfo,
onRelease: clearInfo
}, function () {
//var bounds = this.getBounds();
//var boundx = bounds[0];
//var boundy = bounds[1];
jcrop_api = this;
});
};
};
oReader.readAsDataURL(oFile); minSize:[40,20]
aspectRatio:1.5
function updateInfo(e) {
$('#x1').val(e.x);
$('#y1').val(e.y);
$('#x2').val(e.x2);
$('#y2').val(e.y2);
}; function bytesToSize(bytes) {
var sizes = ['بایت', 'کیلو بایت', 'مگابایت'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
}; public static byte[] Resize(this byte[] byteImageIn, int x1,int y1,int x2,int y2)
{
ImageConverter ic = new ImageConverter();
Image src = (Image)(ic.ConvertFrom(byteImageIn));
Bitmap target = new Bitmap(x2 - x1, y2 - y1);
using (Graphics graphics = Graphics.FromImage(target))
graphics.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
new Rectangle(x1,y1,x2-x1,y2-y1),
GraphicsUnit.Pixel);
src = target;
using (var ms = new MemoryStream())
{
src.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
} <div style="overflow: auto">
<img id="preview" />
</div> <input type="hidden" id="RealW" name="RealW" /> <input type="hidden" id="RealH" name="RealH" />
$('#preview').Jcrop({
aspectRatio: 2,
bgFade: true,
bgOpacity: .3,
onChange: updateInfo,
onSelect: updateInfo
}, function () {
// use the Jcrop API to get the real image size
//============== خطوط جدید
$("#RealW").val($('#preview').css("width").replace("px", ""));
$("#RealH").val($('#preview').css("height").replace("px", ""));
//==============
jcrop_api = this;
}); public static byte[] Resize(this byte[] byteImageIn, int x1,int y1,int x2,int y2,int realW,int realH)
{
//convert to full size image
ImageConverter ic = new ImageConverter();
Image src = (Image)(ic.ConvertFrom(byteImageIn)); //original size
if (src == null)
return null;
// چهار خط زیر فرمول تناسب را اجرا میکند
x1 = src.Width * x1 / realW;
x2 = src.Width * x2 / realW;
y1 = src.Height * y1 / realH;
y2 = src.Height * y2 / realH;
Bitmap target = new Bitmap(x2 - x1, y2 - y1);
using (Graphics graphics = Graphics.FromImage(target))
graphics.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
new Rectangle(x1,y1,x2-x1,y2-y1),
GraphicsUnit.Pixel);
src = target;
using (var ms = new MemoryStream())
{
src.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
} <div class="col-md-6">
<img class="img-fluid" id="preview">
</div>