/ Published in: PHP
                    
                                        
Easily make a thumb file, with a fix size, or a cropped center image
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
function resizeImage( $file, $thumbpath, $max_side = NULL , $fixfor = NULL, $cropped = false ) {
// 1 = GIF, 2 = JPEG, 3 = PNG
$error = __( 'Filetype not supported. Thumbnail not created.' );
}
$error = __( 'Filetype not supported. Thumbnail not created.' );
}
$error = __( 'Filetype not supported. Thumbnail not created.' );
} else {
// create the initial copy from the original file
if ( $type[2] == 1 ) {
}
elseif ( $type[2] == 2 ) {
}
elseif ( $type[2] == 3 ) {
}
$max_side = $max_side ? $max_side : $image_attr[0];
// figure out the longest side
if($fixfor){
if($fixfor == 'width'){
$image_width = $image_attr[0];
$image_height = $image_attr[1];
$image_new_width = $max_side;
$image_ratio = $image_width / $image_new_width;
$image_new_height = $image_height / $image_ratio;
}elseif($fixfor == 'height'){
$image_width = $image_attr[0];
$image_height = $image_attr[1];
$image_new_height = $max_side;
$image_ratio = $image_height / $image_new_height;
$image_new_width = $image_width / $image_ratio;
}
}elseif($cropped){
$image_new_width = $max_side*2;
$image_ratio = $image_attr[0] / $image_new_width;
$image_new_height = $image_attr[1] / $image_ratio;
@imagecopyresampled( $image_resized_crop, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] );
}else{
if ( $image_attr[0] > $image_attr[1] ) {
$image_width = $image_attr[0];
$image_height = $image_attr[1];
$image_new_width = $max_side;
$image_ratio = $image_width / $image_new_width;
$image_new_height = $image_height / $image_ratio;
//width is > height
} else {
$image_width = $image_attr[0];
$image_height = $image_attr[1];
$image_new_height = $max_side;
$image_ratio = $image_height / $image_new_height;
$image_new_width = $image_width / $image_ratio;
//height > width
}
}
if(!$cropped){
@imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] );
}elseif($cropped){
@imagecopyresampled( $thumbnail, $image_resized_crop, 0, 0, $cropX, $cropY, $max_side, $max_side, $max_side, $max_side);
}
$error = 0;
}
}
} else {
$error = 0;
}
return $error;
} else {
return $thumbpath;
}
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                