CodeIgniter图像调整大小

4
我在使用CodeIgniter进行图像调整时遇到了问题。我将文件作为zip文件上传,然后解压缩,解压缩后我扫描目录以查找.jpg文件。如果是.jpg扩展名,则需要调整大小。当zip文件仅有一个.jpg图像时,它可以工作,但是当zip文件中有2个或更多的.jpg图像文件时,它无法正常工作。
它确实扫描所有jpg文件,但问题是它没有进行调整大小。
我希望在上传zip文件时可以调整所有jpg文件的大小。
以下是我的代码:
$images = scandir('uploads/new');
//print_r($images);
foreach($images as $image){
    $last = substr($image, -3);
    if($last == 'jpg'){
        $image_path = './uploads/new/'.$image;

        //$config['image_library'] = 'gd2';
        $config['source_image'] = $image_path;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 100;
        $config['height'] = 100;

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

        $this->image_lib->resize(); 
    }
}       
2个回答

2

您应该使用$this->image_lib->clear();

clear函数会重置处理图像时使用的所有值。如果您正在循环中处理图像,则需要调用此函数。

$this->image_lib->clear();

来自:http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

还有一个提示:

不需要使用字符串函数来获取文件扩展名。您可以使用实际上为您想要的东西设计的pathinfo()函数

$ext = pathinfo($image, PATHINFO_EXTENSION);

在您的情况下:
if(pathinfo($image, PATHINFO_EXTENSION) == 'jpg'){

这样,如果您添加的扩展名超过3个字母,它也会正常工作;)

谢谢,现在它可以工作了。还要感谢您关于pathinfo函数的额外建议。干杯! - Myke Solidum

0

尝试在foreach循环中清除您的配置

$images = scandir('uploads/new');
//print_r($images);
foreach($images as $image){
    $this->image_lib->clear(); // clear previous config
    $last = substr($image, -3);
    if($last == 'jpg'){
        $image_path = './uploads/new/'.$image;

        //$config['image_library'] = 'gd2';
        $config['source_image'] = $image_path;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 100;
        $config['height'] = 100;

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

        $this->image_lib->resize(); 
    }
}       

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接