image_resize and JPEG filesize

See all posts Reply

image_resize and JPEG filesize new!
by Konstantin, 16 years, 2 months ago
Hi, Colin!
Now I try to execute the following algorithm:
1) the maximum size of a file is 300 kilobytes
2) the maximum sizes of a file is 1024*768 pixels
If the user has prepared in advance a file according to these rules, recalculation should not be made. And at me on an input volume of a file of 290 kilobyte, after upload I have 150 kilobytes. How it is possible to avoid recalculation?
$handle->image_resize            = true;
$handle->file_max_size           = '307200';
$handle->allowed                 = array('image/*');
$handle->file_new_name_body      = $add_data;
$handle->image_x                 = 1025;
$handle->image_y                 = 769;
$handle->image_ratio_no_zoom_in  = true;
$handle->image_min_width         = 100;
$handle->image_min_height        = 50;
$handle->process('gallery/data/');

Second question.
How I can forbid to upload the files which are distinct from JPEG? For example, if the user takes in head to upload GIF, and I do not want it? Whether it is possible to include $handle-> image_convert = ' jpg '; whether it will influence loading JPEG-files?

Thanks a lot. RegardsReply
Re: image_resize and JPEG filesize new!
by colin, 16 years, 2 months ago
It is currently not possible to avoid calculation if the uploaded image has already the dimensions that you want to resize it to.

However, the class provides some information about the image, before calling process(). Available are image_src_x, image_src_y and the newly introduced image_src_bits, image_src_pixels and image_src_type. Using these, you can process the picture differently.

For your second question, if you use $handle->image_convert = ' jpg ';, then all images will be converted to JPEG.
If you want to restrict to JPEG only, use:
$handle->allowed = array('image/jpg','image/jpeg');
Reply
Re: image_resize and JPEG filesize new!
by Konstantin, 16 years, 2 months ago
Hi, Colin! Thanks.
I spent tests and could not find essential distinctions between jpeg 300 kilobytes and Jpeg 150 kilobytes. Probably, I shall leave all on the places.Reply
Re: image_resize and JPEG filesize new!
by Konstantin, 16 years, 2 months ago
Hi, Colin!
Now I have construct that code:
// if file size is OK, processing without resizing              
// it is very important                                                                                                         
if ( $handle->image_src_x < 1025 && $handle->image_src_y < 769 ) {                                                               
    $handle->file_max_size       = '307200';                    
    $handle->allowed             = array('image/*');            
    $handle->file_new_name_body  = $add_data;                   
    $handle->image_min_width         = 50;                      
    $handle->image_min_height        = 50;                      
    $handle->process('gallery/data/');                          
} else  { // create big foto 1024x768 pix, let users read rules                                                                 
    $handle->image_resize            = true;                    
    $handle->allowed                 = array('image/*');        
    $handle->file_max_size           = '307200';                
    $handle->file_new_name_body      = $add_data;               
    $handle->image_x                 = 1024;                    
    $handle->image_y                 = 768;                     
    $handle->image_ratio_no_zoom_in  = true;                    
    $handle->image_min_width         = 50;                      
    $handle->image_min_height        = 50;                      
    $handle->process('gallery/data/');                          
}                                                               
                                                                
// create thumbnail                                                                                                             
$handle->allowed                 = array('image/*');            
$handle->file_max_size           = '307200';                    
$handle->file_new_name_body      = $add_data;                   
$handle->image_resize            = true;                        
$handle->image_x                 = 200;                         
$handle->image_y                 = 200;                         
$handle->image_min_width         = 50;                          
$handle->image_min_height        = 50;                          
$handle->image_ratio             = true;                        
$handle->process('gallery/tmb/');

I try to use $handle->allowed = array('image/jpg','image/jpeg');, but have error with processing JPEG: unsupported file type.... SAD. Changing to 'image/*' -- have not errors.Reply