Welcome Guest, Not a member yet? Register   Sign In
image crop problem
#1

[eluser]Galford[/eluser]
I have problem when i crop image. It leaves black space.

First I resized uploaded file into size 150x100 using master_dim (by the smaller side).
Next I want to crop whats left.

Any solution for this?

Code:
$this->load->library('image_lib');
                
                $config = null;
                $this->image_lib->clear();
                $config['image_library'] = 'gd2';
                $config['source_image'] = './gfx/'.$thumb;
                
                list($img_width, $img_height) = getimagesize('./gfx/'.$thumb);
                
                
                $config['x_axis'] =  $img_width - 150 ;
                $config['y_axis'] = $img_height - 100;

                $this->image_lib->initialize($config);
                
                if($this->image_lib->crop())
                {
                    echo br().'crop ok';
                }
                else
                {
                    echo br().$this->image_lib->display_errors();
                }
#2

[eluser]TheFuzzy0ne[/eluser]
What are the dimensions of the original image?
#3

[eluser]Galford[/eluser]
Original: 400x427
After resize: 150x161
Destination size after crop: 150x100
#4

[eluser]TheFuzzy0ne[/eluser]
My only suggestion would be to use ImageMajick if you have access to it, and see what results you get then.
#5

[eluser]Galford[/eluser]
Ok, I've chenge a little code in CI_Image_lib

function image_process_gd

I've change this:
Code:
$dst_img = $create($this->width, $this->height);
        $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);

Into this:
Code:
if ($action == 'crop')
        {
            
            // cropped file dimension
            $x_axis = $this->x_axis;
            $y_axis = $this->y_axis;
            
            if($x_axis < 0)
            {
                $x_axis *= -1;
            }
            
            if($y_axis < 0)
            {
                $y_axis *= -1;
            }

            $dst_img = $create($this->width - $x_axis, $this->height - $y_axis);
            
            
            // setting destination axis            
            if($this->x_axis < 0)
            {
                $dst_x = $this->x_axis;
            }            
            else
            {
                $dst_x = 0;
            }

            if($this->y_axis < 0)
            {
                $dst_y = $this->y_axis;
            }            
            else
            {
                $dst_y = 0;
            }
            

            $copy($dst_img, $src_img, $dst_x, $dst_y, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
            
            
        }
        else
        {
            $dst_img = $create($this->width, $this->height);
            $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
        }

There is still need to check image size before cropping it, if its no smaller then destination size.
#6

[eluser]Galford[/eluser]
ok this is my lib. This will crop with center.

My_Image_lib
Code:
&lt;?php
/**
* @autor: Piotr Werocy
* @www: http://www.werocy.com
* @e-mail: [email protected]
* @date:
*/

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Extends CI_Image_lib
* changes:
* function image_process_gd - crop with center
*/
class MY_Image_lib extends CI_Image_lib {

    function __construct()
    {
        parent::CI_Image_lib();
    }
    // --------------------------------------------------------------------
    /**
     * Image Process Using GD/GD2
     *
     * This function will resize or crop
     *
     * @access    public
     * @param    string
     * @return    bool
     */        
    function image_process_gd($action = 'resize')
    {    
        $v2_override = FALSE;

        // If the target width/height match the source, AND if the new file name is not equal to the old file name
        // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
        if ($this->dynamic_output === FALSE)
        {
            if ($this->orig_width == $this->width AND $this->orig_height == $this->height)            
            {
                 if ($this->source_image != $this->new_image)
                 {
                    if (@copy($this->full_src_path, $this->full_dst_path))
                    {
                        @chmod($this->full_dst_path, DIR_WRITE_MODE);
                    }
                }
                
                return TRUE;
            }
        }
        
        // Let's set up our values based on the action
        if ($action == 'crop')
        {
            //  Reassign the source width/height if cropping
            $this->orig_width  = $this->width;
            $this->orig_height = $this->height;    
                
            // GD 2.0 has a cropping bug so we'll test for it
            if ($this->gd_version() !== FALSE)
            {
                $gd_version = str_replace('0', '', $this->gd_version());            
                $v2_override = ($gd_version == 2) ? TRUE : FALSE;
            }
        }
        else
        {            
            // If resizing the x/y axis must be zero
            $this->x_axis = 0;
            $this->y_axis = 0;
        }
        
        //  Create the image handle
        if ( ! ($src_img = $this->image_create_gd()))
        {        
            return FALSE;
        }

         //  Create The Image
        //
        //  old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
        //  it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
        //  below should that ever prove inaccurate.
        //
        //  if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
         if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))        
        {
            $create    = 'imagecreatetruecolor';
            $copy    = 'imagecopyresampled';
        }
        else
        {
            $create    = 'imagecreate';    
            $copy    = 'imagecopyresized';
        }
            
        if ($action == 'crop')
        {
            
            // cropped file dimension
            $x_axis = $this->x_axis;
            $y_axis = $this->y_axis;
            
            if($x_axis < 0)
            {
                $x_axis *= -1;
            }
            
            if($y_axis < 0)
            {
                $y_axis *= -1;
            }

            $dst_img = $create($this->width - $x_axis, $this->height - $y_axis);
            
            
            // setting destination axis            
            if($this->x_axis < 0)
            {
                $dst_x = $this->x_axis/2;
            }            
            else
            {
                $dst_x = 0;
            }

            if($this->y_axis < 0)
            {
                $dst_y = $this->y_axis/2;
            }            
            else
            {
                $dst_y = 0;
            }
            
            $copy($dst_img, $src_img, $dst_x, $dst_y, $this->x_axis/2, $this->y_axis/2, $this->width, $this->height, $this->orig_width, $this->orig_height);
            
            
        }
        else
        {
            $dst_img = $create($this->width, $this->height);
            $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
        }
    
        

        //  Show the image    
        if ($this->dynamic_output == TRUE)
        {
            $this->image_display_gd($dst_img);
        }
        else
        {
            // Or save it
            if ( ! $this->image_save_gd($dst_img))
            {
                return FALSE;
            }
        }

        //  Kill the file handles
        imagedestroy($dst_img);
        imagedestroy($src_img);
        
        // Set the file to 777
        @chmod($this->full_dst_path, DIR_WRITE_MODE);
        
        return TRUE;
    }
    
    // --------------------------------------------------------------------
}
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB