Welcome Guest, Not a member yet? Register   Sign In
Resize and Crop problem with the Image Manipulation Class
#1

[eluser]RogerMore[/eluser]
Hi guys,

I'm working on a project which involves uploading backgrounds for a webapp which has, when resized and cropped, the dimensions 1024x768 with no black bars. That means that if an image is uploaded that is too big (or small) or has a different ratio in width/height, the processed image is 1024x768 which is all image. In the example below I want to resize/crop a simple image which is too wide when uploaded (1366x768).

I have the following code which has to do the processing of the just uploaded picture, but somehow the outcome is wrong. The image is cropped the right way so it has 1024 pixels of width, but for some reason I only see the lower part of the image wich is in the upper part of the processed image, and the low half is black.

First the code:

Code:
$image_path = 'test/android-wallpaper.jpg';
  $new_image_path = 'test/android-wallpaper-cropped.jpg';
  
  $width = 1024;
  $height = 768;
  
        $this->load->library('image_lib');
      
        //The original sizes
        $original_size = getimagesize($image_path);
        $original_width = $original_size[0];
        $original_height = $original_size[1];
        $ratio = $original_width / $original_height;
      
        //The requested sizes
        $requested_width = $width;
        $requested_height = $height;
      
        //Initialising
        $new_width = 0;
        $new_height = 0;
      
        //Calculations
        if ($requested_width > $requested_height) {
            $new_width = $requested_width;
            $new_height = $new_width / $ratio;
            if ($requested_height == 0)
                $requested_height = $new_height;
          
            if ($new_height < $requested_height) {
                $new_height = $requested_height;
                $new_width = $new_height * $ratio;
            }
      
        }
        else {
            $new_height = $requested_height;
            $new_width = $new_height * $ratio;
            if ($requested_width == 0)
                $requested_width = $new_width;
          
            if ($new_width < $requested_width) {
                $new_width = $requested_width;
                $new_height = $new_width / $ratio;
            }
        }
      
        $new_width = ceil($new_width);
        $new_height = ceil($new_height);
      
        //Resizing         $c
        $config['image_library'] = 'gd2';
        $config['source_image'] = $image_path;
        $config['new_image'] = $new_image_path;
        $config['maintain_ratio'] = FALSE;
        $config['height'] = $new_height;
        $config['width'] = $new_width;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        $this->image_lib->clear();
    
        //Crop if both width and height are not zero
        if (($width != 0) && ($height != 0)) {
            $x_axis = floor(($new_width - $width) / 2);
            $y_axis = floor(($new_height - $height) / 2);
          
            //Cropping
            $config = array();
            $config['source_image'] = $new_image_path;
            $config['maintain_ratio'] = FALSE;
            $config['new_image'] = $new_image_path;
            $config['width'] = $width;
            $config['height'] = $height;
            $config['x_axis'] = $x_axis;
            $config['y_axis'] = $y_axis;
            $this->image_lib->initialize($config);
            $this->image_lib->crop();
            $this->image_lib->clear();
        }


I have added an attachment with a processed image, where you can see that the upper half is the lowerhalf of the image and the lower half is black.
I have tried a lot, but I'm running out of ideas.

Anyone?! Help is much appriciated...

Greetz,

Roger
#2

[eluser]RogerMore[/eluser]
Hey guys,

I guess that every developer now and then has a problem which seems like staring into a void when you know that your code is ok and problems still occur. This is one of those times for me, because the solution wat staring right in my face.

Sorry for who looked into the problem, but I created the little 'Houston' myself by fooling around with the image_lib code and totally forget about that maybe some things were still changed. :red:

After I replaced the original image_lib all things were fine. The code in my previous post works like a charm.

Greetings,

Roger




Theme © iAndrew 2016 - Forum software by © MyBB