CodeIgniter上传和调整大小问题
我花了几天时间尝试根据文档中的示例完成这项工作,但我遗漏了一些东西,或者我只是愚蠢!
我有一个 CMS 应用程序,用户可以上传图像以非常固定的布局显示。我们不想限制上传图像的文件大小,而是希望在图像到达后对其进行“处理”。
图像宽度需要为 615 像素,但某些直接从数码相机上传的图像为 2500X2000 甚至更大,因此这一点至关重要。
我将手册中的代码拼凑在一起,图像已成功上传到 CMS 应用程序内的文件夹中。但是,图像的大小并未调整。
如果我要重新调整大小,我的计划是向用户展示图像,以便使用 jCrop 进行裁剪(最终图像必须为 615X275,调整大小后可能需要裁剪高度),然后使用 codeigniter 进行 FTP使用原始名称将图像复制到其网站的便利设施文件夹中。
我将不胜感激在这件事上的任何帮助!
这是我的代码:
function do_feature_upload() { $imageName = $this->uri->segment(3); //echo $imageName; // Where the file is going to be placed $config['upload_path'] = "./uploads/".$_SESSION['dbPropNumber']; $config['allowed_types'] = 'jpg|jpeg'; $config['max_size'] = '0'; $config['file_name'] = $imageName.'.jpg'; $config['overwrite'] = 'TRUE'; $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $error['propertyDropdown'] = $_SESSION['propertyDropdown']; $error['username'] = $_SESSION['username']; $error['dbPropNumber'] = $_SESSION['dbPropNumber']; $error['propertyName'] = $this->content->getPropertyName($_SESSION['dbPropNumber']); $this->load->view('upload_AmenityImage', $error); } else { $image_data = $this->upload->data(); $origWidth = $image_data['image_width']; $origHeight = $image_data['image_height']; $newWidth = 615; $newHeight = $newWidth*$origHeight/$origWidth; $resize = array( 'image_library'=>'gd2', 'source_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'.jpg', 'new_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'1.jpg', 'create_thumb' => FALSE, 'maintain_ratio'=>FALSE, 'width'=>$newWidth, 'height'=>$newHeight ); $this->load->library('image_lib',$resize); $this->image_lib->resize(); $data = array('upload_data' => $this->upload->data()); $data['propertyDropdown'] = $_SESSION['propertyDropdown']; $data['username'] = $_SESSION['username']; $data['dbPropNumber'] = $_SESSION['dbPropNumber']; $data['propertyName'] = $this->content->getPropertyName($_SESSION['dbPropNumber']); //Present jCrop option after image is resized // FTP to final destination $this->load->view('upload_success', $data); } // end if } // end function
I have spent days trying to make this work based on the examples in the documentation but I am missing something or I am just STUPID!
I have a CMS application where users upload an image for display in a very fixed layout. We do not want to limit the file size of the uploaded image but would rather "process" it after it arrives.
The image needs to be 615px wide but some of the images uploaded directly from digital cameras are 2500X2000 and bigger so this is CRITICAL.
I pieced together the code from the manual and the image is successfully being uploaded to a folder within the CMS app. However, the image is NOT being resized.
If I ever get it to re-size, my plan is to present the image to the user for cropping using jCrop (the final image HAS to be 615X275 and it probably has to be cropped for height after resizing) and then use codeigniter to FTP the image to their site's amenities folder using the original name.
I will appreciate any help in this matter!
Here's my code:
function do_feature_upload() { $imageName = $this->uri->segment(3); //echo $imageName; // Where the file is going to be placed $config['upload_path'] = "./uploads/".$_SESSION['dbPropNumber']; $config['allowed_types'] = 'jpg|jpeg'; $config['max_size'] = '0'; $config['file_name'] = $imageName.'.jpg'; $config['overwrite'] = 'TRUE'; $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $error['propertyDropdown'] = $_SESSION['propertyDropdown']; $error['username'] = $_SESSION['username']; $error['dbPropNumber'] = $_SESSION['dbPropNumber']; $error['propertyName'] = $this->content->getPropertyName($_SESSION['dbPropNumber']); $this->load->view('upload_AmenityImage', $error); } else { $image_data = $this->upload->data(); $origWidth = $image_data['image_width']; $origHeight = $image_data['image_height']; $newWidth = 615; $newHeight = $newWidth*$origHeight/$origWidth; $resize = array( 'image_library'=>'gd2', 'source_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'.jpg', 'new_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'1.jpg', 'create_thumb' => FALSE, 'maintain_ratio'=>FALSE, 'width'=>$newWidth, 'height'=>$newHeight ); $this->load->library('image_lib',$resize); $this->image_lib->resize(); $data = array('upload_data' => $this->upload->data()); $data['propertyDropdown'] = $_SESSION['propertyDropdown']; $data['username'] = $_SESSION['username']; $data['dbPropNumber'] = $_SESSION['dbPropNumber']; $data['propertyName'] = $this->content->getPropertyName($_SESSION['dbPropNumber']); //Present jCrop option after image is resized // FTP to final destination $this->load->view('upload_success', $data); } // end if } // end function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不完全确定您的代码出了什么问题,但这是我编写的一个模型函数,用于调整图像大小以适应精确的目标高度和目标宽度。通读一遍,看看你是否无法找出解决方案。
$this->prefix
是我使用的类中的一个属性,因此我不必继续写出文件的目录。它看起来像这样:$this->prefix = FCPATH.'uploads'.DIRECTORY_SEPARATOR;
图像缩放器
I'm not entirely sure what's going wrong with your code, but here's a model function I wrote for resizing images to fit an exact target height and target width. Read through it and see if you can't figure out the solution.
$this->prefix
is a property in my class that I use so I don't have to keep writing out the directory of the file. It looks like this:$this->prefix = FCPATH.'uploads'.DIRECTORY_SEPARATOR;
Image resizer
几个月前我遇到了同样的问题,可悲的是,我无法弄清楚。我在此处和CodeIgniter 论坛 但没有人可以帮助我。
我最终使用了 timthumb 脚本,这是很棒,但远非理想:(
所以,如果你很着急,我强烈建议使用 timthumb。如果你有时间投资它,我祝你一切顺利,请分享!
I had the same problem a few months back and sadly, I wasn't able to figure it out. I've posted the same question here and in CodeIgniter's forums but no one could help me.
I ended up using timthumb script, which is great, but nowhere near ideal :(
So, if you're in a rush, I'd strongly recommend using timthumb. If you have some time to invest on it, I wish you the best and please, share!