使用 CodeIgniter 动态调整图像大小
我正在编写一个从 Dropbox 下载文件的脚本,应该调整该图像的大小,然后将其发送到 S3 存储桶。
由于某种原因,我无法调整图像大小。
我不断收到以下错误:
图像的路径不正确。 您的服务器不支持处理此类图像所需的 GD 功能。
代码库:
public function resize_test() {
$postcard_assets = $this->conn->getPostcardDirContent("folder_name", "Photos", TRUE);
foreach($postcard_assets['contents'] as $asset) {
$file = pathinfo($asset['path']);
$original_file = $this->conn->downloadFile($asset['path']);
$raw_file = sha1($file['basename']);
$s3_file_name = "1_{$raw_file}.{$file['extension']}";
$this->resize_photo($original_file);
$this->s3->putObject($s3_file_name, $original_file, 's3-bucket-name', 'public-read');
$s3_check = $this->s3->getObjectInfo($s3_file_name, 's3-bucket-name');
if($s3_check['content-length'] > 0) {
krumo($s3_check);
exit();
}
}
}
private function resize_photo($photo) {
$config['image_library'] = 'imagemagick';
$config['source_image'] = $photo;
$config['maintain_ratio'] = TRUE;
$config['width'] = 640;
$config['height'] = 480;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) {
exit($this->image_lib->display_errors());
}
}
Dropbox API 下载文件:
public function downloadFile($file) {
$this->setTokens();
return $this->conn->getFile($file);
}
有人知道我可能做错了什么吗?
I am working on a script that downloads a file from Dropbox, supposed to resize that image and then shoot it up to an S3 bucket.
For some reason, I can't get the image to resize.
I keep getting the following error:
The path to the image is not correct.
Your server does not support the GD function required to process this type of image.
Code Base:
public function resize_test() {
$postcard_assets = $this->conn->getPostcardDirContent("folder_name", "Photos", TRUE);
foreach($postcard_assets['contents'] as $asset) {
$file = pathinfo($asset['path']);
$original_file = $this->conn->downloadFile($asset['path']);
$raw_file = sha1($file['basename']);
$s3_file_name = "1_{$raw_file}.{$file['extension']}";
$this->resize_photo($original_file);
$this->s3->putObject($s3_file_name, $original_file, 's3-bucket-name', 'public-read');
$s3_check = $this->s3->getObjectInfo($s3_file_name, 's3-bucket-name');
if($s3_check['content-length'] > 0) {
krumo($s3_check);
exit();
}
}
}
private function resize_photo($photo) {
$config['image_library'] = 'imagemagick';
$config['source_image'] = $photo;
$config['maintain_ratio'] = TRUE;
$config['width'] = 640;
$config['height'] = 480;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) {
exit($this->image_lib->display_errors());
}
}
Dropbox API DownloadFile:
public function downloadFile($file) {
$this->setTokens();
return $this->conn->getFile($file);
}
Anyone know what I might be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不要多次加载 image_lib。在自动加载库中添加 image_lib 并更改
为
Dont load image_lib multiple times. Add image_lib in autoload libs and change
to
我正在使用 ImageMagick 通过 CI 调整图像大小,就像您一样。您需要执行以下操作才能使其正常工作:
Simply 执行 phpinfo() 并向下滚动到“imagick”。检查它是否存在,然后检查“支持的文件格式”标题,看看是否存在您要调整大小的文件类型。
如果上述所有内容都正确但仍然不起作用,您不应该忘记在代码中包含 imagemagick 的路径:
我之前经历过所有这些痛苦,所以我希望这对您有帮助:)
I'm doing image resizing with CI using ImageMagick just like you are. You need the following to get this to work:
Simpy do a phpinfo() and scroll down to 'imagick'. Check whether it is there and then check the 'supported file formats' heading to see if the file type you are wanting to resize is there.
If all of the above are correct and it still does not work, you should not forget to include the path to imagemagick in your code:
I went through all this pain before so I hope this helps you :)
对于多次调整大小,下面的代码对我有用。
For Multiple resize below code worked for me.
您需要在
resize_photo
函数中使用$config['new_image'] = '/path/to/new_image.jpg';
。阅读http://codeigniter.com/user_guide/libraries/image_lib.html
You need to use
$config['new_image'] = '/path/to/new_image.jpg';
in yourresize_photo
function.Read http://codeigniter.com/user_guide/libraries/image_lib.html
实际上你正在尝试加载图像库两次。由于您还在同一行上初始化配置数组,因此该数组永远不会加载到库中。
将您的代码更改为:
它将完美地工作。
Actually you are trying to load the image library twice. Since you also initialize the config array on the very same line, the array never gets loaded into the library.
Change your code to this:
and it will work perfectly.
在尝试调整图像大小之前,先看看是否可以实际打开原始保存的图像。我在使用 preg_replace 时解码了 Base64 上传的图像。由于某种原因,我仍然无法追踪......它正在删除,所以
它会返回:[removed]/9......等等。当 Base64 解码时...显然不是有效的图像文件..所以调整大小不起作用。我必须添加一个
然后删除[已删除]。额外的工作,花了我一段时间才弄清楚,但现在我可以调整图像大小。
附带问题...为什么 preg_replace 添加 [removed]???叹息...php。
See if you can actually open the original saved image before you try to resize it. I was decoding a base64 uploaded image while using a preg_replace. For some reason, which I still can't track down... it was removing like so
it would return this: [removed]/9....etc. which when base64 decoded... obviously isnt a valid image file.. so the resize wouldnt work. I had to add a
to then remove the [removed]. extra work and took me while to figure out, but now I can resize images.
Side Question... Why is preg_replace adding [removed]??? Sigh... php.
//将控制器命名为“image.php”
//Make controller named "image.php"