CodeIgniter/PHP/GD2 图像操作正在播放

发布于 2024-09-16 17:11:12 字数 2422 浏览 6 评论 0原文

我有一个网站,它需要用户上传的图像,并制作三份副本 - 一份用于打印的“完整”副本(缩小至 1500x1125),一份用于在线显示的“网络”副本(尚未编码),最后是缩略图。

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

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

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

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

函数_resize

($type, $fullpath, $shortpath, $width, $height { // 设置默认的图像操作配置选项 $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)。

发生了什么:在我的上传文件夹中,只有两个图像 - 原始上传的文件 (image.jpg) 和调整大小的文件 (image_full.jpg)。从未创建过缩略图。

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

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

谢谢!

Jack

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

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

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.

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.

// 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)
{
// 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;

}

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).

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.

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.

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

Thanks!

Jack

Edit: 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.

Edit 2: I've also tried using $this->image_lib->clear() - still no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-09-23 17:11:12

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

$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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文