โดย eange08 » 14/01/2021 4:53 pm
ทำได้แล้วนะคะ โดยทำ
1. ในไฟล์ script.js
โค้ด: เลือกทั้งหมด
$(document).on('click','.btn_downcrop',function () {
var input=$(this).closest('.row').find('.cropbe4 input');
var data = {};
$.each(input, function() {
var name=String($(this).attr('name')).replace('[]','');
data[name]=$(this).val();
});
$.post('exportimg_crop.php', data, function (result) { //-------ส่วนเข้าไปแปลงภาพด้วย imagick แล้วส่งค่าที่จะไปที่ไฟล์ download ใน result
console.log(data);
window.open('exportimg.php?id='+result.file_id+'&type='+result.file_type, '_blank'); //-----เมื่อทำการแปลงเสร็จก็ส่งค่ารูปไปที่ไฟล์ exportimg.php
});
});
2. ไฟล์ exportimg_crop.php สำหรับแปลงรูปด้วย imagick
โค้ด: เลือกทั้งหมด
<?php
date_default_timezone_set("Asia/Bangkok");
require_once'conn.php';
//------ส่วนที่เรียกค่าจาก data ที่ส่งค่า post มา
if(!empty($_POST['id_img'])){
$id_img=$_POST['id_img'];
}else{
$id_img='';
}
if(!empty($_POST['x'])){
$img_x=$_POST['x'];
}else{
$img_x='';
}
.............
//-----ส่งค่ากลับไปที่ script.js แล้วเพื่อเอาไปใช้ทำ download ไฟล์ที่ exportimg.php
header('Content-type: application/json');
echo '{"status":"success", "file_id": "'.$straft_filenm.'","file_type":"'.$straft_mime.'"}';
exit;
?>
3. ไฟล์ exportimg.php สำหรับดึงภาพเพื่อ download
โค้ด: เลือกทั้งหมด
<?php
//----รับค่าจาก url ที่ส่งมาจาก script.js
if(!empty($_REQUEST['id'])){
$str_id=$_REQUEST['id'];
}else{
$str_id='';
}
if(!empty($_REQUEST['type'])){
$str_type=$_REQUEST['type'];
}else{
$str_type='';
}
//-----ส่วนดึงภาพแล้ว download ไฟล์ออกไป
$path = dirname(__FILE__) . '/uploads/after/'.$str_id;
if (file_exists($path)) {
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Type: ".$str_type);
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($path));
echo file_get_contents($path);
}else{
echo "<script>alert('ไม่พบไฟล์ที่ต้องการ download ค่ะ');</script>";
}
exit;
?>
ทำได้แล้วนะคะ โดยทำ
1. ในไฟล์ script.js
[code] $(document).on('click','.btn_downcrop',function () {
var input=$(this).closest('.row').find('.cropbe4 input');
var data = {};
$.each(input, function() {
var name=String($(this).attr('name')).replace('[]','');
data[name]=$(this).val();
});
$.post('exportimg_crop.php', data, function (result) { //-------ส่วนเข้าไปแปลงภาพด้วย imagick แล้วส่งค่าที่จะไปที่ไฟล์ download ใน result
console.log(data);
window.open('exportimg.php?id='+result.file_id+'&type='+result.file_type, '_blank'); //-----เมื่อทำการแปลงเสร็จก็ส่งค่ารูปไปที่ไฟล์ exportimg.php
});
});[/code]
2. ไฟล์ exportimg_crop.php สำหรับแปลงรูปด้วย imagick
[code]<?php
date_default_timezone_set("Asia/Bangkok");
require_once'conn.php';
//------ส่วนที่เรียกค่าจาก data ที่ส่งค่า post มา
if(!empty($_POST['id_img'])){
$id_img=$_POST['id_img'];
}else{
$id_img='';
}
if(!empty($_POST['x'])){
$img_x=$_POST['x'];
}else{
$img_x='';
}
.............
//-----ส่งค่ากลับไปที่ script.js แล้วเพื่อเอาไปใช้ทำ download ไฟล์ที่ exportimg.php
header('Content-type: application/json');
echo '{"status":"success", "file_id": "'.$straft_filenm.'","file_type":"'.$straft_mime.'"}';
exit;
?>
[/code]
3. ไฟล์ exportimg.php สำหรับดึงภาพเพื่อ download
[code]
<?php
//----รับค่าจาก url ที่ส่งมาจาก script.js
if(!empty($_REQUEST['id'])){
$str_id=$_REQUEST['id'];
}else{
$str_id='';
}
if(!empty($_REQUEST['type'])){
$str_type=$_REQUEST['type'];
}else{
$str_type='';
}
//-----ส่วนดึงภาพแล้ว download ไฟล์ออกไป
$path = dirname(__FILE__) . '/uploads/after/'.$str_id;
if (file_exists($path)) {
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Type: ".$str_type);
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($path));
echo file_get_contents($path);
}else{
echo "<script>alert('ไม่พบไฟล์ที่ต้องการ download ค่ะ');</script>";
}
exit;
?>
[/code]