【问题标题】:How to upload image, resize it and how the resized image is displayed in codeigniter?如何上传图片、调整图片大小以及调整后的图片如何在 codeigniter 中显示?
【发布时间】:2016-05-18 13:02:25
【问题描述】:

我的控制器:

  public function addgalleryProcess()
        {


            $height = $this->input->POST('height');
            $width = $this->input->POST('width');


            $config['upload_path'] = './assets/images/gallery';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $config['max_width']  = '';
            $config['max_height']  = '';
            $config['overwrite'] = TRUE;
            $config['remove_spaces'] = TRUE;

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload())
            {
                  $error = array('error' => $this->upload->display_errors());
                  $this->load->view('admin/addgallery', $error);
            }
            else
            {

                 $upload_data = $this->upload->data();

                //resize:

                $config1['image_library'] = 'gd2';
                $config1['source_image'] = $upload_data['full_path'];
                $config1['maintain_ratio'] = TRUE;
                $config1['create_thumb'] = TRUE;
                $config1['width'] = $width;
                $config1['height'] = $height;
                $this->load->library('image_lib', $config1); 
                $this->image_lib->resize();

                 $this->adminModel->galleryimages($upload_data);
                 $this->load->view('admin/homeView'); 
              }

我的模特:

public function galleryimages($image_data = array())
{

    $data = array(
    'image' => $image_data['file_name'],

    );

        $this->db->insert('gallery', $data);
}

此处图片已正确上传且正常工作,但无法调整大小。我必须显示具有指定宽度和高度的调整大小的图像。我是新来的。 提前致谢。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    试试这个:

    对于图像调整大小,我建议使用“TimThumb”TimThumb – PHP Image Resizer

    https://www.binarymoon.co.uk/projects/timthumb/

    我在我的项目中使用了 timthumb,比如

    <img src="<?php echo base_url('assets/common/timthumb') . '/timthumb.php?src=./assets/myuploads/myimagename.jpg&w=245&h=164'; ?>" alt="">
    

    我已将 timthumb.php 放在“assets”文件夹中,并将图像上传到“assets/myuploads”文件夹中

    【讨论】:

      【解决方案2】:

      不要分散注意力,如果您想在通过网络上传之前调整图像大小,您可以将其 javascript 到画布,然后将画布图像发送到 toDataURL。这将有助于防止有人上传大量内容,而您的网络和服务器会因此而挣扎。 这也取决于您的客户端是否在 HTML 客户端中。 以下文章可以对此有所帮助: https://stackoverflow.com/a/10334170/811827

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-25
        • 2011-05-28
        • 2019-10-09
        • 1970-01-01
        • 1970-01-01
        • 2011-08-05
        • 1970-01-01
        • 2023-04-01
        相关资源
        最近更新 更多