gpt4 book ai didi

CodeIgniter:图像处理类不能工作两次

转载 作者:行者123 更新时间:2023-12-04 03:17:24 34 4
gpt4 key购买 nike

我在尝试调整图像大小然后创建拇指时遇到问题。

两个函数都单独工作,但是当试图同时调用它们时,只有第一个调用有效。没有创建拇指,为什么?

这是我的 Controller :

if($this->upload->do_upload()){

// Si oui, tout va bien

//Update DB
$pictureData = $this->upload->data();
if($pictureData['file_name']!=''){
//On resize l'immage
//On charge la librarie
$this->load->library('thumbs');

$this->thumbs->resize($pictureData['full_path'], 300, 600);

$News['image']=$pictureData['file_name'];

//On crée un Thumbnail:
// on envoi le full path
if($this->thumbs->create($pictureData['full_path']))

// on stock le path recu
$News['thumb'] = $pictureData['raw_name'].'_thumb'.$pictureData['file_ext'];
}
}

这是我为这两个函数创建的库:
   <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Thumbs
{

function __construct()
{
$CI =& get_instance();
$this->load->library('image_lib');
}

public function create($picPath) {

$CI =& get_instance();

$config['image_library'] = 'gd2';
$config['source_image'] = $picPath;
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;

$CI->image_lib->clear();
$CI->image_lib->initialize($config);

if(!$CI->image_lib->resize()){
$CI->session->set_flashdata('flashError', $CI->image_lib->display_errors());
return False;
}
$CI->image_lib->clear();
Return True;

}

public function resize($picPath, $x, $y) {

$CI =& get_instance();

$config1['image_library'] = 'gd2';
$config1['source_image'] = $picPath;
$config1['maintain_ratio'] = TRUE;

if($x!=0)
$config1['width'] = $x;

if($y!=0)
$config1['height'] = $y;

$CI->image_lib->clear();
$CI->image_lib->initialize($config1);

if(!$CI->image_lib->resize()){
$CI->session->set_flashdata('flashError', $CI->image_lib->display_errors());
return False;
}
$CI->image_lib->clear();
Return True;
}
}

所以如果我只调用一个,它工作正常,无论是创建还是调整大小功能。否则,如果我尝试同时调用它们,则只有被调用的第一个函数有效。

编辑:感谢 fccotech,我做到了。这是他带来的解决方案:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Thumbs
{

function __construct()
{
$this->CI =& get_instance();
$this->CI->load->library('image_lib');
}

public function create($picPath, $picName) {

$config =array();

$config['image_library'] = 'gd2';
$config['source_image'] = $picPath;
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = $picName;

// $CI->image_lib->clear();
$this->CI->image_lib->initialize($config);

if(!$this->CI->image_lib->resize()){
$this->CI->session->set_flashdata('flashError', $this->CI->image_lib->display_errors());
return False;
}
$this->CI->image_lib->clear();
Return True;

}

public function resize($picPath, $x, $y) {

$config1 = Array();

$config1['image_library'] = 'gd2';
$config1['source_image'] = $picPath;
$config1['maintain_ratio'] = TRUE;

if($x!=0)
$config1['width'] = $x;

if($y!=0)
$config1['height'] = $y;

// $CI->image_lib->clear();
$this->CI->image_lib->initialize($config1);

if(!$this->CI->image_lib->resize()){
$this->CI->session->set_flashdata('flashError', $this->CI->image_lib->display_errors());
$this->CI->image_lib->clear();
return False;
}
$this->CI->image_lib->clear();
Return True;
}
}

最佳答案

调整大小后添加

$this->image_lib->clear();

所以在你做一个新的 $CI->image_lib->initialize($config); 之前也要清楚;

关于CodeIgniter:图像处理类不能工作两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13072891/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com