注意:SO已将上述参考图像转换为jpeg.这是透明的PNG.
以下是一个示例代码,它在新画布上重新创建png图像并保留透明度.如您所见,它还允许像素级操作,例如.使用自定义函数custom_func($r, $g, $b),在本问题的底部更好地说明.
基本上,这段代码成功地在新画布上重新创建/重绘上面的图像.请注意,上图中天空完全透明.
$image = imagecreatefrompng('grass.png');
$x_dimension = imagesx($image);
$y_dimension = imagesy($image);
$new_image = imagecreatetruecolor($x_dimension, $y_dimension);
// create a transparent canvas
$trans_color = imagecolorallocatealpha($new_image, 0x00, 0x00, 0x00, 127);
imagefill($new_image, 0, 0, $trans_color);
for ($x = 0; $x < $x_dimension; $x++) {
for ($y = 0; $y < $y_dimension; $y++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) …Run Code Online (Sandbox Code Playgroud) 我在前端提交表单以提交内容,其中我需要检查提交文件是否存在.
请注意,表单提交了一系列文件.它失败了我一直在尝试的所有逻辑.请注意upload[]html的第二行.
HTML
<form method="post" id="upload-form" action="" enctype="multipart/form-data" >
<input type="file" multiple="true" name="upload[]">
<input type="submit" name="upload_attachments" value="UPLOAD">
<form>
Run Code Online (Sandbox Code Playgroud)
PHP
if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {
$upload_error = 'file is not set !';
return; //stop further code execution
}else{
//do further validation
}
Run Code Online (Sandbox Code Playgroud)
上面的代码显示'file is not set !'了两种情况,即当我点击提交按钮而不上传文件,当我选择上传文件并点击提交按钮时.
这里发生了什么,我甚至做得对吗?
基本上我想在有人点击提交按钮而不选择要上传的文件时返回"文件为空"消息,并停止进一步的代码执行.