douyan2680 2011-05-02 19:52
浏览 30
已采纳

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

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();
  • 写回答

1条回答 默认 最新

  • duancai7002 2011-05-02 23:14
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题