我有一个表格,上面有两张图片:
我已经配置了 transloadit,因此一旦选择了新图像,transloadit 就会创建一个缩略图,然后将缩略图和原始图像都推送到 Amazon S3。
为此,在我的 transloadit 帐户中,我创建了以下模板:
{
"steps": {
"thumb": {
"use": ":original",
"robot": "/image/resize",
"width": 150,
"height": 150,
"resize_strategy": "pad",
"background": "#ffffff"
},
"store": {
"use": [
":original",
"thumb"
],
"robot": "/s3/store",
"key": "...",
"secret": "...",
"bucket": "www.mydomain.com",
"path": "staging/${file.id}.${file.ext}"
}
}
}
我的javascript是:
<script type="text/javascript">
var json_data = {};
$(function() {
// Hack to stop form submit from re-uploading files
// https://github.com/transloadit/jquery-sdk/issues/14
var bindTransloadit = function(form) {
form.transloadit({
wait: true,
modal: true,
triggerUploadOnFileSelection: false,
processZeroFiles: false,
autoSubmit: false,
params: {
auth: {
key: "..."
},
template_id: '...'
},
onStart: function(assembly) {
form.find('textarea[name=transloadit]').remove();
},
onUpload: function(upload) {
},
onCancel: function() { form.unbind('submit.transloadit'); },
onError: function(assembly) { form.unbind('submit.transloadit'); },
onSuccess: function(assembly) { form.unbind('submit.transloadit'); },
onResult: function(step, result)
{
json_data[step] = {
"basename": result.basename,
"field": result.field,
"name": result.name,
"url": result.url,
"ssl_url": result.ssl_url
};
var fld_data = '#fld_' + result.field + "_data";
$(fld_data).val(JSON.stringify(json_data));
if(step == "thumb")
{
$(fld_data).closest('.image-select').find('.thumbnail > img').attr('src', result.url);
}
}
});
};
$('#landing-page-form').on('change', 'input[type="file"]', function() {
var form = $(this).closest('form');
bindTransloadit(form);
form.trigger('submit.transloadit');
});
});
</script>
还有我的一些刀片模板/html,即使它可能只会混淆问题:
@foreach ($fields as $name => $properties)
@if ($properties['type'] == "image")
<div class="form-group">
<label class="col-sm-2 control-label">{{ $properties['label'] }}</label>
<div class="col-sm-3">
<div class="image-select">
<!-- Thumbnail display -->
<div class="thumbnail">
<?PHP if(array_key_exists('thumb', $data[$name . "_data"])) { ?>
<img src="<?PHP echo($data[$name . "_data"]['thumb']['url']) ?>" alt="">
<?PHP } ?>
</div>
<!-- Width/Height display -->
<label>Width</label>: {{ $properties['width'] }} / <label>Height</label>: {{ $properties['height'] }}
<input type="file" name="{{ $name }}" id="fld_{{ $name }}" />
<textarea name="{{ $name }}_data" id="fld_{{ $name }}_data" class="hidden">{{ $data[$name . '_data']['json'] }}</textarea>
@if ($properties['help_block'] != "")
<span class="help-block m-b-none">{{ $properties['help_block'] }}</span>
@endif
</div>
</div>
</div>
<div class="hr-line-dashed"></div>
@endif
@endforeach
到目前为止,一切都很好。但是,现在我想让 transloadit 将两个原始图像调整/裁剪到正确的大小。主图像需要为 300x150 像素,次图像需要为 200x200 像素。我该怎么做呢?
谢谢!