我有Codeigniter自定义系统,需要上传图像标准尺寸+创建_thumb到同一文件夹中而不会重复。这是没有拇指图像创建器的工作代码...你可以帮我吗?
public function image($var, $id) {
$config['upload_path'] = './cdn/' . $this->module;
$config['allowed_types'] = 'gif|jpg|png|jpeg|webp';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('featured_image')) {
} else {
$data = $this->upload->data();
if ($data['file_name'])
$this->{$this->model}->featured_image = $data['file_name'];
}
return true;
}
public function image_upload() {
$config['upload_path'] = './cdn/' . $this->module;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$errors = 0;
$error_message = '';
$passed_files = [];
$var = 'file';
$files = $this->reArrayFiles(@$_FILES[$var]);
foreach ($files as $idx => $file) {
$_FILES[$var] = $files[$idx];
if ($this->upload->do_upload($var)) {
$data = $this->upload->data();
if ($data['file_name']) {
$ext = pathinfo($data['file_name'], PATHINFO_EXTENSION);
$file_name = "IRecipes_" . str_replace([".", " "], "", microtime()) . rand(0, 100) . "_orig." . $ext;
rename($data["full_path"], $data["file_path"] . $file_name);
$passed_files[] = $file_name;
}
} else {
$errors++;
$error_message .= strip_tags($this->upload->display_errors());
}
}
if ($passed_files && count($passed_files))
$this->uploadFile[$var] = ($passed_files);
if ($errors == count($files))
die(json_encode(["error" => true, "message" => $error_message]));
else
die(json_encode(["error" => false, "files" => $passed_files]));
}
您可以使用CodeIgniter
的内置image_lib
库,该库支持所有三个主要的图像库:GD/GD2, NetPBM, and ImageMagick
这是官方文档,这里有一个类似的问题来解决您的问题。希望这对你有帮助。
你基本上需要在server/localhost
上安装ImageMagick
。
您还可以查看此链接以获取有关如何创建缩略图的工作示例。