0

I need to resize my images according to width only , not height :( since i am using ** $config['maintain_ratio'] = TRUE;** , codeigniter solves it automatically (thats the problem) sometimes i get a black solid as an extra width or height ,

i have looked at all of the questions relating to codegniter's image library and i couldn't find anything related to my problem :(

Here is a description on the final result... I need to resize according to width only , not height :(

After uploading am having this ...

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

Then i am take caring of the cropping part

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

to sum up , Someone uploads an image , it gets resized to according to width only Step 1 and then it gets cropped Step 2

PS: incase someone needed it md_image

Fahad
  • 1,943
  • 22
  • 27
  • 1
    Where is the problem happening, in the resize or the crop? – John Corry Apr 10 '14 at 14:43
  • Sorry for late reply, Sometimes when the image's height is large i.e +700px it crops without resizing , if the height is a bit larger i.e +100px it leaves a black solid after resizing ... hopes this info helps @jcorry , Thanks. – Fahad Apr 17 '14 at 10:00
  • if i get image_magick to resize according to height only , its a big achievement to me , cropping is not a problem ... if i give it the right dimensions width->700px height->(*greater than >400px*) it crops great! – Fahad Apr 17 '14 at 10:06

2 Answers2

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();
  • 3
    Please consider elaborating on this solution with code comments or an explanation of what you changed and how/why it works. Simply posting code will not help the original poster or anyone else learn much. I recommend reviewing [How to write a good answer](http://stackoverflow.com/help/how-to-answer). – Daniel May 15 '14 at 16:48
  • Thank you for responding user3641704 , I dont know but for some reason , I am having my **image distorted** first before cropping am losing the image resolution , _May be your should consider elaborating your answer just like what @Daniel said_ :) – Fahad May 24 '14 at 13:06
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();
}
Hurkan
  • 23
  • 8
  • Thanks for taking the time to write this, but @user3641704 answered it already , I will consider testing your solution though in my future projects – Fahad Oct 15 '15 at 12:40