Codeigniter 图像操作类:调整多个文件的大小和裁剪

发布于 2024-12-28 19:49:41 字数 3803 浏览 3 评论 0原文

我正在努力让 CodeIgniter 图像操作正常工作。要么它是一个错误,要么我只是没有看到它。我希望有人能帮助我。提前致谢!

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

resize_img() 

crop_img().

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

我从 GD2 中得到了各种不同的错误,但问题是,在 resize_img() 创建缩略图后,crop_img() 不会裁剪它。之后就一切都向南了,接下来的图像就无法打开了。检查文件夹和文件的写入权限。

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

完整代码:

<?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);
}

}

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!

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() 

and

crop_img().

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.

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.

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.

Full code:

<?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);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

难以启齿的温柔 2025-01-04 19:49:41

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

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

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);
野鹿林 2025-01-04 19:49:41

问题是你的 $config['source_image']

var_dump($config['source_image']);

看看你得到了什么。

如果文件位于根文件夹的 images 文件夹中,则 $config['source_image] = 'images/'.$img。

还要确保所有爆炸函数返回正确的文件名。 (返回并回显以检查)。

1、消除文件未找到错误。之后,看看会出现什么错误。

The problem is with your $config['source_image']

var_dump($config['source_image']);

see what you get.

If the file is in images folder in root folder, the $config['source_image] = 'images/'.$img.

Also make sure all the exploding functions returning the correct file name. (return and echo to check).

1st, eliminate file not found error. After that, see what error comes.

樱桃奶球 2025-01-04 19:49:41

您可以使用 Kodem 的图像调整大小服务。您只需一个 http 调用即可调整大小、裁剪任何图像。可以在浏览器中随意使用,也可以在您的生产应用程序中使用。

You can use Kodem's Image Resize Service. You can resize, crop any image with just a http call. Can be used casually in the browser or used in your production app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文