简体   繁体   English

JavaScript更改后上传图片

[英]Upload image after JavaScript changes

I have a Drupal-7 website, and I have created a module, where you upload an image and preview the image before submitting it. 我有一个Drupal-7网站,并且已经创建了一个模块,您可以在其中上传图像并在提交图像之前对其进行预览。

<img id="blah" src="sites/all/themes/my_theme/logo.png" alt="default image" />

Then, I have some buttons, that call a JavaScript function onclick and dynamically change the border of the uploaded image. 然后,我有一些按钮,它们调用JavaScript函数onclick并动态更改上传图像的边框。

Now, what I want, is that when the user uploads the image, then he submits the form and I upload his image with the border he chose. 现在,我想要的是,当用户上传图像时,他提交了表单,而我上传了具有所选边框的图像。
How can I achieve that? 我该如何实现?
The submit button and the image upload are called via a php form at my .module file 通过我的.module文件中的php表单调用提交按钮和图像上传

It is very common algorithm, with many examples in internet, suppoerted browsers are 10 ie and all modern browsers. 这是非常常见的算法,在Internet中有很多示例,支持的浏览器是10 ie,并且是所有现代浏览器。

 1. Convert user selected file to image (img.src = URL.createObjectURL(userSelectedFile))
 2. Load image to html5 canvas (canvas.getContext("2d").drawImage(image,0,0,width, height)) 
 3. Draw Border on html5 canvas (canvas.getContext("2d").drawRectangle(0,0, width, height))
 4. Convert canvas to blob (canvas.toBlob(function(blob){/** use new blob **/}))
 5. Upload this blob to server by xhr or xhr + FormData

You have to process image (add border) on server side. 您必须在服务器端处理图像(添加边框)。 When user submit form, he send to server side info about which border he have chosen. 用户提交表单时,他会将选择的边界发送到服务器端信息。 And using this info you have to change uploaded image by ImageMajick or etc. 并使用此信息,您必须通过ImageMajick等更改上传的图像。

Since you have a JavaScript function that onclick will dynamically change the border of the uploaded image 由于您具有JavaScript函数,因此onclick会动态更改上传图像的边框

function borderColor() {
var color;
// change border color
$("#borderColorPickID").val(color); // pass color to form
} 

Pass the hexadecimal color value or color name "green,blue" etc. In a hidden input. 在隐藏的输入中传递十六进制颜色值或颜色名称“绿色,蓝色”等。

<img id="blah" src="sites/all/themes/my_theme/logo.png" alt="default image" />
<input type="hidden" value="" name="borderColorPick" id="borderColorPickID" />

Bind the image name and the color value, server side, to rename the image accordingly. 在服务器端绑定图像名称和颜色值,以相应地重命名图像。

So if the uploaded image is upload.png and the color value is #00fc00 rename and save the image as upload___00fc00.png 因此,如果上传的图片是upload.png且颜色值为#00fc00,请重命名并将图片另存为upload ___ 00fc00.png

Now all you need is a javascript function to get the border color from the new name of the image, create the border and apply it to the image. 现在,您只需要一个JavaScript函数即可从图像的新名称获取边框颜色,创建边框并将其应用于图像。

function applyBorder(imageName) {
//// get color from image name
//// apply border to image 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM