<?php
//定义API接口项目根目录
define('IMG_API_PATH', __DIR__);
//定义图片存储根目录
define('IMG_PATH', IMG_API_PATH . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
//接受一个项目图片目录
$project_name = isset($_POST['project_name']) && !empty($_POST['project_name']) ? $_POST['project_name'] : '';
if (empty($project_name)) {
exit(json_encode([
'status' => 100,
'message' => 'Please select a project directory!'
]));
}
$attachment = isset($_POST['attachment']) && !empty($_POST['attachment']) ? $_POST['attachment'] : '';
if (empty($attachment)) {
exit(json_encode([
'status' => 100,
'message' => 'Please select a attachment directory!'
]));
}
//引入composer类库
include './vendor/autoload.php';//https://packagist.org/packages/verot/class.upload.php
use Verot\Upload\Upload;
$handle = new Upload($_FILES['files']);
if ($handle->uploaded) {
$path_info = pathinfo($attachment);
if (PHP_OS == 'Linux') {
$dirname = $path_info['dirname'];
} else {
$dirname = PHP_OS == 'Linux' ? $path_info['dirname'] : str_replace('/', '\\', $path_info['dirname']);
}
$image_name = $path_info['filename'];
//生成图片路径
$server_path = IMG_PATH . $project_name . DIRECTORY_SEPARATOR . 'attachment' . DIRECTORY_SEPARATOR . $dirname . DIRECTORY_SEPARATOR;
//生成唯一文件名称
$handle->file_new_name_body = $image_name;
$handle->image_ratio = true;
//Set Image watermark
$handle->image_watermark = IMG_PATH . $project_name . DIRECTORY_SEPARATOR . 'watermark.png';
$handle->image_watermark_position = 'BR';
$handle->process($server_path);
if ($handle->processed) {
$file_dst_pathname = explode('..', $handle->file_dst_pathname);
if (isset($file_dst_pathname[1])) {
$attachment = str_replace(DIRECTORY_SEPARATOR . $project_name, '', $file_dst_pathname[1]);
} else {
$attachment = $file_dst_pathname;
}
$data = [
'status' => 0,
'message' => 'success',
'ext' => $handle->file_dst_name_ext,
'maxsize' => $handle->file_max_size,
'image' => $handle->file_is_image,
'size' => $handle->file_src_size,
'type' => $handle->file_src_mime,
'attachment' => $attachment
];
$handle->clean();
} else {
$data = [
'status' => 100,
'message' => $handle->error
];
}
exit(json_encode($data));
}