resizing image using gd2 in codeigniter

1.2k views Asked by At

I am trying to resize an uploaded image from Codeigniter panel to the server, in this process when i try to overwrite the image i get a black background with the resized image in it (image attached) , please help me with this

New Image Created

My code is as follows :

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();

Also is there any other way i can directly take the temp image and save with compression?

Thank You, Rebecca

2

There are 2 answers

1
Alex Mac On BEST ANSWER

Please go through below, It will help your issue.

You can create another folder or new name for resize image. You are facing issue because while resizing image, the source image already in use. Hence either create new folder or change filename.

Create New Folder

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

Change New Image(Resized) name

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

Let me know if it not works to you.

3
Alex Mac On

Please go through below running solution as per your requirement.

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;