0

I created a library that just uploads 1 image, crops it given the path, width, and height. But i cant figure out how I can have it crop as many images as i want and return an array of all the filenames it created. Here is my library, let me know if there is a good approach. thanks

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

Update: I figured it out, here is the class and how to use it for anyone who is trying to do the something. What this class does is uploads the image and lets you create as many sizes of the original upload as you want.

[CLASS]

<?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'];
    }
}

Usage

$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);
Exploit
  • 6,278
  • 19
  • 70
  • 103

1 Answers1

1

Separate the crop method from the upload method.

After upload, perform crop twice, separately on the uploaded file.

Delete uploaded temp. file.

Finish up.

Winterain
  • 284
  • 1
  • 3
  • 16
  • thanks, i did that but only the first resize is being done and not the second. does the image_lib>resize() create another image or would i need to do that myself? i also called image_lab->clear() because it was creating a full black image. I have posted a pastie of the updated class. – Exploit Mar 30 '14 at 04:11
  • the CI resize() method saves the resized image at whichever location you specify in the 'new_image' parameter. It leaves the source_image alone, so you should be able to run another resize on the source image. In this case I think you are overriding your the same new image with the 2nd resizing. Also, I think you are returning the wrong filename on the resize method, you should instead be returning the new_image filename. It will probably be helpful to include the target_filename (new_image) in the method param list. ie. resize($data, $width, $height, $new_file) – Winterain Mar 30 '14 at 04:43
  • i've updated it, this is what is working for me, should i change it or its cool? thanks :) – Exploit Mar 30 '14 at 04:47
  • Actually, if all you need to do is to create a thumb, you can run resize again after $CI->image_lib->clear(); , also CI has a built in thumbnail option in the image resize lib. What you're doing now is performing the upload again for the thumbnail, which is kinda redundant and wastes resources. – Winterain Mar 30 '14 at 04:58
  • i tried that, i wasnt able to get the name of the thumbnail and the other file. i was only receiving the thumbnails name in return. Maybe i should get the new image filename for it to work right? – Exploit Mar 30 '14 at 05:05