Php 在Codeigniter中将缩略图裁剪为方形
我上传一些图片到我的网页,并希望他们的缩略图是从中心裁剪正方形。我正在使用Codeigniter和gd2 以下是我目前的代码:Php 在Codeigniter中将缩略图裁剪为方形,php,css,image,codeigniter,Php,Css,Image,Codeigniter,我上传一些图片到我的网页,并希望他们的缩略图是从中心裁剪正方形。我正在使用Codeigniter和gd2 以下是我目前的代码: $config['image_library'] = 'gd2'; $config['source_image'] = $this->userlibrary->picturesdir . $newfilename; $config['new_image'] = $this->userlibrary->thu
$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,它们不会被裁剪。
设置mainter\u ratio
仍然不会裁剪图像,而是使其倾斜
我怎么能这样做
//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);
资料来源:
回复太晚了,但我在寻找同样的东西时发现了这篇文章。我想我会和其他人分享我的解决方案
$config['source_image'] = 'path/to/image';
//Find smallest dimension
$imageSize = $this->image_lib->get_image_properties($config['source_image'], TRUE);
$newSize = min($imageSize);
$config['image_library'] = 'gd2';
$config['width'] = $newSize;
$config['height'] = $newSize;
$config['y_axis'] = ($imageSize['height'] - $newSize) / 2;
$config['x_axis'] = ($imageSize['width'] - $newSize) / 2;
$this->image_lib->initialize($config);
if(!$this->image_lib->crop())
{
echo $this->image_lib->display_errors();
}
看看这是否是您需要的:您可以将原始图像裁剪成正方形,然后调整其大小。