使用CI中的坐标调整图像大小

时间:2009-12-24 05:50:36

标签: php codeigniter

我正在尝试用坐标实现裁剪图像。但如果是坐标,它就不会成功。我的代码是:

function image_thumb($image_path, $height, $width, $x=0, $y=0)
{
    // Get the CodeIgniter super object
    $CI =& get_instance();

    // Path to image thumbnail
    $image_thumb = dirname($image_path) . '/' . $height . '_' . $width . '.jpg';

    if( ! file_exists($image_thumb))
    {
        // LOAD LIBRARY
        $CI->load->library('image_lib');

        // CONFIGURE IMAGE LIBRARY
        $config['x_axis']            = $x;
        $config['y_axis']             = $y;
        $config['image_library']    = 'gd2';
        $config['source_image']        = $image_path;
        $config['new_image']        = $image_thumb;
        $config['maintain_ratio']    = TRUE;
        $config['height']            = $height;
        $config['width']            = $width;
        $CI->image_lib->initialize($config);
        $CI->image_lib->resize();
        $CI->image_lib->clear();
    }

    return '<img src="' . dirname($_SERVER['SCRIPT_NAME']) . '/' . $image_thumb . '" />';
} 

请帮帮我......

2 个答案:

答案 0 :(得分:2)

也许是因为你没有使用正确的功能 ......

$CI->image_lib->resize();

您的意思是crop()吗?

$CI->image_lib->crop();

我讨厌这样说,但是RTM


这是一个可以同时裁剪和调整大小的功能:

function ImageCR($source, $crop = null, $scale = null, $destination = null)
{
    $source = @ImageCreateFromString(@file_get_contents($source));

    if (is_resource($source) === true)
    {
        $size = array(ImageSX($source), ImageSY($source));

        if (isset($crop) === true)
        {
            $crop = array_filter(explode('/', $crop), 'is_numeric');

            if (count($crop) == 2)
            {
                $crop = array($size[0] / $size[1], $crop[0] / $crop[1]);

                if ($crop[0] > $crop[1])
                {
                    $size[0] = $size[1] * $crop[1];
                }

                else if ($crop[0] < $crop[1])
                {
                    $size[1] = $size[0] / $crop[1];
                }

                $crop = array(ImageSX($source) - $size[0], ImageSY($source) - $size[1]);
            }

            else
            {
                $crop = array(0, 0);
            }
        }

        else
        {
            $crop = array(0, 0);
        }

        if (isset($scale) === true)
        {
            $scale = array_filter(explode('*', $scale), 'is_numeric');

            if (count($scale) >= 1)
            {
                if (empty($scale[0]) === true)
                {
                    $scale[0] = $scale[1] * $size[0] / $size[1];
                }

                else if (empty($scale[1]) === true)
                {
                    $scale[1] = $scale[0] * $size[1] / $size[0];
                }
            }

            else
            {
                $scale = array($size[0], $size[1]);
            }
        }

        else
        {
            $scale = array($size[0], $size[1]);
        }

        $result = ImageCreateTrueColor($scale[0], $scale[1]);

        if (is_resource($result) === true)
        {
            ImageFill($result, 0, 0, IMG_COLOR_TRANSPARENT);
            ImageSaveAlpha($result, true);
            ImageAlphaBlending($result, true);

            if (ImageCopyResampled($result, $source, 0, 0, $crop[0] / 2, $crop[1] / 2, $scale[0], $scale[1], $size[0], $size[1]) === true)
            {
                if (preg_match('~gif$~i', $destination) >= 1)
                {
                    return ImageGIF($result, $destination);
                }

                else if (preg_match('~png$~i', $destination) >= 1)
                {
                    return ImagePNG($result, $destination, 9);
                }

                else if (preg_match('~jpe?g$~i', $destination) >= 1)
                {
                    return ImageJPEG($result, $destination, 90);
                }
            }
        }
    }

    return false;
}

像这样使用:

// resize to 400x400 px 
ImageCR('path/to/sourceImg.jpg', null, '400*400', 'path/to/outputImg.jpg');

// crop to a 1:1 ratio (square) from the center
ImageCR('path/to/sourceImg.jpg', '1/1', null, 'path/to/outputImg.jpg');

// crop to a 1:1 ratio (square) from the center AND resize to 400x400 px 
ImageCR('path/to/sourceImg.jpg', '1/1', '400*400', 'path/to/outputImg.jpg');

// crop to a 1:1 ratio (square) from the center AND resize to 400 px width AND maintain aspect ratio 
ImageCR('path/to/sourceImg.jpg', '1/1', '400*', 'path/to/outputImg.jpg');

// crop to a 1:1 ratio (square) from the center AND resize to 400 px height AND maintain aspect ratio 
ImageCR('path/to/sourceImg.jpg', '1/1', '*400', 'path/to/outputImg.jpg');

使用选项进行游戏,如果您有任何疑问,请告诉我。

答案 1 :(得分:0)

我得到了答案。 image_lib不支持裁剪并同时调整大小。所以我必须对代码进行一些修改。

/**
     * Image Process Using GD/GD2
     *
     * This function will resize or crop
     *
     * @access  public
     * @param   string
     * @return  bool
     */
    function image_process_gd($action = 'resize', $org_w = 0, $org_h = 0)
    {
        $v2_override = FALSE;
        // If the target width/height match the source, AND if the new file name is not equal to the old file name
        // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
        if ($this->dynamic_output === FALSE)
        {
            if ($this->orig_width == $this->width AND $this->orig_height == $this->height)
            {
                if ($this->source_image != $this->new_image)
                {
                    if (@copy($this->full_src_path, $this->full_dst_path))
                    {
                        @chmod($this->full_dst_path, DIR_WRITE_MODE);
                    }
                }

                return TRUE;
            }
        }

        // Let's set up our values based on the action
        if ($action == 'crop')
        {
            //  Reassign the source width/height if cropping
            $this->orig_width  = $this->width;
            $this->orig_height = $this->height;

            // GD 2.0 has a cropping bug so we'll test for it
            if ($this->gd_version() !== FALSE)
            {
                $gd_version = str_replace('0', '', $this->gd_version());
                $v2_override = ($gd_version == 2) ? TRUE : FALSE;
            }
        }
        else if ($action == 'resize')
        {
            // If resizing the x/y axis must be zero
            $this->x_axis = 0;
            $this->y_axis = 0;
        } else {
            $this->orig_width  = $org_w;
            $this->orig_height = $org_h;
        }

        //  Create the image handle
        if ( ! ($src_img = $this->image_create_gd()))
        {
            return FALSE;
        }

        //  Create The Image
        //
        //  old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
        //  it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
        //  below should that ever prove inaccurate.
        //
        //  if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
        if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
        {
            $create = 'imagecreatetruecolor';
            $copy   = 'imagecopyresampled';
        }
        else
        {
            $create = 'imagecreate';
            $copy   = 'imagecopyresized';
        }

        /*echo '<br /> Coordinate' . $this->x_axis . " x " . $this->y_axis;
        echo '<br /> Destination size' .$this->width . " x " . $this->height;
        echo '<br /> Originl Size' . $this->orig_width. " x " . $this->orig_height;*/
        $dst_img = $create($this->width, $this->height);
        $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);

        //  Show the image
        if ($this->dynamic_output == TRUE)
        {
            $this->image_display_gd($dst_img);
        }
        else
        {
            // Or save it
            if ( ! $this->image_save_gd($dst_img))
            {
                return FALSE;
            }
        }

        //  Kill the file handles
        imagedestroy($dst_img);
        imagedestroy($src_img);

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

        return TRUE;
    }

调用:

//set image library configuration
            $config['image_library']    = 'gd2';
            $config['source_image']     = $src_path;
            $config['new_image']        = $des_path;
            $config['maintain_ratio']   = FALSE;
            $config['width']            = 150;
            $config['height']           = 150;
            $config['orig_width']       = $width;
            $config['orig_height']      = $height;
            $config['x_axis']           = $x;
            $config['y_axis']           = $y;
            $this->image_lib->initialize($config);

            //process thumb and reset the original with and height
            $this->image_lib->image_process_gd('thumb', $width, $height);