使用库image_lib,Codeigniter调整Gif大小而不丢失动画

时间:2017-05-23 10:37:23

标签: codeigniter resize gif

我想如何使用带有image_lib的Codeigniter来调整Gif的大小而不会丢失动画这可能吗?

有我的功能

公共函数裁剪($ width,$ height,$ xaxis,$ yaxis,$ img)     {         $ img = is_file(_UPLOADS_DIR_。$ img)? $ img:'placeholder.jpg';

    $ext = substr($img, strrpos($img, '.') + 1);
    $img_name = str_replace('.' . $ext, '', $img);
    $file_name = $img_name . '-' . $width . '-' . $height . '-' . $xaxis . '-' . $yaxis . '.' . $ext;

    if (!is_file(_THUMBS_DIR_ . $file_name)) {

        log_message('info', 'Create thumb: '.$file_name);
        $config = array();
        $config['source_image'] = _UPLOADS_DIR_ . $img;
        $config['new_image'] = _THUMBS_DIR_ . $file_name;
        list($w, $h) = getimagesize($config['source_image']);

        $canvas_ratio = $width / $height;
        $img_ratio = $w / $h;

        $config['width'] = round(($canvas_ratio > $img_ratio) ? $width : $height / $h * $w);
        $config['height'] = round(($canvas_ratio < $img_ratio) ? $height : $width / $w * $h);
        $this->image_lib->initialize($config);
        $this->image_lib->resize();

        $config = array();
        $config['source_image'] = _THUMBS_DIR_ . $file_name;
        $config['maintain_ratio'] = false;
        $config['width'] = $width;
        $config['height'] = $height;
        list($w, $h) = getimagesize($config['source_image']);

        $config['x_axis'] = ($xaxis > 0) ? ($width - $w) * ($xaxis / -100) : 0;
        $config['y_axis'] = ($yaxis > 0) ? ($height - $h) * ($yaxis / -100) : 0;

        $this->image_lib->initialize($config);
        $this->image_lib->crop();

        chmod(_THUMBS_DIR_ . $file_name, 777);
    }

    switch ($ext) {
        case 'jpg':
        case 'jpeg':
        header('Content-Type: image/jpg');
        break;
        case 'png':
        header('Content-Type: image/png');
        break;
        case 'gif':
        header('Content-Type: image/gif');
        break;
    }
    readfile(_THUMBS_DIR_ . $file_name);
}

0 个答案:

没有答案