Home php教程 php手册 Codeigniter实现智能裁剪图片的方法

Codeigniter实现智能裁剪图片的方法

Jun 06, 2016 pm 08:21 PM
codeigniter

这篇文章主要介绍了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();

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement custom middleware in CodeIgniter How to implement custom middleware in CodeIgniter Jul 29, 2023 am 10:53 AM

How to implement custom middleware in CodeIgniter

CodeIgniter middleware: Accelerate application responsiveness and page rendering CodeIgniter middleware: Accelerate application responsiveness and page rendering Jul 28, 2023 pm 06:51 PM

CodeIgniter middleware: Accelerate application responsiveness and page rendering

PHP development: Using CodeIgniter to implement MVC pattern and RESTful API PHP development: Using CodeIgniter to implement MVC pattern and RESTful API Jun 16, 2023 am 08:09 AM

PHP development: Using CodeIgniter to implement MVC pattern and RESTful API

How to use the database query builder (Query Builder) in the CodeIgniter framework How to use the database query builder (Query Builder) in the CodeIgniter framework Jul 28, 2023 pm 11:13 PM

How to use the database query builder (Query Builder) in the CodeIgniter framework

How to use CodeIgniter5 framework in php? How to use CodeIgniter5 framework in php? Jun 01, 2023 am 11:21 AM

How to use CodeIgniter5 framework in php?

CodeIgniter middleware: Provides secure file upload and download functions CodeIgniter middleware: Provides secure file upload and download functions Aug 01, 2023 pm 03:01 PM

CodeIgniter middleware: Provides secure file upload and download functions

How to use the PHP framework CodeIgniter to quickly build a backend management system How to use the PHP framework CodeIgniter to quickly build a backend management system Jun 27, 2023 am 09:46 AM

How to use the PHP framework CodeIgniter to quickly build a backend management system

Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services Jun 27, 2023 pm 02:49 PM

Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services

See all articles