在 codeigniter 中使用 gd2 调整图像大小

resizing image using gd2 in codeigniter

我正在尝试调整从 Codeigniter 面板上传到服务器的图像的大小,在此过程中,当我尝试覆盖图像时,我得到一个黑色背景,其中包含调整后的图像(附图像),请帮助我这个

我的代码如下:

move_uploaded_file($_FILES['img']['tmp_name'],"img".".".$extension);
$config['image_library'] = 'gd2';
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = False;
$config['overwrite'] = TRUE;
$config['width'] = 1000;
$config['height'] = 1000;
$config['quality'] = 100;
$config['source_image'] = "img".".".$extension;
$config['new_image'] = "img".".".$extension;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();

还有没有其他方法可以直接拍摄临时图像并压缩保存?

谢谢, 丽贝卡

请看下面,对您的问题有帮助。

您可以为调整图像大小创建另一个文件夹或新名称。您遇到问题是因为在调整图像大小时,源图像已在使用中。因此,创建新文件夹或更改文件名。

Create New Folder

$config['new_image'] = "path/to/new/folder/img".".".$extension;

Change New Image(Resized) name

$config['new_image'] = "newimg".".".$extension;

如果它对你不起作用,请告诉我。

请根据您的要求完成以下 运行 解决方案。

if ($this->input->server('REQUEST_METHOD') === 'POST'):
    $this->load->library('image_lib');
    $path_parts = pathinfo($_FILES["file"]["name"]);
    $extension = $path_parts['extension'];
    move_uploaded_file($_FILES['file']['tmp_name'], "./uploads/img" . "." . $extension);
    $config['image_library'] = 'gd2';
    $config['maintain_ratio'] = TRUE;
    $config['create_thumb'] = False;
    $config['overwrite'] = TRUE;
    $config['width'] = 200;
    $config['height'] = 200;
    $config['quality'] = 100;
    $config['source_image'] = "./uploads/img" . "." . $extension;
    $this->image_lib->clear();
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
endif;