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 ?
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.
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).
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.
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.
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.
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) {
) </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>