仅显示缩略图大小的图像预览。您需要将您的图像转换为 base64,这样它就不会花费太多时间来加载并且它会显示完美。
/**
* Upload image action for Gravity Forms
* This script displays the thumbnail upon image upload for multi file field.
*
*/
function gravity_image_thumb() {
if ( is_page('slugname') ) {
?>
<script type="text/javascript">
gform.addFilter('gform_file_upload_markup', function (html, file, up, strings, imagesUrl) {
//alert(strings);
//Path of your temp file
var myFilePath = '/wp-content/uploads/gravity_forms/FormNameFolderURL/tmp/';
var formId = up.settings.multipart_params.form_id,
fieldId = up.settings.multipart_params.field_id;
var fileName = up.settings.multipart_params.gform_unique_id + '_input_' + fieldId +'_'+ file.target_name;
var fid = "fid"+ Math.ceil((Math.random() * (10000 - 1000)) + 1000);
//Converting Image to the base64
function convertImgToBase64URL(url, callback, outputFormat){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'), dataURL;
canvas.height = (300 * img.height)/img.width;
canvas.width = 300; //img.width;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
dataURL = canvas.toDataURL(outputFormat);
callback(dataURL);
canvas = null;
};
img.src = url;
}
convertImgToBase64URL(myFilePath + fileName , function(base64Img){
var ffid = "#"+fid;
$(ffid).attr("src", base64Img);
console.log('RESULT:', base64Img);
});
html = '<img id="'+fid+"\" src='' style='width:100%;height:auto;'/><img class='gform_delete' " + "src='" + imagesUrl + "/delete.png' "+ "onclick='gformDeleteUploadedFile(" + formId + "," + fieldId + ", this);' " + "alt='" + strings.delete_file + "' title='" + strings.delete_file + "' />";
return html;
});
</script>
<?php }
}
add_action('wp_head','gravity_image_thumb');