使用 CodeIgniter 动态调整图像大小

发布于 2024-10-28 01:48:02 字数 1752 浏览 0 评论 0原文

我正在编写一个从 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 技术交流群。

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

发布评论

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

评论(7

彼岸花似海 2024-11-04 01:48:02

不要多次加载 image_lib。在自动加载库中添加 image_lib 并更改

$this->load->library('image_lib', $config);

$this->image_lib->initialize($config);

Dont load image_lib multiple times. Add image_lib in autoload libs and change

$this->load->library('image_lib', $config);

to

$this->image_lib->initialize($config);
云之铃。 2024-11-04 01:48:02

我正在使用 ImageMagick 通过 CI 调整图像大小,就像您一样。您需要执行以下操作才能使其正常工作:

  • 应安装 imagemagick。您可以使用“convert”命令从命令行进行测试
  • imagick 需要安装,这是绑定到 imagemagick 的 PHP 库
  • ImageMagick 本身依赖于各种其他库,例如 libjpeg 和 libpng。确保这些都已安装,

Simply 执行 phpinfo() 并向下滚动到“imagick”。检查它是否存在,然后检查“支持的文件格式”标题,看看是否存在您要调整大小的文件类型。

如果上述所有内容都正确但仍然不起作用,您不应该忘记在代码中包含 imagemagick 的路径:

$config['library_path'] = '/usr/local/bin';

我之前经历过所有这些痛苦,所以我希望这对您有帮助:)

I'm doing image resizing with CI using ImageMagick just like you are. You need the following to get this to work:

  • imagemagick should be installed. You can test it from the command line using the 'convert' command
  • imagick needs to be installed, this is the PHP library that binds to imagemagick
  • ImageMagick itself depends on various other libraries such as libjpeg and libpng. Make sure those are installed as well

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:

$config['library_path'] = '/usr/local/bin';

I went through all this pain before so I hope this helps you :)

动听の歌 2024-11-04 01:48:02

对于多次调整大小,下面的代码对我有用。

    $config['create_thumb'] = FALSE; //to avoid _thumb prefixing
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 250;
    $config['height'] = 250;
    $config['new_image'] = 'thumb_250x250_'.$file_name; // new name
    $CI->load->library('image_lib', $config, 'abc'); //abc to avoid instance caching.
    $CI->abc->resize();
    unset($CI->abc); //unsetting instance.
    $config['width'] = 100;
    $config['height'] = 100;
    $config['new_image'] = 'thumb_100x100_'.$file_name; // new name
    $CI->load->library('image_lib', $config, 'xyz'); // xyz to avoid instance caching.
    $CI->xyz->resize();
    unset($CI->xyz); // unsetting instance.

For Multiple resize below code worked for me.

    $config['create_thumb'] = FALSE; //to avoid _thumb prefixing
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 250;
    $config['height'] = 250;
    $config['new_image'] = 'thumb_250x250_'.$file_name; // new name
    $CI->load->library('image_lib', $config, 'abc'); //abc to avoid instance caching.
    $CI->abc->resize();
    unset($CI->abc); //unsetting instance.
    $config['width'] = 100;
    $config['height'] = 100;
    $config['new_image'] = 'thumb_100x100_'.$file_name; // new name
    $CI->load->library('image_lib', $config, 'xyz'); // xyz to avoid instance caching.
    $CI->xyz->resize();
    unset($CI->xyz); // unsetting instance.
情何以堪。 2024-11-04 01:48:02

您需要在 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 your resize_photo function.

Read http://codeigniter.com/user_guide/libraries/image_lib.html

南汐寒笙箫 2024-11-04 01:48:02

实际上你正在尝试加载图像库两次。由于您还在同一行上初始化配置数组,因此该数组永远不会加载到库中。

将您的代码更改为:

    //this
    $this->load->library('image_lib', $config);


    //to this
    $this->load->library('image_lib');
    $this->image_lib->initialize($config);

它将完美地工作。

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:

    //this
    $this->load->library('image_lib', $config);


    //to this
    $this->load->library('image_lib');
    $this->image_lib->initialize($config);

and it will work perfectly.

疑心病 2024-11-04 01:48:02

在尝试调整图像大小之前,先看看是否可以实际打开原始保存的图像。我在使用 preg_replace 时解码了 Base64 上传的图像。由于某种原因,我仍然无法追踪......它正在删除,所以

$file = preg_replace('/data.*base64,/', '', chunk_split($this->post('myimg'));

它会返回:[removed]/9......等等。当 Base64 解码时...显然不是有效的图像文件..所以调整大小不起作用。我必须添加一个

$file = substr($file,9);

然后删除[已删除]。额外的工作,花了我一段时间才弄清楚,但现在我可以调整图像大小。

附带问题...为什么 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

$file = preg_replace('/data.*base64,/', '', chunk_split($this->post('myimg'));

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

$file = substr($file,9);

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.

人事已非 2024-11-04 01:48:02

//将控制器命名为“image.php”

class Image extends CI_Controller {

 public function index($width, $height, $image_path)
    {   
        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/'.$image_path;
        $config['dynamic_output'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = $width;
        $config['height'] = $height;

        $this->load->library('image_lib', $config); 
        $this->image_lib->initialize($config);
        echo $this->image_lib->resize();
    }
}

?>
//Call from view page
<img src="<?php echo ("index.php/image/index/150/150/".$luserdata[0]'profile_image']);?>"      alt="resized mage1"/>

//Make controller named "image.php"

class Image extends CI_Controller {

 public function index($width, $height, $image_path)
    {   
        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/'.$image_path;
        $config['dynamic_output'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = $width;
        $config['height'] = $height;

        $this->load->library('image_lib', $config); 
        $this->image_lib->initialize($config);
        echo $this->image_lib->resize();
    }
}

?>
//Call from view page
<img src="<?php echo ("index.php/image/index/150/150/".$luserdata[0]'profile_image']);?>"      alt="resized mage1"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文