Codeigniter没有调整图像大小

时间:2018-03-06 10:13:14

标签: image codeigniter codeigniter-3

我正在尝试在用户上传时调整图片大小,但它没有调整大小,水印看起来非常小,图像很大,而且在较小的图像上看起来很大

这是我的代码

if (!empty($_FILES['IMG']['tmp_name'])) 
{
    $config["upload_path"] = "./uploads/";
    $config["allowed_types"] = "gif|jpg|png|jpeg";
    $config["max_size"] = "20444448";
    $config["encrypt_name"] = true;
    $this->load->library("upload", $config);
    if ($this->upload->do_upload('IMG')) 
    {
        $photo = $this->upload->data();
        $row1['photo'] = $photo['file_name'];
        $this->ads_photos->create($row1);
        $this->load->library('image_lib');
        //make image smaller
        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/' . $photo['file_name'];
        $config['create_thumb'] = true;
        $config['thumb_marker'] = '_thumb';
        $config['master_dim'] = 'auto';
        $config['quality'] = '70%';
        $config['new_image'] = './uploads/cars-list/'. $photo['file_name'];
        $config['width'] = '700';
        $config['height'] = '300';
        $this->image_lib->clear();
        $this->image_lib->initialize($config);
        $this->image_lib->resize();

        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/cars-list/'. $photo['file_name'];
        $config['wm_type'] = 'overlay';
        $config['wm_overlay_path'] = './uploads/watermark.png';
        //the overlay image (watermark)
        $config['wm_opacity'] = 60;
        $config['wm_vrt_alignment'] = 'bottom';
        $config['wm_hor_alignment'] = 'right';
        $config['create_thumb'] = true;
        $config['new_image'] = './uploads/cars-list/'. $photo['file_name'];
        $config['width'] = '400';
        $config['height'] = '250';
        $this->image_lib->initialize($config);
        $this->image_lib->resize();

        if (!$this->image_lib->watermark()) {
            $this->session->set_userdata('message', $this->image_lib->display_errors());
        }
     //the rest of code is adding the images to database
    }
}

我尝试了很多修复工作,已经过了2周,没有任何工作

2 个答案:

答案 0 :(得分:0)

我有调整大小图像的片段。我已将此代码用于许多项目。

任何控制器:

$this->modelname->resizeImage($originalImagepath,$toWidth,$toHeight,$filename, $path, $extention);

在模型中(modelname.php)

/**
* Resize Image
**/
function resizeImage($originalImage,$toWidth,$toHeight,$filename, $path, $extention){


    $new_width = $toWidth;

    $new_height = $toHeight;
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);

    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;

    if(($width > $toWidth) && ($height > $toHeight))
    {
            // Recalculate new size with default ratio
            if ($yscale<$xscale){
                    $new_width = round($width * (1/$yscale));
                    $new_height = round($height * (1/$yscale));
            }
            else {
                    $new_width = round($width * (1/$xscale));
                    $new_height = round($height * (1/$xscale));
            }
    }
    else
    {
            $new_width  = $width;
            $new_height = $height;
    }

    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
            if($extention == 'image/jpeg' || $extention == 'image/jpg') {
                    $imageTmp = imagecreatefromjpeg ($originalImage);
                    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagejpeg($imageResized,$path."thumb_".$filename,100);
            }
            else if($extention == 'image/png')
            {
                    $imageTmp = imagecreatefrompng ($originalImage);
                    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagepng($imageResized,$path."thumb_".$filename);
            }
            else if($extention == 'image/gif')
            {
                    $imageTmp = imagecreatefromgif ($originalImage);
                    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagegif($imageResized,$path."thumb_".$filename,100);
            }
    return $imageResized;
}

一切顺利。

答案 1 :(得分:0)

您遇到的主要问题是您多次覆盖$config变量(总是很好地为每个操作启动一个新变量)。而你也覆盖了水印/调整大小的版本:

if (!empty($_FILES['IMG']['tmp_name'])) {
            $upload["upload_path"] = "./uploads/";
            $upload["allowed_types"] = "gif|jpg|png|jpeg";
            $upload["max_size"] = "20444448";
            $upload["encrypt_name"] = true;
            $this->load->library("upload", $upload);
            if (!$this->upload->do_upload('IMG')) {
                $this->session->set_userdata('message', $this->upload->display_errors());
                redirect('somepage');
            }
            $photo = $this->upload->data();
            $row1['photo'] = $photo['file_name'];
            $this->ads_photos->create($row1);
            $this->load->library('image_lib');
            // Make 700x300 image {image_name)_thumb
            $config1['image_library'] = 'gd2';
            $config1['source_image'] = './uploads/' . $photo['file_name'];
            $config1['create_thumb'] = true;
            $config1['thumb_marker'] = '_thumb';
            //$config1['master_dim'] = 'auto'; // DEFAULT IS ALREADY AUTO
            $config1['quality'] = '70%';
            $config1['new_image'] = './uploads/cars-list/' . $photo['file_name'];
            $config1['width'] = '700';
            $config1['height'] = '300';
            $this->image_lib->initialize($config1);
            if (!$this->image_lib->resize()) {
                // NOTE: uncomment all unlink statements so if an error occurs the images
                // are deleted to prevent duplicates down the line or orphans
                //@unlink('./uploads/' . $photo['file_name']);
                $this->session->set_userdata('message', $this->image_lib->display_errors());
                redirect('somepage');
            }
            $this->image_lib->clear();
            // Make 400x250 watermarked image {image_name)_watermarked
            $config2['image_library'] = 'gd2';
            $config2['source_image'] = './uploads/cars-list/' . $photo['raw_name'] . $config1['thumb_marker'] . $photo['file_ext'];
            $config2['wm_type'] = 'overlay';
            // make sure watermark.png is much smaller than the 400x250 image
            // or it will go all bonkers (this may require some trial and error)
            $config2['wm_overlay_path'] = './uploads/watermark.png';
            $config2['wm_opacity'] = 60;
            $config2['wm_vrt_alignment'] = 'bottom';
            $config2['wm_hor_alignment'] = 'right';
            $config2['thumb_marker'] = '_watermarked'; // !important
            $config2['create_thumb'] = true;
            $config2['new_image'] = './uploads/cars-list/' . $photo['raw_name'] . $photo['file_ext'];
            $config2['width'] = '400';
            $config2['height'] = '250';
            $this->image_lib->clear();
            $this->image_lib->initialize($config2);
            $this->image_lib->resize();
            if (!$this->image_lib->resize() || !$this->image_lib->watermark()) {
                //@unlink('./uploads/' . $photo['file_name']);
                //@unlink('./uploads/cars-list/' . $photo['raw_name'] . $config1['thumb_marker'] . $photo['file_ext']);
                //@unlink('./uploads/cars-list/' . $photo['raw_name'] . $config2['thumb_marker'] . $photo['file_ext']);
                $this->session->set_userdata('message', $this->image_lib->display_errors());
                redirect('somepage');
            }
            // made it this far! success!
            // names/paths of files to be added to db
            $main_image = './uploads/' . $photo['file_name'];
            $thumb = './uploads/cars-list/' . $photo['raw_name'] . $config1['thumb_marker'] . $photo['file_ext'];
            $watermarked = './uploads/cars-list/' . $photo['raw_name'] . $config2['thumb_marker'] . $photo['file_ext'];
        }