[英]Crop thumbnail image to square in Codeigniter
我正在將一些圖像上傳到我的網頁,並希望它們的縮略圖是從中心裁剪的正方形。 我正在使用Codeigniter和gd2。
到目前為止,這是我的代碼:
$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
仍不會裁剪圖像,而是使其傾斜。
我該怎么辦?
超級遲到的回復,但是當我尋找相同的東西時,我碰到了這篇文章。 以為我會與其他尋求相同解決方案的人分享我所使用的解決方案。
$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();
}
//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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.