这篇文章主要介绍了Codeigniter实现智能裁剪图片的方法,可以做到裁剪后不失真,尽可能保留图片主题含义。需要的朋友可以参考下
一副1024*768大小的图片,裁剪到240*240大小,,裁剪后不失真,尽可能保留图片主题含义。
我使用到的方法:
1. 先将图片等比例缩略到可以裁剪的大小;
如果是宽幅图片,则按高度等比例缩放到高度 = 240px,窄幅图片(高度大于宽度)则按宽度等比例缩放;
2. 按长宽格式居中裁剪;
保留缩略后的图片中间部分;
复制代码 代码如下:
$this->load->library('image_lib');
list($width, $height) = getimagesize("upload/123.jpg");
$config['image_library'] = 'gd2';
$config['source_image'] = 'upload/123.jpg';
$config['maintain_ratio'] = TRUE;
if($width >= $height)
{
$config['master_dim'] = 'height';
}else{
$config['master_dim'] = 'width';
}
$config['width'] = 240;
$config['height'] = 240;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$config['maintain_ratio'] = FALSE;
if($width >= $height)
{
$config['x_axis'] = floor(($width * 240 / $height - 240)/2);
}else{
$config['y_axis'] = floor(($height * 240 / $width - 240)/2);
}
$this->image_lib->initialize($config);
$this->image_lib->crop();

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version
