Welcome Guest, Not a member yet? Register   Sign In
Image Lib - Resize failure
#11

[eluser]gRoberts[/eluser]
Well i thought i had fixed it. It seems that this works for the specific image i had but doesn't work for gifs.

Would there be any reason why it wont work?

I have attached the image that doesn't work using GD2 and CI's image lib.

Code:
public function generate($source, $output, $width, $height) {

            
            $size = getimagesize($source);

            if($size[0] > $width || $size[1] > $height) {

                $src = imagecreatefromjpeg($source);
                
                $original_width = imagesx($src);
                $original_height = imagesy($src);

                if($original_width > $original_height) {
                    $new_width = $width;
                    $new_height = $original_height*($width/$original_width);
                } else {
                    $new_height = $width;
                    $new_width = $original_width*($width/$original_height);
                }
                $op_img = imagecreatetruecolor($new_width, $new_height);
                imagecopyresampled($op_img, $src, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
                imagejpeg($op_img, $output);

            } else
                copy($source, $output);

        }
#12

[eluser]blender[/eluser]
[quote author="gRoberts" date="1207107432"]
Code:
$src = imagecreatefromjpeg($source);
[/quote]

I'm pretty sure the reason the gif won't work is because you are using imagecreatefromjpeg() instead of imagecreatefromgif().

I downloaded the image you attached and tried it using CI's library and it worked fine for me. I'm using CI v1.6.1. Here is my code:
Code:
$max_width = 600;
      $max_height = 400;
      // resize and copy to the image dir
      $config['image_library'] = 'GD2';
      $config['source_image'] = "$originals_directory/$image";
      $config['new_image'] = "$photo_directory/$image";
      $config['maintain_ratio'] = TRUE;
      $config['width'] = 600;
      $config['height'] = 400;

      $this->image_lib->clear();
      $this->image_lib->initialize($config);
      $this->image_lib->resize();

Have you verified that your "source_image" and "new_image" locations are correct and that you have write access to the new_image location?
#13

[eluser]gRoberts[/eluser]
Hey thanks for the reply.

I'm still using the CI code, but now I am using seperate code to calculate the size to resize to, and then passing these sizes directly rather then expect CI to handle it.

Code:
public function generate($source, $output, $width, $height) {

            $size = getimagesize($source);
            $original_width = $size[0];
            $original_height = $size[1];

            if($original_width > $original_height) {
                $new_width = $width;
                $new_height = $original_height*($width/$original_width);
            } else {
                $new_height = $width;
                $new_width = $original_width*($width/$original_height);
            }

            $config['image_library'] = 'GD2';
            //$config['library_path'] = '/usr/bin/X11';
            $config['source_image'] = $source;
            $config['maintain_ratio'] = TRUE;
            $config['create_thumb'] = false;
            $config['new_image'] = $output;
            $config['quality'] = 100;
            $config['width'] = $new_width;
            $config['height'] = $new_height;
            
            $this->base->image_lib->clear(); // added
            $this->base->image_lib->initialize($config);
            //echo $this->base->image_lib->width.'*'.$this->base->image_lib->height;
            if(!$this->base->image_lib->resize())
                die($this->base->image_lib->display_errors());
            return array(
                'width' => $new_width,
                'height' => $new_height
            );
        }




Theme © iAndrew 2016 - Forum software by © MyBB