Welcome Guest, Not a member yet? Register   Sign In
ImageMagick Image Stretching
#1

[eluser]dudeami0[/eluser]
Well I have been working on a site that heavily deals in editing of images. Found a little 'bug' or 'missing feature' with imagemagick and the Image Library. That feature is the 'maintain_ratio' option. The following code in application/libraries/MY_Image_lib.php will fix that problem:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Image_lib extends CI_Image_lib {

    function image_process_imagemagick($action = 'resize') {
        //  Do we have a vaild library path?
        if ($this->library_path == '') {
            $this->set_error('imglib_libpath_invalid');
            return FALSE;
        }
        
        if ( ! preg_match("/convert$/i", $this->library_path)) {
            $this->library_path = rtrim($this->library_path, '/').'/';
            $this->library_path .= 'convert';
        }

        // Create the command
        $cmd = $this->library_path." -quality ".$this->quality;

        if ($action == 'crop') {
            $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
        } elseif ($action == 'rotate') {
            switch ($this->rotation_angle) {
                case 'hor'    : $angle = '-flop';
                    break;
                case 'vrt'    : $angle = '-flip';
                    break;
                default        : $angle = '-rotate '.$this->rotation_angle;
                    break;
            }

            $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
        } else {
            $cmd .= " -resize ".$this->width."x".$this->height . (!$this->maintain_ratio ? '!' : '') . " \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
        }

        $retval = 1;

        // Execute the command
        @exec($cmd, $output, $retval);

        //    Did it work?
        if ($retval > 0) {
            $this->set_error('imglib_image_process_failed');
            return FALSE;
        }

        // Set the file to 777
        @chmod($this->full_dst_path, FILE_WRITE_MODE);

        return TRUE;
    }

}

The only change is on line 34 were I added the switch to disable maintaining aspect ratios:

Code:
$cmd .= " -resize ".$this->width."x".$this->height . (!$this->maintain_ratio ? '!' : '') . " \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";

Which adds the ! flag to not maintain aspect ratio in imagemagick.

Hope this helps someone out there Smile I was wondering why it wasn't working for the longest time!
#2

[eluser]cereal[/eluser]
This will be helpful! Thank you! Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB