如何使用 Imagemagick 在 CodeIgniter 中进行重力缩放?

发布于 2024-11-05 06:43:18 字数 2066 浏览 2 评论 0原文

我正在尝试以这种方式裁剪图像(通过 Curl 抓取),以便裁剪后的图像为 171 x 118px。我不是强制将该图像大小调整为 171x118px,而是尝试让它以这种方式工作,以便它抓取图像的任何 171x118px 区域并裁剪该区域。所以原始图像是 http://www.mirzar.com/ember/actual-image .png,它应该输出 http://www.mirzar.com/ember/desired-crop.png 而不是 http://www.mirzar.com/ember/cropped.png

到目前为止,这是我的代码:

            $config['image_library']  = 'ImageMagick';
            $config['library_path']   = '/usr/bin/';
            $config['create_thumb']   = TRUE;
            $config['maintain_ratio'] = FALSE;
            $this->load->library('image_lib', $config);
            $dimensions[0] = array('x'=>171, 'y'=>118, 'dir'=>'small');
            $dimensions[1] = array('x'=>154, 'y'=>105, 'dir'=>'medium');
            $dimensions[2] = array('x'=>312, 'y'=>164, 'dir'=>'large');
            for ($i=0; $i<3;$i++){
                $filesize = filesize($filename);
                $config['image_library']  = 'ImageMagick';
                $config['library_path']   = '/usr/bin/';
                $config['source_image']   = $filename;
                $config['create_thumb']   = TRUE;
                $config['maintain_ratio'] = FALSE;
                $config['width'] = $dimensions[$i]['x'];
                /* $config['height'] = $dimensions[$i]['y']; */
                $config['master_dim'] = 'width';

                $this->image_lib->test();
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
                $this->saveThumbnail($imageid, $ext, $dimensions[$i]['dir'], $i); 
            }

            // regular site pic
            $config['width']          = 171;
            $config['height']         = 218;
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();

I am trying to crop an image (grabbed via Curl) in such a manner so that the cropped image is 171 x 118px. Instead of force re-sizing that image to be 171x118px, I am trying to have it work in such a manner so that it grabs any 171x118px area of the image and crops that. So of the original image is http://www.mirzar.com/ember/actual-image.png, it should output http://www.mirzar.com/ember/desired-crop.png instead of http://www.mirzar.com/ember/cropped.png.

Here's my code so far:

            $config['image_library']  = 'ImageMagick';
            $config['library_path']   = '/usr/bin/';
            $config['create_thumb']   = TRUE;
            $config['maintain_ratio'] = FALSE;
            $this->load->library('image_lib', $config);
            $dimensions[0] = array('x'=>171, 'y'=>118, 'dir'=>'small');
            $dimensions[1] = array('x'=>154, 'y'=>105, 'dir'=>'medium');
            $dimensions[2] = array('x'=>312, 'y'=>164, 'dir'=>'large');
            for ($i=0; $i<3;$i++){
                $filesize = filesize($filename);
                $config['image_library']  = 'ImageMagick';
                $config['library_path']   = '/usr/bin/';
                $config['source_image']   = $filename;
                $config['create_thumb']   = TRUE;
                $config['maintain_ratio'] = FALSE;
                $config['width'] = $dimensions[$i]['x'];
                /* $config['height'] = $dimensions[$i]['y']; */
                $config['master_dim'] = 'width';

                $this->image_lib->test();
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
                $this->saveThumbnail($imageid, $ext, $dimensions[$i]['dir'], $i); 
            }

            // regular site pic
            $config['width']          = 171;
            $config['height']         = 218;
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();

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

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

发布评论

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

评论(1

金兰素衣 2024-11-12 06:43:18

首先,您的“小”和“中”尺寸在尺寸数组中似乎是错误的。

至于任务本身,您可以简单地使用 codeigniter 的“裁剪”功能来为您提供缩略图的正确尺寸。您可以使用以下几种方法:

1) 使用您描述的方法调整图像大小,但保持图像比例并使用 $config['master_dim'] = 'auto';。然后使用裁剪功能将这个新调整大小的图像裁剪为您想要的精确尺寸。此方法还有一个额外的好处,可以补偿可能小于缩略图的图像!

2)从头开始裁剪图像并完全忽略调整大小。

无论哪种方式,您都会添加类似这样的内容:

$config['x_axis'] = 0;
$config['y_axis'] = 0;
$config['width'] = $dimensions[$i]['x'];
$config['height'] = $dimensions[$i]['y'];
$this->image_lib->initialize($config);
$this->image_lib->crop();

创建更整洁的缩略图的第三种方法是为每个图像找出“最适合”。如下所示:

$size = getimagesize($filename);
$width_ratio = floor($size[0] / $dimensions[$i]['x']);
$height_ratio = floor($size[1] / $dimensions[$i]['y']);
$min_ratio - min($width_ratio, $height_ratio);
$left = (($size[0] - ($dimensions[$i]['x'] * $min_ratio)) / 2);
$right = (($size[1] - ($dimensions[$i]['y'] * $min_ratio)) / 2);
$config['width'] = ($dimensions[$i]['x'] * $min_ratio);
$config['height'] = ($dimensions[$i]['y'] * $min_ratio);
$config['x_axis'] = $left;
$config['y_axis'] = $right;
$this->image_lib->initialize($config);
$this->image_lib->crop();

然后将此新图像的大小调整为您想要的缩略图大小。

请记住,我还没有检查小于 0 的比率,我将把它留给你。

问候,
埃亚卡1

Firstly, it seems your 'small' and 'medium' dimensions are the wrong way round in the dimensions array.

As for the task itself, you could simply use the 'crop' function of codeigniter to give you the correct dimensions for the thumbnail. There are a few methods you could use:

1) Resize the image using the method you describe except maintain the image ratio and use $config['master_dim'] = 'auto';. Then use the crop function to crop this newly resized image to the exact dimensions you desire. This method has the added bonus of compensating for images that may be smaller than your thumbnails!

2) Crop the image from the start and ignore the resize altogether.

Either way, you would be adding something like so:

$config['x_axis'] = 0;
$config['y_axis'] = 0;
$config['width'] = $dimensions[$i]['x'];
$config['height'] = $dimensions[$i]['y'];
$this->image_lib->initialize($config);
$this->image_lib->crop();

A third method which would create much neater thumbnails would be to work out the 'best fit' for each image. Something like the following:

$size = getimagesize($filename);
$width_ratio = floor($size[0] / $dimensions[$i]['x']);
$height_ratio = floor($size[1] / $dimensions[$i]['y']);
$min_ratio - min($width_ratio, $height_ratio);
$left = (($size[0] - ($dimensions[$i]['x'] * $min_ratio)) / 2);
$right = (($size[1] - ($dimensions[$i]['y'] * $min_ratio)) / 2);
$config['width'] = ($dimensions[$i]['x'] * $min_ratio);
$config['height'] = ($dimensions[$i]['y'] * $min_ratio);
$config['x_axis'] = $left;
$config['y_axis'] = $right;
$this->image_lib->initialize($config);
$this->image_lib->crop();

Then resize this new image to your desired thumbnail size.

Bare in mind, I haven't checked for ratios less than 0, I'll leave that to you.

Regards,
eyaka1

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