当我们上传时,如何减少codeigniter中的图像的文件大小(kb)? [英] how to reduce the file size in kb of an image in codeigniter when we upload?

查看:106
本文介绍了当我们上传时,如何减少codeigniter中的图像的文件大小(kb)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,用户可以拖放多个图片。我需要调整大小和存储它作为缩略图,我还必须减少大图像在kb,而不会失去图像宽度和高度。

I have a page where user will drag and drop multiple images. I need to resize and store it as thumbnail and also i have to reduce the big images in kb without losing the image width and height. What would be the best way do it in codeigniter?

     if ($_FILES["file"]["name"])
        {

        $targetPath = APPPATH . 'uploads/portfolios/';

        $result = $this->do_upload("file", $targetPath);
        $data =  array();
        if (!$result['status']) {
                $data['error_msg'] ="Can not upload Image for " . $result['error'] . " ";

        }

                else {

    $this->resize_image($targetPath . $result['upload_data']['file_name'],$targetPath,'120','120');


    $file_name = $result['upload_data']['raw_name'].'_thumb'.$result['upload_data']['file_ext'];    


        }

function resize_image($sourcePath, $desPath, $width = '500', $height = '500')
{

    $this->load->library('image_lib');
    $this->image_lib->clear();
    $config['image_library'] = 'gd2';
    $config['source_image'] = $sourcePath;
    $config['new_image'] = $desPath;
    //$config['quality'] = '100%';
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = false;
    $config['thumb_marker'] = '_thumb';
    $config['width'] = 120;
    $config['height'] = 120;
 $this->image_lib->initialize($config);

    if ($this->image_lib->resize())

        return true;
    return false;
}

  function do_upload($htmlFieldName, $path)
{
    $config['file_name'] = time();
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '20000';
    //$config['max_width'] = '2000';
    //$config['max_height'] = '2000';
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    unset($config);
    if (!$this->upload->do_upload($htmlFieldName))
    {
        return array('error' => $this->upload->display_errors(), 'status' => 0);
    } else
    {
        return array('status' => 1, 'upload_data' => $this->upload->data());
    }
}


推荐答案

I有类似的问题,通过原生的php函数解决。

I had similar problem which is solved via native php function.

归功于 pez-cuckow 他的原始答案
从它的问题也有一个改进,但它删除了对gif图像的支持。

Credit to pez-cuckow from his original answer. From it's question also have an improvement but it remove support for gif image.

function compressImage($source_url, $destination_url, $quality) {

  $info = getimagesize($source_url);

  if ($info['mime'] == 'image/jpeg') 
    $image= imagecreatefromjpeg($source_url);
  elseif ($info['mime'] == 'image/gif')
    $image = imagecreatefromgif($source_url);
  elseif ($info['mime'] == 'image/png')
    $image = imagecreatefrompng($source_url);

  //save file
  imagejpeg($image, $destination_url, $quality);

  //return destination file
  return $destination_url;
}

这篇关于当我们上传时,如何减少codeigniter中的图像的文件大小(kb)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆