我決定爲你走過這一個。以下是關於如何使用ajax構建請求的分步指南。
什麼下面的代碼將創建可以在http://tomsfreelance.com/stackoverflow/imageDownloadLoading/main.php
文件看出,包括被包含在目錄(http://tomsfreelance.com/stackoverflow/imageDownloadLoading/)
步驟1中的脈動熱管:設置一個形式。我們現在將保持簡單。
<form name="resizeImage">
<select id="image" onChange="com.demo.updateImage();">
<option value="img1.jpg">img1.jpg</option>
<option value="img2.jpg">img2.jpg</option>
<option value="img3.jpg">img3.jpg</option>
</select>
<input type="text" id="tint" placeholder="Tint" onKeyUp="com.demo.updateTint();">
<input type="button" id="download" value="Download" onClick="com.demo.downloadImage();" />
<br />
<img style="max-width: 400px; max-height: 400px;" src="img1.jpg" id="preview">
<div id="color"></div>
</form>
第2步:添加一些JavaScript/AJAX
<script>
var com = {};
com.demo = {};
com.demo.loading = false;
com.demo.loadingBox = null;
com.demo.loadingStage = 1;
com.demo.updateImage = function() {
$('#preview').prop('src', $('#image').val());
com.demo.updateTint();
}
com.demo.updateTint = function() {
$('#color').css({
'width': $('#preview').outerWidth() + 'px',
'height': $('#preview').outerHeight() + 'px',
'background-color': $('#tint').val(),
'z-index' : 10,
'position': 'absolute',
'left': $('#preview').position().left,
'top' : $('#preview').position().top,
'opacity' : 0.4
});
}
com.demo.doLoading = function() {
if (com.demo.loading) {
if (com.demo.loadingBox == null) {
com.demo.loadingBox = $('<div id="loading">Loading</div>').appendTo('body').css({ "position" : "absolute", "width" : "100px", "left" : "50px", "top" : "50px", "border" : "5px solid #000000", "padding" : "10px", "z-index" : "20", "background-color" : "#FFFFFF" });
}
else com.demo.loadingBox.css({ 'display' : 'block' });
com.demo.loadingStage = ++com.demo.loadingStage % 3;
var string = "Loading";
for (var x = 0; x < com.demo.loadingStage; x++) {
string += ".";
}
com.demo.loadingBox.text(string);
setTimeout(function() { com.demo.doLoading(); }, 1000);
}
else {
com.demo.loadingBox.css({ 'display' : 'none' });
}
}
com.demo.downloadImage = function() {
var data = {};
data.img = $('#image').val();
data.tint = $('#tint').val();
// Show loading box.
com.demo.loading = true;
com.demo.doLoading();
$.post("convert.php", data)
.done(function(d) {
com.demo.loading = false;
document.location.href = d;
});
}
</script>
第3步:處理該文件中的PHP頁面。
<?php
function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
//return implode(",", $rgb); // returns the rgb values separated by commas
return $rgb; // returns an array with the rgb values
}
if (isset($_POST['img'])) {
$im = imagecreatefromjpeg($_POST['img']);
$color = (empty($_POST['tint'])) ? hex2rgb("#000000") : hex2rgb($_POST['tint']);
if (imagefilter($im, IMG_FILTER_COLORIZE, $color[0], $color[1], $color[2])) {
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=download.jpg");
imagejpeg($im, "download.jpg");
echo "download.php";
}
}
第4步:進入下載頁面。
<?php
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=download.jpg");
readfile("download.jpg");
哪裏是ajax? – codepixlabs
沒有ajax,調整腳本完全是php。目前,我已將這部分代碼註釋掉了。請注意,我對ajax或js一無所知。 – gurung
您的表單如何提交? –