Problem loading and modifying CodeIgniter code

I spent days trying to make this work based on the examples in the documentation, but I have something missing or I'm just STUPID!

I have a CMS application where users upload an image to display in a very fixed layout. We do not want to limit the file size of the downloaded image, but rather "process" it after it appears.

The image should be 615 pixels wide, but some images downloaded directly from digital cameras are 2500X2000 and higher, so this is CRITICAL.

I compiled the code from the manual and the image is successfully uploaded to a folder in the CMS application. However, the image does NOT change.

If I ever get it for resizing, my plan is to present the image to the user for cropping using jCrop (the final image should be 615X275, and maybe it needs to be cropped in height after resizing), and then use codeigniter for an FTP image to a folder with folders with their original name.

I would be grateful for any help in this matter!

Here is my code:

function do_feature_upload () {
        $ imageName = $ this-> uri-> segment (3);
        // echo $ imageName;

        // Where the file is going to be placed
        $ config ['upload_path'] = "./uploads/".$_SESSION['dbPropNumber '];
        $ config ['allowed_types'] = 'jpg | jpeg';
        $ config ['max_size'] = '0';
        $ config ['file_name'] = $ imageName. '. jpg';
        $ config ['overwrite'] = 'TRUE';

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

        if (! $ this-> upload-> do_upload ()) {
            $ error = array ('error' => $ this-> upload-> display_errors ());

            $ error ['propertyDropdown'] = $ _SESSION ['propertyDropdown'];
            $ error ['username'] = $ _SESSION ['username'];
            $ error ['dbPropNumber'] = $ _SESSION ['dbPropNumber'];
            $ error ['propertyName'] = $ this-> content-> getPropertyName ($ _ SESSION ['dbPropNumber']);

            $ this-> load-> view ('upload_AmenityImage', $ error);
        } else {
            $ image_data = $ this-> upload-> data ();

            $ origWidth = $ image_data ['image_width'];
            $ origHeight = $ image_data ['image_height'];
            $ newWidth = 615;
            $ newHeight = $ newWidth * $ origHeight / $ origWidth;

            $ resize = array (
                'image_library' => 'gd2',
                'source_image' => base_url (). 'uploads /'.$_ SESSION [' dbPropNumber '].' / '. $ imageName.'. jpg ',
                'new_image' => base_url (). 'uploads /'.$_ SESSION [' dbPropNumber '].' / '. $ imageName.'1.jpg',
                'create_thumb' => FALSE,
                'maintain_ratio' => FALSE,
                'width' => $ newWidth,
                'height' => $ newHeight
            );

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

            $ data = array ('upload_data' => $ this-> upload-> data ());
            $ data ['propertyDropdown'] = $ _SESSION ['propertyDropdown'];
            $ data ['username'] = $ _SESSION ['username'];
            $ data ['dbPropNumber'] = $ _SESSION ['dbPropNumber'];
            $ data ['propertyName'] = $ this-> content-> getPropertyName ($ _ SESSION ['dbPropNumber']);

            // Present jCrop option after image is resized

            // FTP to final destination

            $ this-> load-> view ('upload_success', $ data);
        } // end if
    } // end function
+3
source share
2 answers

, , , . , .

$this->prefix - , , . :

$this->prefix = FCPATH.'uploads'.DIRECTORY_SEPARATOR;

/**
 * Resizes an image to fit exact dimensions
 * 
 * @param string    filename
 * @param int      target_width
 * @param int      target_height
 * 
 * @return array('success' ? null : 'error')
 */
function resizeImageToDimensions($filename, $target_width=700, $target_height=399)
{
    $file_type = $this->getFileType($this->prefix.$filename);

    if (!$file_type || $file_type != 'image')
        return array('success'=>false, 'error'=>"This file doesn't exist or isn't an image");

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

    list($width, $height) = getimagesize($this->prefix.$filename);
    $current_ratio = $width/$height;
    $target_ratio = $target_width/$target_height;
    $config['source_image'] = $this->prefix.$filename;

    if ($current_ratio > $target_ratio)
    {
        //resize first to height, maintain ratio
        $config['height'] = $target_height;
        $config['width'] = $target_height * $current_ratio;
        $this->image_lib->initialize($config);

        if (!$this->image_lib->resize())
            return array('success'=>false, 'error'=>"There was an error while resizing this image");

        //then crop off width
        $config['width'] = $target_width;
        $config['maintain_ratio'] = false;
        $this->image_lib->initialize($config);

        if ($this->image_lib->crop())
            return array('success'=>true);
        else
            return array('success'=>false, 'error'=>"There was an error while cropping this image");
    }
    else if ($current_ratio < $target_ratio)
    {
        //resize first to width, maintain ratio
        $config['width'] = $target_width;
        $config['height'] = $target_width / $current_ratio;
        $this->image_lib->initialize($config);

        if (!$this->image_lib->resize())
            return array('success'=>false, 'error'=>"There was an error while resizing this image");

        //then crop off height
        $config['height'] = $target_height;
        $config['maintain_ratio'] = false;
        $this->image_lib->initialize($config);

        if ($this->image_lib->crop())
            return array('success'=>true);
        else
            return array('success'=>false, 'error'=>"There was an error while cropping this image");
    }
    else {
        $config['width'] = $target_width;
        $config['height'] = $target_height;
        $this->image_lib->initialize($config);

        if ($this->image_lib->resize())
            return array('success'=>true);
        else
            return array('success'=>false, 'error'=>"There was an error while resizing this image");
    }
}
+6

, , . CodeIgniter, .

timthumb script, , : (

, , timthumb. , , , , !

+1

Source: https://habr.com/ru/post/1777952/


All Articles