【问题标题】:Resize image according to width only & crop Extra Height , Codeigniter仅根据宽度调整图像大小并裁剪额外高度,Codeigniter
【发布时间】:2014-05-03 13:16:42
【问题描述】:

我需要根据 仅宽度 调整图像大小,而不是高度 :( 因为我正在使用** $config['maintain_ratio'] = TRUE;** ,codeigniter 会自动解决它(这就是问题)有时我会得到一个黑色实心作为额外的宽度或高度,

我查看了所有与 codegniter 的图片相关的问题 图书馆,我找不到任何与我的问题相关的东西:(

这是对最终结果的描述...

上传后我有这个...

$this->load->library('md_image');
        $source='assets/img/hotellist/'.$data1['hotel_pictures'];
        $width=746;
        $height=400;
        $dest = FALSE;
        //$this->md_image->resize_image($source, $width, $height, $source);
            $config['image_library'] = 'gd2';//imagemagik
            $config['source_image'] = 'assets/img/hotellist/'.$data1['hotel_pictures'];
            //$config['image_library'] = 'imagemagick';
            //$config['library_path'] = '/usr/X11R6/bin/';
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['width']     = 746;
            $config['height']   = 400;
            $config['quality']   = 75;
            $config['encrypt_name'] = TRUE;
            $config['remove_spaces'] = TRUE;
            //$config['x_axis'] = 100;
            //$config['y_axis'] = 300;
            $img =$config['source_image'];
            //var_dump('check problem',$config['source_image']);

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

            $this->image_lib->resize();

然后我负责裁剪部分

$new_img =$this->md_image->crop_to_ratio($source, $width, $height, $x = 17, $y = 9, $dest = FALSE);

总结一下,有人上传图片,它只根据宽度调整大小 然后它被裁剪

PS:如果有人需要它md_image

【问题讨论】:

  • 问题出在哪里,调整大小还是裁剪?
  • 抱歉回复晚了,有时当图像的高度很大时,即+700px,它会在不调整大小的情况下进行裁剪,如果高度稍大一点,即+100px,它会在调整大小后留下黑色实心......希望这个信息帮助@jcorry,谢谢。
  • 如果我让 image_magick 只根据高度调整大小,这对我来说是一个很大的成就,裁剪不是问题......如果我给它正确的尺寸宽度->700px 高度->(大于 >400 像素)它的效果很好!

标签: php image codeigniter imagemagick gd2


【解决方案1】:
function resize_than_crop($path, $new_path, $width = 400, $height = 746) {

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

    $this->image_lib->initialize(array(
        'image_library' => 'gd2',
        'source_image' => $path,
        'new_image' => $new_path,
        'maintain_ratio' => true,
        'master_dim' => 'width',
        'width' => $width
    ));
    $this->image_lib->resize();

    $this->image_lib->clear();

    $this->image_lib->initialize(array(
        'image_library' => 'gd2',
        'source_image' => $new_path,
        'height' => $height
    ));
    $this->image_lib->crop();
}

【讨论】:

  • 感谢您抽出宝贵的时间来写这篇文章,但@user3641704 已经回答了,我会考虑在以后的项目中测试您的解决方案
【解决方案2】:
$this->load->library('md_image');
        $source='assets/img/hotellist/'.$data1['hotel_pictures'];
        $width=746;
        $height=400;
        $size = getimagesize($source);
        $resize_height=($size[1]*746)/$size[0];
        $dest = FALSE;
        //$this->md_image->resize_image($source, $width, $height, $source);
            $config['image_library'] = 'gd2';//imagemagik
            $config['source_image'] = 'assets/img/hotellist/'.$data1['hotel_pictures'];
            //$config['image_library'] = 'imagemagick';
            //$config['library_path'] = '/usr/X11R6/bin/';
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['width']     = 746;
            $config['height']   = $resize_height;
            $config['quality']   = 75;
            $config['encrypt_name'] = TRUE;
            $config['remove_spaces'] = TRUE;
            //$config['x_axis'] = 100;
            //$config['y_axis'] = 300;
            $img =$config['source_image'];
            //var_dump('check problem',$config['source_image']);

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

            $this->image_lib->resize();

【讨论】:

  • 请考虑使用代码 cmets 详细说明此解决方案,或解释您更改的内容及其工作方式/原因。简单地发布代码不会帮助原始海报或其他任何人学到很多东西。我建议查看How to write a good answer
  • 感谢您对 user3641704 的回复,我不知道,但由于某种原因,我的 图像失真 在裁剪之前我首先会丢失图像分辨率,可能是您的应该考虑像@Daniel 所说的那样详细说明您的答案 :)
  • 谢谢@user3641704 #Cumali :)
猜你喜欢
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 2013-04-28
相关资源
最近更新 更多