如何在codeigniter中裁剪图像?

发布于 2024-09-11 09:39:43 字数 123 浏览 4 评论 0原文

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮是我掰弯的 2024-09-18 09:39:43

您需要设置 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;

上面的代码将图像的左、右、上、下各裁剪 10 个像素。您可以随意将“10”的值更改为您喜欢的值;)

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.

        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 ;)

山人契 2024-09-18 09:39:43

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

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

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

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.

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().

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.

盛夏已如深秋| 2024-09-18 09:39:43

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

所以这里是 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();
        }
    }

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

so here is the solution with imagemagick and works great.

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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文