Revision: 58866
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 7, 2012 17:03 by kh411d
Initial Code
function resizeImage( $file, $thumbpath, $max_side = NULL , $fixfor = NULL, $cropped = false ) {
// 1 = GIF, 2 = JPEG, 3 = PNG
if ( file_exists( $file ) ) {
$type = getimagesize( $file );
if (!function_exists( 'imagegif' ) && $type[2] == 1 ) {
$error = __( 'Filetype not supported. Thumbnail not created.' );
}
elseif (!function_exists( 'imagejpeg' ) && $type[2] == 2 ) {
$error = __( 'Filetype not supported. Thumbnail not created.' );
}
elseif (!function_exists( 'imagepng' ) && $type[2] == 3 ) {
$error = __( 'Filetype not supported. Thumbnail not created.' );
} else {
// create the initial copy from the original file
if ( $type[2] == 1 ) {
$image = imagecreatefromgif( $file );
}
elseif ( $type[2] == 2 ) {
$image = imagecreatefromjpeg( $file );
}
elseif ( $type[2] == 3 ) {
$image = imagecreatefrompng( $file );
}
if ( function_exists( 'imageantialias' ))
imageantialias( $image, TRUE );
$image_attr = getimagesize( $file );
$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;
$image_resized_crop = imagecreatetruecolor( $image_new_width, $image_new_height);
@imagecopyresampled( $image_resized_crop, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] );
$cropX = intval((imagesx($image_resized_crop) - $max_side) / 2);
$cropY = intval((imagesy($image_resized_crop) - $max_side) / 2);
}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){
$thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height);
@imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] );
}elseif($cropped){
$thumbnail = imagecreatetruecolor( $max_side, $max_side);
@imagecopyresampled( $thumbnail, $image_resized_crop, 0, 0, $cropX, $cropY, $max_side, $max_side, $max_side, $max_side);
}
if (!imagejpeg( $thumbnail, $thumbpath ) ) {
$error = 0;
}
}
} else {
$error = 0;
}
if (!empty ( $error ) ) {
return $error;
} else {
return $thumbpath;
}
}
Initial URL
Initial Description
Easily make a thumb file, with a fix size, or a cropped center image
Initial Title
Image Resize function for PHP
Initial Tags
Initial Language
PHP