2017-01-01 96 views
1

我上传一些图像到我的网页,并希望他们的缩略图是从中心裁剪出的方块。我正在使用Codeigniter和gd2。裁剪缩略图图像在Codeigniter广场

这里是到目前为止我的代码:

$config['image_library'] = 'gd2'; 
        $config['source_image'] = $this->userlibrary->picturesdir . $newfilename; 
$config['new_image'] = $this->userlibrary->thumbsdir; 
$config['create_thumb'] = TRUE; 
$config['maintain_ratio'] = TRUE; 
$config['width']= 150; 
$config['height']= 150; 

的图像被很好地缩放,但他们保持它们的纵横比和只有它们的宽度或高度被设定为150,它们不会被裁剪。 设置maintain_ratio仍然不会裁剪图像,而是倾斜它。

我该怎么做?

+0

看看这是你所需要的:http://stackoverflow.com/questions/28002244/crop-resize-image-function-using-gd-library/28008400# 28008400 – Rasclatt

+0

您可以将原始图像裁剪为正方形,然后调整其大小。 – ourmandave

回答

0
//Set config for img library 
$config['image_library'] = 'ImageMagick'; 
$config['library_path'] = '/usr/bin/'; 
$config['source_image'] = $filePath . $fileOldName; 
$config['maintain_ratio'] = false; 

//Set cropping for y or x axis, depending on image orientation 
if ($fileData['image_width'] > $fileData['image_height']) { 
    $config['width'] = $fileData['image_height']; 
    $config['height'] = $fileData['image_height']; 
    $config['x_axis'] = (($fileData['image_width']/2) - ($config['width']/2)); 
} 
else { 
    $config['height'] = $fileData['image_width']; 
    $config['width'] = $fileData['image_width']; 
    $config['y_axis'] = (($fileData['image_height']/2) - ($config['height']/2)); 
} 

//Load image library and crop 
$this->load->library('image_lib', $config); 
$this->image_lib->initialize($config); 
if ($this->image_lib->crop()) { 
    $error = $this->image_lib->display_errors(); 
} 

//Clear image library settings so we can do some more image 
//manipulations if we have to 
$this->image_lib->clear(); 
unset($config); 

来源: https://forum.codeigniter.com/thread-7286.html