【问题标题】:how to crop and receive two images after the original image has been uploaded using codeigniter使用codeigniter上传原始图像后如何裁剪和接收两张图像
【发布时间】:2014-03-30 01:33:15
【问题描述】:

我创建了一个库,它只上传 1 张图片,并根据路径、宽度和高度对其进行裁剪。但我不知道如何让它裁剪尽可能多的图像并返回它创建的所有文件名的数组。这是我的图书馆,如果有好的方法,请告诉我。谢谢

<?php
/*
    Usage:
    -----------------------------------------------------------------------
    $this->load->library('up_image');
    $this->logo = $this->up_image->upload_image(upload_path, width, height);
*/
class up_image
{
    /**
    *   Upload Image Method
    *   ----------------------
    *   @Param: Upload Path
    *   @Param: Width: 
    *   @Param: Height: 
    **/
    public function upload_image($path, $width, $height)
    {
        //load codeigniter instant to load stuff
        $CI =& get_instance();
        $config1 = array(
            'allowed_types' => 'jpg|jpeg|png',
            'upload_path' => $path,
            'max_size' => 4000,
            'remove_spaces' => TRUE,
            'encrypt_name' => TRUE
        );


        $CI->load->library('upload', $config1);
        $CI->upload->display_errors('<p class="errors">', '</p>');

        //if upload do something
        if($CI->upload->do_upload())
        {
            $image_data = $CI->upload->data();

            $config2 = array(
                'image_library' => 'gd2',
                'source_image' => $image_data['full_path'],
                'new_image' => $path . '/' . $image_data['file_name'],
                'maintain_ratio' => TRUE,
                'width' => $width,
                'height' => $height
            );


            $CI->load->library('image_lib', $config2);
            $CI->image_lib->resize();
            return $image_data['file_name'];
        }
        else
        {
            return FALSE;
        }
    }
}

更新:我想通了,这是课程以及如何为任何尝试做某事的人使用它。这个类的作用是上传图片,并让您创建任意大小的原始上传文件。

[类]

<?php
/*
Author: Sarmen B
URL: sarmenb.com

*/
class up_image
{
    var $path = '';

    /**
    *   Upload Image
    *   ----------------------
    *   @Param: Upload Path
    *   @Param: Width
    *   @Param: Height
    **/
    public function upload_image()
    {
        //load codeigniter instant to load stuff
        $CI =& get_instance();
        $config = array(
            'allowed_types' => 'jpg|jpeg|png',
            'upload_path' => $this->path,
            'max_size' => 4000,
            'remove_spaces' => TRUE,
            'encrypt_name' => TRUE
        );


        $CI->load->library('upload', $config);
        $CI->upload->display_errors('<p class="errors">', '</p>');

        //if upload do something
        if($CI->upload->do_upload())
        {
            //returns the images data as an array
            return $CI->upload->data();
        }
        else
        {
            return FALSE;
        }
    }


    /**
    *   Resize the image
    *   ----------------------
    *   @Param: $data data returned from upload_image()
    *   @Param: Width
    *   @Param: Height
    **/
    public function resize($data, $width, $height)
    {
        $CI =& get_instance();

        $config = array(
            'image_library' => 'gd2',
            'source_image' => $data['full_path'],
            'new_image' => $this->path . '/' . $data['file_name'],
            'maintain_ratio' => TRUE,
            'width' => $width,
            'height' => $height,
            'create_thumb' => FALSE
        );

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

        $CI->image_lib->clear();
        unset($config);
        return $data['file_name'];
    }
}

用法

$img1 = $this->up_image->upload_image();
$thumb = $this->up_image->resize($img1, 140, 120);

$img2 = $this->up_image->upload_image();
$large = $this->up_image->resize($img2, 420, 360);

【问题讨论】:

    标签: php codeigniter class gd2


    【解决方案1】:

    将裁剪方法与上传方法分开。

    上传后,对上传的文件分别进行两次裁剪。

    删除上传的温度。文件。

    完成。

    【讨论】:

    • 谢谢,我这样做了,但只完成了第一次调整大小,而不是第二次。 image_lib>resize() 是创建另一个图像还是我需要自己做?我还调用了 image_lab->clear(),因为它正在创建一个全黑图像。我已经发布了更新课程的贴图。
    • CI resize() 方法将调整大小的图像保存在您在“new_image”参数中指定的任何位置。它将 source_image 单独保留,因此您应该能够在源图像上运行另一个调整大小。在这种情况下,我认为您正在使用第二次调整大小来覆盖相同的新图像。另外,我认为你在 resize 方法上返回了错误的文件名,你应该返回 new_image 文件名。在方法参数列表中包含 target_filename (new_image) 可能会有所帮助。 IE。调整大小($data, $width, $height, $new_file)
    • 我已经更新了它,这对我有用,我应该改变它还是它很酷?谢谢:)
    • 其实,如果你只需要创建一个拇指,你可以在 $CI->image_lib->clear(); 之后再次运行 resize , CI 在图像调整大小库中也有一个内置的缩略图选项。您现在正在做的是再次为缩略图执行上传,这有点多余并且浪费资源。
    • 我试过了,我无法获取缩略图和其他文件的名称。我只收到缩略图名称作为回报。也许我应该得到新的图像文件名才能正常工作?
    猜你喜欢
    • 2019-02-03
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    相关资源
    最近更新 更多