具有自动压缩功能的Drupal Form API文件上传

时间:2018-10-30 05:27:29

标签: drupal-7 form-api

我编写了一个带有表单api的模块,该模块可以上传具有动态文件名的图像。我想使用自动压缩功能上传该文件。我不明白我将如何实现这一目标。我该如何在提交回调中实现这一点。 在我的代码下面:

function MYMODULE_registration_form() {

  $form['name'] = array(
    '#title'=> t('Name'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#size' => 40,
    '#attributes' => array('placeholder' => t('Enter your name'),),
  );

$form['personal_info']['upload_pic'] = array(
    '#title' => t('Upload Photo'),
    '#type' => 'file',
    '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif, bmp'),
  );
 $form['submit'] = array(
      '#value' => t('Submit'),
      '#type' => 'submit',
    );
  $form['#attributes']['enctype'] = 'multipart/form-data';

return $form;

}

function MYMODULE_registration_form_submit($form, $form_state) {
$name = $form_state['values']['name'];
    $count = 1;
    $original_username = $name;
    while (user_load_by_name($name)) {
        $name = $original_username . $count++;
    }

$validators = array(
      'file_validate_extensions' => array('png gif jpg jpeg bmp')
    );

    $uploadCheck = file_save_upload('upload_pic', $validators);

    if($uploadCheck) {
        $parts = pathinfo($uploadCheck->filename);
        $imagename = str_replace(' ', '_',$original_username.'_'. $count . '.'. $parts['extension']);
        $target = "public://attorneys/".$imagename;
        file_move($uploadCheck, $target, FILE_EXISTS_REPLACE);
        $picture_fid = $uploadCheck->fid;
    }

}

1 个答案:

答案 0 :(得分:0)

经过大量的研发,我得出了这个结论,它运行良好:

实施gulp软件包并安装图像优化插件。 从以下网址获取ref:https://www.tutorialspoint.com/gulp/gulp_optimizing_images.htm