Welcome Guest, Not a member yet? Register   Sign In
image manipulation lib: How to reset $config[] properly for sequential manipulations?
#1

[eluser]moonbeetle[/eluser]
Been reading several posts on the forum about using the image manipulation library but nothing worked for me nor solved my problem. I want to run a several manipulations.

1. resizing an image
2. generating a thumb (without a watermark)
3. applying a watermark to the original
4. outputting the image, thumb and meta data

Unfortunately, I can't get the results I wanted. The original get the watermark applied to it twice and all generated images look like a "picture in a picture", not quite the result I was hoping for ;-)

Code:
class Test extends Controller {

    public $imgPath;
    public $imgWidth;
    public $imgHeight;
    public $maxWidth;
    public $maxHeight;
    public $tmbWidth;
    public $tmbHeight;
    public $imgEXIFPath;

    function Test(){
        parent::Controller();
        $this->load->library('image_lib');
        $this->load->helper('url');

        $this->imgPath      = './img/';
        $this->imgWidth     = 0;
        $this->imgHeight    = 0;
        $this->maxWidth     = 600;
        $this->maxHeight    = 500;
        $this->tmbWidth     = 120;
        $this->tmbHeight    = 90;
        $this->imgEXIFPath  = base_url().'img/';
    }
    
    function index(){
        $this->imgPath .= 'test.jpg';
        $this->imgEXIFPath .= 'test.jpg';

        // Get EXIF data
        $exif = exif_read_data($this->imgEXIFPath);
        
        // Set values
        $this->imgWidth = $exif['COMPUTED']['Width'];
        $this->imgHeight = $exif['COMPUTED']['Height'];
        
        // RESIZE ORIGINAL (if needed)
        if($this->imgWidth > $this->maxWidth || $this->imgHeight > $this->maxHeight){
            $this->resize();
            $data['Original was resized'] = 'Yes';
        } else {
            $data['Original was resized'] = 'No';
        }
        // CREATE THUMB
        unset($this->image_lib);
        $this->image_lib = new CI_Image_lib();
        $this->create_thumb();
        
        // WATERMARK BIG PICTURE VERSION
        unset($this->image_lib);
        $this->image_lib = new CI_Image_lib();
        $this->watermark();
        
        // Set image meta data
        unset($exif);
        $exif = exif_read_data($this->imgEXIFPath);
        $data['Filepath'] = $this->imgEXIFPath;
        $data['Filename'] = $exif['FileName'];
        $data['Date'] = date("F d, Y h:i", getLastMod());
        $data['Filesize'] = (ceil($exif['FileSize']/1000)).' Kbytes';
        $data['Mimetype'] = $exif['MimeType'];
        $data['Dimensions'] = $exif['COMPUTED']['Width'].' x '.$exif['COMPUTED']['Height'].' pixels';
        
        // Output image, thumb and image meta data
        echo '<p><img src="'.base_url().'img/test.jpg"></p>';
        echo '<p><img src="'.base_url().'img/test_thumb.jpg"></p>';
        echo '<div style="padding:5px; border:1px solid #999; background:#e7e7e7;">';
        foreach($data as $key => $val){
            echo "<b>$key : </b>$val<br />";
        }
        echo '</div>';
    }
    
    private function resize(){

        // Config
        $config_img['image_library']    = "GD2";
        $config_img['source_image']     = $this->imgPath;
        $config_img['create_thumb']     = FALSE;
        $config_img['maintain_ratio']   = TRUE;
        $config_img['width']            = $this->maxWidth;
        $config_img['height']           = $this->maxHeight;
      
        $this->image_lib->initialize($config_img);
        $this->image_lib->resize();

        // show error
        if (!$this->image_lib->resize()){
            echo "--- Error resizing ---<br />";
            echo $this->image_lib->display_errors();
        }
        return;
    }

    private function create_thumb(){
    
        // Config
        $config_tmb['image_library']    = "GD2";
        $config_tmb['source_image']     = $this->imgPath;
        $config_tmb['create_thumb']     = TRUE;
        $config_tmb['maintain_ratio']   = TRUE;
        $config_tmb['width']            = $this->tmbWidth;
        $config_tmb['height']           = $this->tmbHeight;
        
        $this->image_lib->initialize($config_tmb);
        $this->image_lib->resize();

        // show error
        if (!$this->image_lib->resize()){
            echo "--- Error thumbnail creation ---<br />";
            echo $this->image_lib->display_errors();
        }
        return;
    }
    
    private function watermark(){

        // Config
        $config_wm['image_library']     = "GD2";
        $config_wm['source_image']      = $this->imgPath;
        $config_wm['create_thumb']      = FALSE;
        $config_wm['maintain_ratio']    = TRUE;
        $config_wm['wm_text']           = 'Copyright '.date("Y");
        $config_wm['wm_type']           = 'text';
        $config_wm['wm_font_path']      = './system/fonts/verdana.ttf';
        $config_wm['wm_font_size']      = '16';
        $config_wm['wm_font_color']     = 'ffffff';
        $config_wm['wm_vrt_alignment']  = 'bottom';
        $config_wm['wm_hor_alignment']  = 'right';
        $config_wm['wm_padding']        = '0';
        
        $this->image_lib->initialize($config_wm);
        $this->image_lib->watermark();

        // show error
        if (!$this->image_lib->watermark()){
            echo "--- Error watermarking ---<br />";
            echo $this->image_lib->display_errors();
        }
        return;
    }
}
?&gt;
#2

[eluser]Rick Jolly[/eluser]
Did you try calling clear() before initialize()?
Code:
$this->image_lib->clear();
$this->image_lib->initialize($config);




Theme © iAndrew 2016 - Forum software by © MyBB