Codeigniter图像处理输出黑色

时间:2014-04-08 05:26:28

标签: php codeigniter image-manipulation

我正在使用CI处理黑色区域,图像有950x720

第一次调整大小没问题     $ this->调整大小($ data [' upload_data'] [' full_path'],220,200,true,false); 但在第二个,出现错误

gist上的代码: https://gist.github.com/mateuspv/48ba464a557da9bbdf10

ci black error

<?php
/**
 * resize
 * @param  string $path   [description]
 * @param  int $width  [description]
 * @param  int $height [description]
 * @param  boolean $thumb  [description]
 * @param  boolean $ratio  [description]
 */
private function resize($path, $width, $height, $thumb, $ratio) {
        $config['image_library'] = 'GD2';
        $config['source_image'] = $path;
        $config['maintain_ratio'] = $ratio;
        $config['create_thumb'] = $thumb;
        $config['encrypt_name'] = TRUE;
        $config['width'] = $width;
        $config['height'] = $height;
        $this->image_lib->clear();
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        if (!$this->image_lib->resize()) {
            die($this->image_lib->display_errors());

        }
    }

//...

/**
 * TODO ~
 */
$data = array('upload_data' => $this->upload->data());
$this->resize($data['upload_data']['full_path'], 220, 200, true, false);
$this->resize($data['upload_data']['full_path'], 450, 450, false, false);

1 个答案:

答案 0 :(得分:1)

我认为当您第一次调用函数时脚本会修改图像本身并使其达到220px×200px。当你第二次调用函数时,它会拍摄小图像并将其调整为450像素×450像素。这可能是错误..尝试将第一个缩略图保存到新的目标文件。尝试以下代码。

<?php
/**
 * resize
 * @param  string $path   [description]
 * @param  int $width  [description]
 * @param  int $height [description]
 * @param  boolean $thumb  [description]
 * @param  boolean $ratio  [description]
 */
private function resize($path, $width, $height, $thumb, $ratio) {
    $config['image_library'] = 'GD2';
    $config['source_image'] = $path;
    $config['new_image'] = PATH_TO_NEW_IMAGE;
    $config['maintain_ratio'] = $ratio;
    $config['create_thumb'] = $thumb;
    $config['encrypt_name'] = TRUE;
    $config['width'] = $width;
    $config['height'] = $height;
    $this->image_lib->clear();
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    if (!$this->image_lib->resize()) {
        die($this->image_lib->display_errors());

    }
}

//...

/**
 * TODO ~
 */
$data = array('upload_data' => $this->upload->data());
$this->resize($data['upload_data']['full_path'], 220, 200, true, false);
$this->resize($data['upload_data']['full_path'], 450, 450, false, false);

将PATH_TO_NEW_IMAGE更改为目标文件路径。希望这会有所帮助。