CodeIgniter 2 image_lib->resize() 错误 - 渲染空白页面并且不返回错误消息
我正在尝试做一些非常简单的事情,但我看不到让它发挥作用。无论如何,我目前正在我的 MAMP 开发机器上进行测试,但从临时服务器 (GoDaddy GridHost) 获得相同的结果。
这就是我想要做的:
- 上传用户从表单指定的图像。
- 创建上传图像的缩略图。
我的示例中有一些额外的数据库代码,但脚本在 if( ! $this->image_lib->resize()) 调用时出现“错误”。但是,它会导致整个应用程序退出,呈现空白页面。该代码实际上从未进入 if{} 块。
function _upload_image(&$location, &$image = NULL)
{
// Configure the initial full-sized image upload
$upload_config = array(
'upload_path' => "./uploads/users/{$this->viewer->id}/locations/{$location->id}/",
'allowed_types' => 'jpg|jpeg|gif|bmp|png',
'max_size' => 8192 // 8mb
);
$this->load->library('upload', $upload_config);
// Upload failed
if( ! $this->upload->do_upload('image'))
{
$this->errors = strip_tags($this->upload->display_errors());
return FALSE;
}
// Get the uploaded file's metadata
$data = $this->upload->data();
// Change permissions of the uploaded file so that the image library can copy and resize it
if(is_file($config['upload_path'] . $data['file_name']))
{
chmod($config['upload_path'] . $data['file_name'], 0777);
}
// If no existing image object was passed to this function, we are creating a brand new image
if(is_null($image))
{
$image = new Image();
$thumbnail = new Thumbnail();
}
else
{
$thumbnail = $image->thumbnail->get(); // Get the existing thumbnail
}
// Set the image object fields to save to the db
$image->name = $data['file_name'];
$image->mime_type = $data['file_type'];
$image->extension = $data['file_ext'];
$image->file_path = $data['file_path'];
$image->full_path = $data['full_path'];
$image->size = $data['file_size'];
$image->width = $data['image_width'];
$image->height = $data['image_height'];
// Failed to save the image to the db
if( ! $image->save())
{
$this->errors = array_merge($this->errors, array($image->error->string));
return FALSE;
}
// Failed to save the location/image relationship in the db
if( ! $location->save($image))
{
$this->errors = array_merge($this->errors, array($location->error->string));
return FALSE;
}
// Configure options for the thumbnail
$thumb_config = array(
'image_library' => 'GD2',
'source_image' => "./uploads/users/{$this->viewer->id}/locations/{$location->id}/{$image->name}",
'create_thumb' => TRUE,
'width' => 400,
'height' => 300
);
$this->load->library('image_lib');
$this->image_lib->initialize($thumb_config);
// Failed to create the image thumbnail
if( ! $this->image_lib->resize())
{
$this->errors = array_merge($this->errors, array($this->image_lib->display_errors()));
return FALSE;
}
// Set the thumbnail object fields to save to the db
$thumbnail->name = $data['raw_name'] . '_thumb' . $data['file_ext'];
$thumbnail->file_path = $image->file_path;
$thumbnail->full_path = $image->file_path . $thumbnail->name;
// Failed to save the thumbnail to the db
if( ! $thumbnail->save())
{
$this->errors = array_merge($this->errors, array($thumbnail->error->string));
return FALSE;
}
// Failed to save the image/thumbnail relationship in the db
if( ! $image->save($thumbnail))
{
$this->errors = array_merge($this->errors, array($image->error->string));
return FALSE;
}
// Everything worked
return TRUE;
}
数据库交互由 DataMapper ORM 处理,我将其包含在内是为了完整性,但我认为它不一定与此问题相关。该函数在调用时显然失败了:
if( ! $this->image_lib->resize())
I'm trying to do something very simple but I can't see to make it work. For what it's worth, I'm currently testing on my MAMP dev machine, but get the same results from staging server (GoDaddy GridHost).
Here's what I want to do:
- Upload an image specified by the user from a form.
- Create a thumbnail of the uploaded image.
There is some extra database code in my example, but the script "errors" at the if( ! $this->image_lib->resize()) call. However, it causes the entire application to exit, rendering a blank page. The code never actually enters the if{} block.
function _upload_image(&$location, &$image = NULL)
{
// Configure the initial full-sized image upload
$upload_config = array(
'upload_path' => "./uploads/users/{$this->viewer->id}/locations/{$location->id}/",
'allowed_types' => 'jpg|jpeg|gif|bmp|png',
'max_size' => 8192 // 8mb
);
$this->load->library('upload', $upload_config);
// Upload failed
if( ! $this->upload->do_upload('image'))
{
$this->errors = strip_tags($this->upload->display_errors());
return FALSE;
}
// Get the uploaded file's metadata
$data = $this->upload->data();
// Change permissions of the uploaded file so that the image library can copy and resize it
if(is_file($config['upload_path'] . $data['file_name']))
{
chmod($config['upload_path'] . $data['file_name'], 0777);
}
// If no existing image object was passed to this function, we are creating a brand new image
if(is_null($image))
{
$image = new Image();
$thumbnail = new Thumbnail();
}
else
{
$thumbnail = $image->thumbnail->get(); // Get the existing thumbnail
}
// Set the image object fields to save to the db
$image->name = $data['file_name'];
$image->mime_type = $data['file_type'];
$image->extension = $data['file_ext'];
$image->file_path = $data['file_path'];
$image->full_path = $data['full_path'];
$image->size = $data['file_size'];
$image->width = $data['image_width'];
$image->height = $data['image_height'];
// Failed to save the image to the db
if( ! $image->save())
{
$this->errors = array_merge($this->errors, array($image->error->string));
return FALSE;
}
// Failed to save the location/image relationship in the db
if( ! $location->save($image))
{
$this->errors = array_merge($this->errors, array($location->error->string));
return FALSE;
}
// Configure options for the thumbnail
$thumb_config = array(
'image_library' => 'GD2',
'source_image' => "./uploads/users/{$this->viewer->id}/locations/{$location->id}/{$image->name}",
'create_thumb' => TRUE,
'width' => 400,
'height' => 300
);
$this->load->library('image_lib');
$this->image_lib->initialize($thumb_config);
// Failed to create the image thumbnail
if( ! $this->image_lib->resize())
{
$this->errors = array_merge($this->errors, array($this->image_lib->display_errors()));
return FALSE;
}
// Set the thumbnail object fields to save to the db
$thumbnail->name = $data['raw_name'] . '_thumb' . $data['file_ext'];
$thumbnail->file_path = $image->file_path;
$thumbnail->full_path = $image->file_path . $thumbnail->name;
// Failed to save the thumbnail to the db
if( ! $thumbnail->save())
{
$this->errors = array_merge($this->errors, array($thumbnail->error->string));
return FALSE;
}
// Failed to save the image/thumbnail relationship in the db
if( ! $image->save($thumbnail))
{
$this->errors = array_merge($this->errors, array($image->error->string));
return FALSE;
}
// Everything worked
return TRUE;
}
The database interaction is handled by the DataMapper ORM, which I included for completeness, but I don't feel it is necessarily relevant to this issue. The function clearly fails at the call:
if( ! $this->image_lib->resize())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在index.php引导程序中设置了应用程序环境?如果您还没有将其设置为开发,则值得将其设置为开发,这可能会使错误可见,如果设置为测试/生产,这些错误将被隐藏。
如果这不能帮助诊断问题,请在 MAMP 中转到“服务器”选项卡,单击“PHP”,然后单击“查看日志”。这将打开 PHP 错误日志文件,该文件有望让您了解发生了什么。
Have you set your application environment in the index.php bootstrap? It's worth setting it to development if you haven't already, this may make errors visible that would be hidden if set to testing/production.
If that doesn't help diagnose the issue, in MAMP, go to the Server tab, click PHP and then View Log. This will open up the PHP error log file which will hopefully give you an idea of what's up.