Codeigniter图像处理类别:调整大小和裁剪多个文件 [英] Codeigniter Image Manipulation Class : Resize and Crop on multiple files

查看:92
本文介绍了Codeigniter图像处理类别:调整大小和裁剪多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使CodeIgniter图像处理正常工作。这是一个错误,或者我只是没有看到它。我希望有人可以帮助我。

I'm struggling to get the CodeIgniter Image Manipulation working correctly. Either it's a bug or I'm just not seeing it. I hope someone can help me with it. Thanks in advance!

在脚本上:我想创建缩略图(176w x 132h)。输入图像的大小和比例不同。为了始终获得此尺寸,我首先将其尺寸调整为适合最大宽度或高度(取决于图像方向),然后在中间进行裁切。
我尝试用一​​种方法完成所有操作。那没有用,所以我创建了两个单独的方法。

On the script: I want to create thumbnails (176w x 132h). The input images are in different sizes and ratios. In order to always get this size I first resize them to fit the max width or height (depending on image orientation) and then crop in the center. I've tried to do it all in 1 method. That didn't work, so I created two seperate methods.

resize_img() 

crop_img().

当我在3个不同的文件上运行resize_img()时,它可以工作。如果之后我在这些缩略图上使用crop_img(),则创建的第一个方法将起作用。如果我将两者结合在一起,或者彼此接连使用,那就不会了。
我尝试过$ this-> image_lib-> clear();,取消配置文件。为了确保,我什至创建了两个配置文件。

When I run resize_img() on 3 different files, it works. If after that I use crop_img() on these thumbnails the 1th method created, it works. If I combine the two, or use them after one another, it doesn't. I've tried $this->image_lib->clear();, unsetting the config files. I even created two config files, just to be sure.

我遇到了与GD2不同的各种错误,但是问题是,在resize_img()创建之后缩略图,crop_img()不会对其进行裁剪。此后一切都向南,无法打开下一张图像。在文件夹和文件上都会检查写权限。

I'm getting different all kind of errors from GD2, but the problem is, that after resize_img() creates the thumbnail, the crop_img() won't crop it. After that it all goes south, and the next images can't be opened. Write premissions are checked, both on folder and files.


无法保存图像。请确保映像和文件
目录可写。图像的路径不正确。您的
服务器不支持处理此类
图像所需的GD功能。

Unable to save the image. Please make sure the image and file directory are writable. The path to the image is not correct. Your server does not support the GD function required to process this type of image.

完整代码:

<?PHP

class Imagetest extends MY_Controller {

function __construct()
{
    parent::__construct();
    $this->load->library('image_lib');
}

function index()
{
    $testimg1 = 'uploads/test/1.png';
    $testimg2 = 'uploads/test/2.png';
    $testimg3 = 'uploads/test/3.png';

    $this->resize_img($testimg1);
    $this->crop_img($testimg1);

    $this->resize_img($testimg2);
    $this->crop_img($testimg2);

    $this->resize_img($testimg3);
    $this->crop_img($testimg3);
}


function image_thumb_name($img = null)
{
    if(!empty($img)) {
        $exploded = explode('.', $img);
        return $exploded['0'] . '_thumb.' . $exploded['1'];
    }
}

function get_axis($img)
{
    // Default values
    $output['x_axis'] = 0;
    $output['y_axis'] = 0;

    // Settings
    $config['height'] = 132;
    $config['width']  = 176;

    if ($img_dim = getimagesize($img)) {
        list($thumb_width, $thumb_height) = $img_dim;
    } else {
        echo '<h1> ERROR HERE TOO</h1>';
        return false;
    }

    if ($thumb_width > $config['width']) {

        $output['x_axis'] = (($thumb_width - $config['width']) / 2) ;               

    } else if ($thumb_height > $config['height']) {

        $output['y_axis'] = (($thumb_height - $config['height']) / 2);
    }       
    return $output;
} 

function resize_img($img) 
{
    $config = array();
    echo 'Processing: '. $img .'<br/>';  // Debug

    if ($img_dim = getimagesize($img)) {
        list($image_width, $image_height) = $img_dim;
    } else {
        echo '<h1> ERROR HERE </h1>';
    }

    // create a thumbnail
    $config['image_library'] = 'GD2';
    $config['source_image'] = $img;
    $config['quality'] = 100;
    $config['height'] = 132;
    $config['width'] = 176;
    $config['create_thumb']  = TRUE;
    $config['maintain_ratio']= TRUE;
    $config['master_dim'] = ($image_width > $image_height) ? 'height' : 'width';
    $this->image_lib->initialize($config);

    if (!$this->image_lib->resize()) { 
        echo $this->image_lib->display_errors();
    }

    echo '<img src="../'.$this->image_thumb_name($img).'" /> <br/>'; // Debug

    $this->image_lib->clear();
    unset($config);
}   




function crop_img($img)
{
    $config2 = array();

    // Crop that thumbnail
    $config2['image_library'] = 'GD2';
    $config2['quality'] = 100;
    $config2['height'] = 132;
    $config2['width'] = 176;
    $config2['source_image']  = $this->image_thumb_name($img);
    $axis = $this->get_axis($config2['source_image']);
    $config2['x_axis'] = $axis['x_axis'];
    $config2['y_axis'] = $axis['y_axis'];
    $config2['maintain_ratio'] = FALSE;
    $config2['create_thumb'] = FALSE;
    $this->image_lib->initialize($config2);

    if (!$this->image_lib->crop()) { 
        echo $this->image_lib->display_errors(); 
    }

    echo '<img src="../'.$config2['source_image'].'" /> <br/>'; // Debug

    $this->image_lib->clear();
    unset($config2);
}

}


推荐答案

知道了!
我已将create_thumb选项设置为FALSE,并在resize_img方法中使用了new_image参数。效果是一样的,但是没有使用内置的create_tumb函数。
这是一个恕我直言的错误,但现在可以正常使用:)

Got it! I've set the create_thumb option to FALSE, and used the new_image parameter in the resize_img method. The effect is the same, but the built-in create_tumb function is not being used. It's a bug IMHO, but it's working now :)

$config['create_thumb'] = FALSE;
$config['new_image'] = $this->image_thumb_name($img);

这篇关于Codeigniter图像处理类别:调整大小和裁剪多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆