问题描述
我有一个网站需要用户上传的图片,并制作三个副本 - 完整副本打印(缩小到1500x1125),网络副本在线显示(尚未编码),最后
I have a website going that takes a user's uploaded image, and makes three copies - a 'full' copy to print with (downsized to 1500x1125), a 'web' copy to display online (not coded yet), and finally a thumbnail.
这里是代码 - _imageformat()传递来自CI的上传类的参数(我已经确认是正确的):
So here's the code - _imageformat() is passed the parameters (which I've confirmed to be correct) from CI's Upload Class:
function _imageformat($fullpath, $shortpath, $width, $height)
{
//我们现在格式化图片。
{ // We now format the image.
//首先,风景或肖像
if($ width> = $ height)//它是风景(或正方形)
{
//现在创建完整的打印图像
$ fullimage = $ this - > _ resize('l',$ fullpath,$ shortpath,$ width,$ height);
}
else //它的肖像
{
//现在创建完整的打印图像
$ fullimage = $ this - > _ resize('p',$ fullpath ,$ shortpath,$ width,$ height);
}
// First, we check if it is landscape or portrait if ($width >= $height) // It's landscape (or square) { // Now create the full printing image $fullimage = $this->_resize('l', $fullpath, $shortpath, $width, $height); } else // It's portrait { // Now create the full printing image $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;
function _resize($type, $fullpath, $shortpath, $width, $height) { // Set up the default Image Manipulation config options $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)
What SHOULD happen: In my uploads folder, there are three images - the original uploaded file (we'll call it image.jpg), the resized file (named image_full.jpg), and the thumbnail (named image_thumb.jpg).
发生什么:在我的上传文件夹中,只有两个图片 - 原始上传的文件image.jpg)和调整大小的文件(image_full.jpg)。
What DOES happen: In my uploads folder, there are only TWO images - the original uploaded file (image.jpg), and the resized file (image_full.jpg). No thumbnail is ever created.
有趣的是,**如果我先放置缩略图创建的代码 ,它会生成缩略图图像,但** NOT 的_full(调整大小)图像。
What's interesting, however, ** is that if I place the code for the Thumbnail creation first, it generates the thumbnail image but **NOT the _full (resized) image.
所以我觉得它不会运行 $ this-> image_lib-> resize()
两次。为什么不?是我做的一些业余的错误,还是我错过了明显的东西? :P
So it appears to me that it won't ever run $this->image_lib->resize()
twice. Why not? Is it some amateur mistake I'm making, or have I missed something obvious?! :P
谢谢!
杰克
修改:我应该指出是的,我知道我要加载 image_lib
库两次。我怀疑这是向它传递新参数的唯一方法。我也试过,调整完整的形象后,调用 $ this-> _thumbnail()
,再次加载库。但仍然发生同样的问题。
I should point out that yes, I know I'm loading the image_lib
library twice. I fathomed this was the only way of passing new parameters to it. I also tried, after resizing the full image, calling $this->_thumbnail()
which loaded the library again there. But still the same issue occurred.
编辑2:我也尝试使用 $ this-> image_lib - > clear()
- 仍然没有运气。
Edit 2: I've also tried using $this->image_lib->clear()
- still no luck.
推荐答案
并用不同的配置初始化它:
You should load the library only once and initialize it with different configs:
$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();
这篇关于CodeIgniter / PHP / GD2图像处理正在播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!