How to grayscale an image in codeigniter php?

by darrion.kuhn , in category: PHP Frameworks , 2 months ago

How to grayscale an image in codeigniter php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , 2 months ago

@darrion.kuhn 

To grayscale an image in CodeIgniter PHP, you can use the following steps:

  1. Load the image library in your controller:


$this->load->library('image_lib');

  1. Set the configuration options for the grayscale effect:


$config['image_library'] = 'gd2'; $config['source_image'] = 'path_to_your_image.jpg'; $config['new_image'] = 'path_to_save_grayscale_image.jpg'; $config['create_thumb'] = FALSE; $config['maintain_ratio'] = TRUE; $config['width'] = 300; // Set the width of the grayscale image $config['height'] = 200; // Set the height of the grayscale image $config['master_dim'] = 'auto'; $config['quality'] = 90;

  1. Apply the grayscale effect by setting the image manipulation type to 'greyscale':


$config['new_image'] = 'path_to_save_grayscale_image.jpg'; $config['create_thumb'] = FALSE; $config['maintain_ratio'] = TRUE; $config['width'] = 300; // Set the width of the grayscale image $config['height'] = 200; // Set the height of the grayscale image $config['master_dim'] = 'auto'; $config['quality'] = 90; $config['image_library'] = 'gd2'; $config['source_image'] = 'path_to_your_image.jpg'; $config['create_thumb'] = FALSE; $config['maintain_ratio'] = TRUE; $config['width'] = 300; // Set the width of the grayscale image $config['height'] = 200; // Set the height of the grayscale image $config['master_dim'] = 'auto'; $config['quality'] = 90; $config['new_image'] = 'path_to_save_grayscale_image.jpg'; $config['maintain_ratio'] = TRUE;


$this->image_lib->initialize($config); $this->image_lib->greyscale();

  1. Check if the grayscale effect was applied successfully:


if (!$this->image_lib->greyscale()) { echo $this->image_lib->display_errors(); } else { echo 'Image has been gray scaled successfully'; }


This code will load the image library in CodeIgniter, set the configuration options for the grayscale effect, apply the grayscale effect to the image, and then display any errors if the grayscale effect fails.