顯示廣告
隱藏 ✕
看板 Mesak
作者 mesak (Mesak)
標題 [PHP] Codeigniter Soup_Image_lib
時間 2013年09月28日 Sat. AM 01:51:42


Codeigniter Image_lib maintain ratio crop
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Soup_Image_lib extends CI_Image_lib{
	
public function __construct($props = array())
	
{
	
	
if (count($props) > 0)
	
	
{
	
	
	
$this->initialize($props);
	
	
}
	
	
log_message('debug', "Soup Image Lib Class Initialized");
	
}
	
public function get_info()
	
{
	
	
$result = array(
	
	
	
'width'=>$this->width,
	
	
	
'height'=>$this->height,
	
	
	
'path'=> str_replace( str_replace('\\','/',FCPATH) , '', dirname($this->full_dst_path) ),
	
	
	
'fullpath'=> $this->full_dst_path,
	
	
	
'filename'=> $this->dest_image
	
	
);
	
	
$result['filepath'] = $result['path'] .'/' .$result['filename'];
	
	
$result['url'] = base_url( $result['filepath'] );
	
	
return $result;
	
}
	
public function resize()
	
{
	
	
$protocol = 'image_process_'.$this->image_library;
	
	
if (preg_match('/gd2$/i', $protocol))
	
	
{
	
	
	
$protocol = ($this->maintain_ratio ) ? 'image_process_gd' : 'soup_image_process_gd';
	
	
}
	
	
return $this->$protocol('resize');
	
}
	
function soup_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, FILE_WRITE_MODE);
	
	
	
	
	
}
	
	
	
	
}
	
	
	
	
return TRUE;
	
	
	
}
	
	
}
	
	
$crop_w = $this->width;
	
	
$crop_h = $this->height;
	
	
$type_origin = $this->orig_height / $this->orig_width;
	
	
if( $type_origin == 1 ){
	
	
	
$type_origin = 'square';
	
	
}else if( $type_origin < 1 ){
	
	
	
$type_origin = 'w_rectangle';
	
	
}else{
	
	
	
$type_origin = 'h_rectangle';
	
	
}
	
	
$type_crop = $crop_h / $crop_w;
	
	
if( $type_crop == 1 ){
	
	
	
$type_crop = 'square';
	
	
}else if( $type_crop < 1 ){
	
	
	
$type_crop = 'w_rectangle';
	
	
}else{
	
	
	
$type_crop = 'h_rectangle';
	
	
}
	
	
$type_porp_w = round($crop_w / $this->orig_width,2);
	
	
$type_porp_h = round($crop_h / $this->orig_height,2);

	
	
$type_pw_w = ceil($this->orig_width * $type_porp_w);
	
	
$type_pw_h = ceil($this->orig_height * $type_porp_w);

	
	
$type_ph_w = ceil($this->orig_width * $type_porp_h);
	
	
$type_ph_h = ceil($this->orig_height * $type_porp_h);

	
	
if( $crop_h < $type_pw_h )
	
	
{
	
	
	
$this->width = $type_pw_w;
	
	
	
$this->height= $type_pw_h;
	
	
}else{
	
	
	
$this->width = $type_ph_w;
	
	
	
$this->height= $type_ph_h;
	
	
}

	
	
if ( ! ($src_img = $this->image_create_gd()))
	
	
{
	
	
	
return FALSE;
	
	
}
	
	
if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
	
	
{
	
	
	
$create
	
= 'imagecreatetruecolor';
	
	
	
$copy
	
= 'imagecopyresampled';
	
	
}
	
	
else
	
	
{
	
	
	
$create
	
= 'imagecreate';
	
	
	
$copy
	
= 'imagecopyresized';
	
	
}

	
	
$rst_img = $create($this->width, $this->height);
	
	
if ($this->image_type == 3) // png we can actually preserve transparency
	
	
{
	
	
	
imagealphablending($rst_img, FALSE);
	
	
	
imagesavealpha($rst_img, TRUE);
	
	
}

	
	
$copy($rst_img, $src_img, 0, 0, 0, 0, $this->width, $this->height, $this->orig_width, $this->orig_height);

	
	
$dst_img = $create($crop_w, $crop_h);
	
	
if ($this->image_type == 3) // png we can actually preserve transparency
	
	
{
	
	
	
imagealphablending($dst_img, FALSE);
	
	
	
imagesavealpha($dst_img, TRUE);
	
	
}

	
	
if( $type_crop == 'square')
	
	
{
	
	
	
$crop_x = ( $type_origin == 'w_rectangle' ) ? ceil( ($this->width - $this->height) / 2 ) : 0 ;
	
	
	
$crop_y = ( $type_origin == 'h_rectangle' ) ? ceil( ($this->height - $this->width) / 2 ) : 0 ;
	
	
}else{
	
	
	
$crop_x = ( $type_origin == 'w_rectangle' ) ? ceil( ($this->width - $crop_w) / 2 ) : 0 ;
	
	
	
$crop_y = ( $type_origin == 'h_rectangle' ) ? ceil( ($this->height - $crop_h) / 2 ) : 0 ;
	
	
}

	
	
$copy($dst_img, $rst_img, 0, 0, $crop_x, $crop_y, $crop_w, $crop_h, $crop_w, $crop_h);
	
	
$this->width = $crop_w;
	
	
$this->height = $crop_h;

	
	
//  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($rst_img);
	
	
imagedestroy($dst_img);
	
	
imagedestroy($src_img);
	
	
// Set the file to 777
	
	
@chmod($this->full_dst_path, FILE_WRITE_MODE);
	
	
return TRUE;
	
}
	
// --------------------------------------------------------------------
}
// END Image_lib Class

/* End of file Soup_Image_lib.php */
/* Location: ./app/libraries/Soup_Image_lib.php */

--
Mesak Blog
http://mesak.tw
--
※ 作者: mesak 時間: 2013-09-28 01:51:42\
※ 編輯: mesak 時間: 2014-02-04 17:56:29
※ 看板: Mesak 文章推薦值: 0 目前人氣: 0 累積人氣: 125 
分享網址: 複製 已複製
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇