2009-12-24 58 views
0

我正在嘗試使用座標實現裁剪圖像。但對於座標來說它不成功。我的代碼是:使用座標在CI中調整圖像大小

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

請幫我...

回答

2

也許是因爲你不是使用正確的功能 ...

$CI->image_lib->resize(); 

你不是說crop()?我不想說這個,但是RTM


這裏是一個可以裁剪,同時調整功能:

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

使用方法如下:

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

播放角落找尋與選項,如果你有任何疑問只是讓我知道。

+0

對不起我的兄弟......,有一些錯誤的。 第一,我想用我的coodinate裁剪圖像。 x,y,x1,y1。 之後,我想使用我的寬度和高度來調整圖像大小。 因爲我的結果是拇指圖像不是裁剪圖像,所以我使用調整大小功能。 你能幫我嗎,在這裏我必須把x1,y2的配置? 謝謝 – 2009-12-24 06:36:54

+0

當然有一些錯誤,請再次閱讀您的問題,並嘗試理解您對它的要求。其次,'x,y,x1,y1,x1,y2' **什麼?!**您只需要4個變量來裁剪圖像:'x1,x2,y1,y2'我猜你的'x1'和'y1'是'$ x'和'$ y',你的'x2'和'y2'實際上是'$ width'和'$ height',但就像我說的那樣 - **我只是猜測**你不是很清楚。再次嘗試使用'crop()'方法並閱讀手冊。 – 2009-12-24 07:17:19

+0

我已經閱讀手冊man ...,這個詞必須是x1,y1配置 它不需要x2和y2。因爲我輸入x,y,x1,y1。 我檢查了image_lib代碼,庫不支持裁剪和同時調整大小。 所以我必須做一些修改image_process_gd();功能 – 2009-12-25 09:25:16

0

我已經得到了答案。 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', $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; 
    } 

調用:

//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);