0

我用表单 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;
    }

}
4

1 回答 1

0

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

实现 gulp 包并安装图像优化插件。从此网址获取参考:https ://www.tutorialspoint.com/gulp/gulp_optimizing_images.htm

于 2018-11-01T08:45:25.907 回答