php 如何在codeigniter中裁剪图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3325106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:20:51  来源:igfitidea点击:

how to crop image in codeigniter?

phpcodeigniterimage-manipulation

提问by trrrrrrm

i'm using codeigniter to build a project and right now i need to create a thumb depending on user choice. like he will give me X1,Y1,X2,Y2,X3,Y3,X4,Y4 i want to crop the image depending on that 4 points. i checked the image manipulation class. the crop function seems to be very strange. any help please ?

我正在使用 codeigniter 来构建一个项目,现在我需要根据用户的选择创建一个拇指。就像他会给我 X1,Y1,X2,Y2,X3,Y3,X4,Y4 我想根据那 4 个点裁剪图像。我检查了图像处理类。裁剪功能似乎很奇怪。有什么帮助吗?

回答by tnylea

You will need to set the x-axis (left), width (right), y-axis (top), and height (bottom). You need to be sure to set the width and height of the image.

您需要设置 x 轴(左)、宽度(右)、y 轴(顶部)和高度(底部)。您需要确保设置图像的宽度和高度。

        list($width, $height, $type, $attr) = getimagesize($img);

        $CI->load->library('image_lib');

        $config['image_library'] = 'gd2';
        $config['source_image'] = $img;
        $config['x_axis'] = '10';
        $config['y_axis'] = '10';
        $config['maintain_ratio'] = FALSE;
        $config['width'] = $width-10;
        $config['height'] = $height-10;

The code above will crop the image by 10 pixels on the left, right, top, and bottom. you can feel free to change the value of '10' to whichever value you prefer ;)

上面的代码将在左侧、右侧、顶部和底部将图像裁剪 10 个像素。您可以随意将“10”的值更改为您喜欢的任何值;)

回答by C. E.

I know that the documentation is sparse on this particular function in the image library. The crop function asks you only to supply to axises. It will then cut along these axes and return the part of the image which is closer to the center. So if you set the x axis to 10 and the y axis to 10 it will remove the top 10px of the image and the left 10 px of the image. Similarly, if you set the x axis to the image width - 10 it will crop 10 pixels from the right of the image.

我知道有关图像库中此特定功能的文档很少。裁剪功能只要求您提供给轴。然后它将沿着这些轴切割并返回靠近中心的图像部分。因此,如果您将 x 轴设置为 10,将 y 轴设置为 10,它将删除图像的顶部 10 像素和图像的左侧 10 像素。同样,如果您将 x 轴设置为图像宽度 - 10,它将从图像右侧裁剪 10 个像素。

What your four positions tell you are really four different axises. Therefore, you need to do two operations. You just need to change the axises in between each $this->image_lib->crop().

你的四个位置告诉你的实际上是四个不同的轴。因此,您需要进行两次操作。您只需要更改每个 $this->image_lib->crop() 之间的轴。

How to figure out these axises depends on how you get this data. In an array, as separate values etc. so I won't go into this.

如何找出这些轴取决于您如何获取这些数据。在数组中,作为单独的值等,所以我不会讨论这个。

回答by pinarella

i am not getting any results with gd2 library. it always resizes image, but newer crops.

我没有得到 gd2 库的任何结果。它总是调整图像大小,但更新作物。

so here is the solution with imagemagick and works great.

所以这里是 imagemagick 的解决方案,效果很好。

public function resize_prep($path, $file){
        $config['image_library'] = 'imagemagick';
        $config['library_path'] = '/usr/bin';
        $config['source_image'] = $path;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = FALSE;
        $config['x_axis'] = 300;
        $config['y_axis'] = 300;
        //$config['width'] = 650;
        //$config['height'] = 353;      
        $config['new_image'] = './uploads/'.$file;

        $this->load->library('image_lib', $config);
        //$this->image_lib->crop();

        $this->image_lib->initialize($config); 
        if (!$this->image_lib->crop()){
            echo $this->image_lib->display_errors();
        }
    }