views:

209

answers:

2

Hi ..., I’m trying implementing crop image with coordinate. but the it doesn’t success if for the coordinate. my code is :

function image_thumb($image_path, $height, $width, $x=0, $y=0)
{
    // Get the CodeIgniter super object
    $CI =& get_instance();

    // Path to image thumbnail
    $image_thumb = dirname($image_path) . '/' . $height . '_' . $width . '.jpg';

    if( ! file_exists($image_thumb))
    {
        // LOAD LIBRARY
        $CI->load->library('image_lib');

        // CONFIGURE IMAGE LIBRARY
        $config['x_axis']            = $x;
        $config['y_axis']             = $y;
        $config['image_library']    = 'gd2';
        $config['source_image']        = $image_path;
        $config['new_image']        = $image_thumb;
        $config['maintain_ratio']    = TRUE;
        $config['height']            = $height;
        $config['width']            = $width;
        $CI->image_lib->initialize($config);
        $CI->image_lib->resize();
        $CI->image_lib->clear();
    }

    return '<img src="' . dirname($_SERVER['SCRIPT_NAME']) . '/' . $image_thumb . '" />';
}

Help me please ...

+1  A: 

Maybe it's because you are not using the right function...

$CI->image_lib->resize();

Don't you mean crop()?

$CI->image_lib->crop();

I hate saying this, but RTFM.


Here is a function that can crop and resize simultaneously:

function ImageCR($source, $crop = null, $scale = null, $destination = null)
{
    $source = @ImageCreateFromString(@file_get_contents($source));

    if (is_resource($source) === true)
    {
     $size = array(ImageSX($source), ImageSY($source));

     if (isset($crop) === true)
     {
      $crop = array_filter(explode('/', $crop), 'is_numeric');

      if (count($crop) == 2)
      {
       $crop = array($size[0] / $size[1], $crop[0] / $crop[1]);

       if ($crop[0] > $crop[1])
       {
        $size[0] = $size[1] * $crop[1];
       }

       else if ($crop[0] < $crop[1])
       {
        $size[1] = $size[0] / $crop[1];
       }

       $crop = array(ImageSX($source) - $size[0], ImageSY($source) - $size[1]);
      }

      else
      {
       $crop = array(0, 0);
      }
     }

     else
     {
      $crop = array(0, 0);
     }

     if (isset($scale) === true)
     {
      $scale = array_filter(explode('*', $scale), 'is_numeric');

      if (count($scale) >= 1)
      {
       if (empty($scale[0]) === true)
       {
        $scale[0] = $scale[1] * $size[0] / $size[1];
       }

       else if (empty($scale[1]) === true)
       {
        $scale[1] = $scale[0] * $size[1] / $size[0];
       }
      }

      else
      {
       $scale = array($size[0], $size[1]);
      }
     }

     else
     {
      $scale = array($size[0], $size[1]);
     }

     $result = ImageCreateTrueColor($scale[0], $scale[1]);

     if (is_resource($result) === true)
     {
      ImageFill($result, 0, 0, IMG_COLOR_TRANSPARENT);
      ImageSaveAlpha($result, true);
      ImageAlphaBlending($result, true);

      if (ImageCopyResampled($result, $source, 0, 0, $crop[0] / 2, $crop[1] / 2, $scale[0], $scale[1], $size[0], $size[1]) === true)
      {
       if (preg_match('~gif$~i', $destination) >= 1)
       {
        return ImageGIF($result, $destination);
       }

       else if (preg_match('~png$~i', $destination) >= 1)
       {
        return ImagePNG($result, $destination, 9);
       }

       else if (preg_match('~jpe?g$~i', $destination) >= 1)
       {
        return ImageJPEG($result, $destination, 90);
       }
      }
     }
    }

    return false;
}

Use it like this:

// resize to 400x400 px 
ImageCR('path/to/sourceImg.jpg', null, '400*400', 'path/to/outputImg.jpg');

// crop to a 1:1 ratio (square) from the center
ImageCR('path/to/sourceImg.jpg', '1/1', null, 'path/to/outputImg.jpg');

// crop to a 1:1 ratio (square) from the center AND resize to 400x400 px 
ImageCR('path/to/sourceImg.jpg', '1/1', '400*400', 'path/to/outputImg.jpg');

// crop to a 1:1 ratio (square) from the center AND resize to 400 px width AND maintain aspect ratio 
ImageCR('path/to/sourceImg.jpg', '1/1', '400*', 'path/to/outputImg.jpg');

// crop to a 1:1 ratio (square) from the center AND resize to 400 px height AND maintain aspect ratio 
ImageCR('path/to/sourceImg.jpg', '1/1', '*400', 'path/to/outputImg.jpg');

Play arround with the options, if you have any doubt just let me know.

Alix Axel
Sorry my bro ..., There is some mistaken.The first, I want to crop image using my coodinate. x, y, x1, y1.after that i want to resize the image using my width and height.because I the result is the thumb image not the cropped image, So I use resize function.can you help me, here I must put the x1, y2 configuration ?thanks
adisembiring
Of course there is some mistake, read your question again and try to understand what you ask from it. Secondly, `x, y, x1, y1, x1, y2` **What?!** You only need 4 variables to crop a image: `x1, x2, y1, y2` I'm guessing your `x1` and `y1` is `$x` and `$y`, and your `x2` and `y2` is actually `$width` and `$height`, but like I said - **I'm only guessing** since you're not being very clear. Again, try to use the `crop()` method and read the manual.
Alix Axel
I have read the manual man ..., this word must be x1, y1 configuration it doesnt need x2, and y2. because I type x, y, x1, y1.I have check the image_lib code, the library doesnt support crop and resize simultaneously.So I must make some modification image_process_gd(); function
adisembiring
You want to **crop and resize simultaneously**? You really have to be more explicit in your questions... I can't help you doing that with CodeIgniter but check the function I'll add to my answer in a couple of minutes.
Alix Axel
Sorry my man ..., My english is not good yet. Next time , I will try to ask explicitly.I have solve it, but I will try your code :D
adisembiring
A: 

Hi... I've got the answer. image_lib doesnt support crop and resize simultaneously. SO i must make some modification of the code.

/**
     * Image Process Using GD/GD2
     *
     * This function will resize or crop
     *
     * @access public
     * @param string
     * @return bool
     */
    function image_process_gd($action = 'resize', $org_w = 0, $org_h = 0)
    {
     $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 ($action == 'resize')
     {
      // If resizing the x/y axis must be zero
      $this->x_axis = 0;
      $this->y_axis = 0;
     } else {
      $this->orig_width  = $org_w;
      $this->orig_height = $org_h;
     }

     //  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';
     }

     /*echo '<br /> Coordinate' . $this->x_axis . " x " . $this->y_axis;
     echo '<br /> Destination size' .$this->width . " x " . $this->height;
     echo '<br /> Originl Size' . $this->orig_width. " x " . $this->orig_height;*/
     $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;
    }

invoke:

//set image library configuration
      $config['image_library'] = 'gd2';
      $config['source_image']  = $src_path;
      $config['new_image']  = $des_path;
      $config['maintain_ratio'] = FALSE;
      $config['width']   = 150;
      $config['height']   = 150;
      $config['orig_width']  = $width;
      $config['orig_height']  = $height;
      $config['x_axis']   = $x;
      $config['y_axis']   = $y;
      $this->image_lib->initialize($config);

      //process thumb and reset the original with and height
      $this->image_lib->image_process_gd('thumb', $width, $height);
adisembiring