Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Image upload and manipulation process
  • Hi,

    I would like to develop an application that upload pictures, resize it and set the format to jpeg.
    I use the upload process of Fuelphp to do it.
    I would like to upload the picture, manipulate it and save it. But as far as I understand, I first need to save the uploaded picture somewhere in my server to be able to reopen it with the image classes of FuelPHP.

    In other words, the upload process create a tmp file in the default /tmp/ folder. Then, the file is rewriten with the save() method (and renamed in my case). Then if I need to manipulate the picture (crop, resize, ...), I will have to reopen the file and save it again.
    So in that case the process needs 3 writings on the disk.

    I tried seval things but I can't figure it out.
    For instance, to use the tmp file but FuelPHP shouts that the library does not support this filetype.

    Is there a way to avoid to save the file in the server prior to the manipulation (for example Image::load(memoryStreamFile) ) ?
    Or any other optimisation to reduce the weight of the process ?

    I use the latest 1.6 build.

    Thank you in advance for your help.

    Philippe
  • HarroHarro
    Accepted Answer
    When you upload a file using PHP, it will be stored in a temp location on disk after the upload. After you have called Upload::process() (if auto_process is false in your config, otherwise it happens automatically), you can call get_files() to get the information about the uploaded files. This includes the name and location of the uploaded file.

    The issue you bumped into is that the Image library needs to know the type of image (jpg, png, etc), before you can manipulate it. It uses the file's extension to determinte the type, but as you have noticed, the temp file doesn't have an extension, so the load fails.

    You can work around that by passing the extension as the third parameter of the load() method. You can use the mime information in the array returned by get_files() to determine what extension to pass. The Image class supports the extensions 'png', 'gif', 'jpg' and 'jpeg'.

    After you have manipulated the image, don't forget to call Upload::save() to save the file to its final location.
  • Thank you so much Harro,
    That's exactly what I needed.
  • Ok, now FuelPhp allows me to open the temp file with the given extension.
    But what I need is to be able to save it in jpeg whatever the source format is (that was a simple manipulation).

    To do it, I tried :

    \Fuel\Core\Image::load($file['file'], false, $file['extension'])->output('jpg');
    Upload::save();

    It gives me an error 500 (internal server error).
    I think I don't use the Image::output() method well. Could you give me a tip ?

    Thank you in advance.
  • HarroHarro
    Accepted Answer
    output() does what it says, it outputs the file to the browser.

    Which is probably not what you want, you probably want to call save() instead, see http://docs.fuelphp.com/classes/image.html#/method_save
  • That's right ! Thanks again.
  • Hello,

    I don't know what I'm doing wrong but it is not working.

    When I do the following I always get an error 500 telling me the filetype (.tmp) is not supported :
    \Fuel\Core\Image::load($file['file'], false, $file['extension'])->save($config['path'].'/'.$OutputFileName.'.jpg');

    If I don't use the Image::save() method or any Image method I don't get any error

    I don't know how to solve it, I think I'm quiet obliged to save the uploaded file first. If you have any idea regarding this issue would be nice for my understanding.

    Thank you anyway.
  • I assume this is because $file['extension'] contains ".tmp", which is something the Image class can't handle.

    Instead, use the mimetype information in the upload file array to determine if it's a png, gif or jpg, and pass that instead of the real file extension. Also, you need to save back to the original file name (in the tmp folder), otherwise Upload::save() can't do it's job.
  • I'm running into this same issue.
    It seemed like it was going to work, but just keeps failing with the 'filetype not supported' errors

    What I have is;


    foreach (Upload::get_files() as $file) {
        Upload::save();
        Image::load($file['file'],false, $file['mimetype'])->preset('100x100', $file['saved_as']);

        Image::load($file['file'],false, $file['mimetype'])->preset('800', $file['saved_as']);

    Presets are as follows:
    'presets' = array(
            '800' => array(
                'quality' => 100,
                'bgcolor' => null,
                'actions' => array(
                    array('resize', 800),
                    array('save', DOCROOT.'/../images_800/$1'),
                )
            ),
    ........
        )

    I had this working with just the Upload, but now that i am trying to manipluate the image and save it to two locations, i am unable to make this work.

    any suggestions?

    thanks.


  • With some adjusting, I can get the image to save Only on in the first Image::load method. The second resize doesnt work and neither does it if I only do the one resize.

    The presets I am using are:

    'presets' = array(
            '280x280' => array(
                'quality' => 100,
                'filetype' => 'jpg',
                'actions' => array(
                      array('crop_resize', 280, 280),
                      array('save', '$1'),
                )
            ),
            '800' => array(
                'quality' => 100,
                'filetype' => 'jpg',
                'actions' => array(
                    array('resize', 800),
                    array('save', '$1'),
                )
            )
        )


    I'm stuck and cannot seem to get past the first Image::load ... on only the first element in the uploaded array.

    Is there a problem with multiple images in 1.6 ?


    Do I need to set the 'path' in the Upload config?
    I currently have it not set in my custom upload.php config file and then in my code where I am uploading i am using:

    Upload::save()
    foreach (Upload::get_files() as $key=>$file) {

    Image::load($file['file'], false, 'jpg')->preset('280x280', DOCROOT.'/../images_280x280/'.$file['saved_as']);
    Image::load($file['file'], false, 'jpg')->preset('800', DOCROOT.'/../images_8000/'.$file['saved_as']);

    ....
    }

    Second one always fails and the '800' one never runs either.

    'COREPATH/classes/image/driver.php @ line 151'

    There is an error or message in the posted to page along with 'Prior Contents':

    Array
    (
        [saved_as] => 8db062babb3218deaafc2646f4db8e68.jpg
        [field] => file:0
        [name] => 6008033350cf804bedd0a.jpg
        [type] => image/jpeg
        [file] => /tmp/php1hrce4
        [error] =>
        [size] => 392623
        [extension] => jpg
        [basename] => 6008033350cf804bedd0a
        [mimetype] => image/jpeg
        [saved_to] => //
        [errors] => Array
            (
            )

    )
    </pre><hr /><div>Image Class was initialized using the gd driver. </div><div> </div><div><strong>Loaded</strong> <code>/tmp/php1hrce4</code> with size of 1093x752 </div><div>Queued <code>'crop_resize', '280', '280'</code> </div><div> </div><div>Saving image as <code>/home/memine/wwwroot/me-mine.com//../images_280x280/8db062babb3218deaafc2646f4db8e68.jpg</code> </div><div> </div><div><b>Executing <code>'crop_resize', '280', '280'</code></b> </div><div>Resizing image to 407, 280 with keeping AR and without padding. </div><div>Cropping image 280x280+63+0 based on coords (63, 0), (343, 280) </div><div>Adding background color #fff </div><div>Reloading was called! </div>

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion