CodeIgniter / PHP / GD2 Image Manipulation正在播放

时间:2010-08-26 10:50:14

标签: php codeigniter image-processing gd2

我有一个网站,用户上传的图像,并制作三个副本 - 一个'完整'副本打印(缩小到1500x1125),一个'网页'副本在线显示(尚未编码),最后缩略图。

所以这里是代码 - _imageformat()从CI的上传类传递参数(我已经证实是正确的):

function _imageformat($fullpath, $shortpath, $width, $height)

{   //我们现在格式化图像。

//首先,我们检查它是横向还是纵向   if($ width> = $ height)//它的风景(或方形)   {    //现在创建完整的打印图像    $ fullimage = $ this-> _resize('l',$ fullpath,$ shortpath,$ width,$ height);   }   否则//这是肖像   {    //现在创建完整的打印图像    $ fullimage = $ this-> _resize('p',$ fullpath,$ shortpath,$ width,$ height);   }

}

function _resize($ type,$ fullpath,$ shortpath,$ width,$ height)    {     //设置默认的Image Manipulation配置选项     $ config ['image_library'] ='gd2';     $ config ['source_image'] = $ fullpath;     $ config ['maintain_ratio'] = TRUE;

// Shave the '.jpg' from the end to append some nice suffixes we'll use
$newimage = substr($fullpath, 0, -4).'_full'.".jpg";

$config['new_image'] = $newimage;

if ($type == 'l') // If it's landscape
{
 $config['width'] = 1500;
 $config['height'] = 1125;
}
else if ($type == 'p') // If it's portrait
{
 $config['width'] = 1125;
 $config['height'] = 1500;   
}

// Load the image library with the specified parameters, and resize the image!  
$this->load->library('image_lib', $config); 
$this->image_lib->resize();

// Create a thumbnail from the full image
$config['source_image']  = $newimage;
$config['new_image']  = substr($fullpath, 0, -9)."_thumb".".jpg";
$config['maintain_ratio']  = TRUE;
$config['width']    = 150;
$config['height']   = 150;

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

$this->image_lib->resize();

return $newimage;

}

应该发生什么:在我的上传文件夹中,有三个图片 - 原始上传文件(我们称之为image.jpg),调整大小的文件(名为image_full.jpg),以及缩略图(名为image_thumb.jpg)。

会发生什么:在我的上传文件夹中,只有 TWO 图片 - 原始上传文件(image.jpg)和调整大小的文件(image_full.jpg )。没有创建缩略图。

然而,有趣的是,**如果我为缩略图创建首先放置代码,它会生成缩略图,但** _full(调整大小) ) 图片。

所以在我看来它不会运行$this->image_lib->resize()两次。为什么不?这是我正在做的一些业余错误,还是我错过了一些明显的东西?! :P

谢谢!

杰克

编辑:我应该指出是的,我知道我正在加载image_lib库两次。我发现这是将新参数传递给它的唯一方法。在调整完整图像大小后,我还尝试调用$this->_thumbnail()再次加载库。但仍然出现同样的问题。

编辑2:我也尝试使用$this->image_lib->clear() - 仍然没有运气。

1 个答案:

答案 0 :(得分:2)

您应该只加载一次库并使用不同的配置对其进行初始化:

$this->load->library('image_lib');

// full image stuff
$this->image_lib->initialize($config);
$this->image_lib->resize();

// thumbnail stuff
$this->image_lib->initialize($config);
$this->image_lib->resize();