To implement image resizing, cropping, and watermarking in CodeIgniter, you can utilize CodeIgniter's Image Manipulation Library. Below is a step-by-step guide on how to achieve each of these tasks:
1. Image Resizing:
$config['image_library'] = 'gd2'; // Choose your preferred image library (e.g., 'gd2', 'imagemagick', 'gd') $config['source_image'] = '/path/to/source/image.jpg'; // Path to the source image $config['create_thumb'] = TRUE; // Create a thumbnail $config['maintain_ratio'] = TRUE; // Maintain aspect ratio $config['width'] = 200; // New width $config['height'] = 200; // New height $this->load->library('image_lib'); $this->image_lib->initialize($config); if (!$this->image_lib->resize()) { echo $this->image_lib->display_errors(); }
2. Image Cropping:
$config['image_library'] = 'gd2'; $config['source_image'] = '/path/to/source/image.jpg'; $config['maintain_ratio'] = FALSE; // Disable maintaining aspect ratio $config['width'] = 200; // New width $config['height'] = 200; // New height $config['x_axis'] = 50; // X-axis coordinate for cropping $config['y_axis'] = 50; // Y-axis coordinate for cropping $this->load->library('image_lib'); $this->image_lib->initialize($config); if (!$this->image_lib->crop()) { echo $this->image_lib->display_errors(); }
3. Image Watermarking:
$config['source_image'] = '/path/to/source/image.jpg'; $config['wm_type'] = 'overlay'; // Type of watermark $config['wm_overlay_path'] = '/path/to/watermark.png'; // Path to the watermark image $config['wm_vrt_alignment'] = 'bottom'; // Vertical alignment $config['wm_hor_alignment'] = 'right'; // Horizontal alignment $config['wm_padding'] = '20'; // Padding $config['wm_opacity'] = 50; // Watermark opacity $this->load->library('image_lib'); $this->image_lib->initialize($config); if (!$this->image_lib->watermark()) { echo $this->image_lib->display_errors(); }
Comments