Codeigniter 通过自定义尺寸创建缩略图,如何实现?

问题描述 投票:0回答:2

我有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]));
}
php codeigniter
2个回答
1
投票

您可以使用

CodeIgniter
的内置
image_lib
库,它支持所有三个主要图像库:
GD/GD2, NetPBM, and ImageMagick


Here是官方文档,here是解决您问题的类似问题。希望这对您有帮助。


1
投票

您基本上需要在 ImageMagick

 上安装 
server/localhost

您还可以查看 this 链接,获取有关如何创建缩略图的工作示例。

© www.soinside.com 2019 - 2024. All rights reserved.