-->można podać wysokość i szerokość obrazka docelowego jako liczbę lub "auto"
-->można podać jedną z opcji "fill" "crop" lub "auto"
-->podanie wymiaru jako "auto" oznacza automatyczne wskoczenie do trybu "auto" (chyab nie da sie inaczej)
-->obrazki nigdy nie są zwiększane; mogą być tylko zmniejszane
-->wolne przestrzenie wypełniane są białym tłem
-->podajemy $_MAX_WIDTH_ i $_MAX_HEIGHT_ oznaczające maksymalne wymiary (to chyba jest najtrudniejsze bo obrazek docelowy nie powinien przekraczac tych wartości)
Przy testach z opcją "fill" i "crop" działa poprawnie. Przy opcji "auto" są jeszcze małe bugi. Moze by się komuś chciało z tym pobawić i dopomóc (choć pewnie nie). Za testy też byłbym wdzięczny.
<?php /** * The $a_mode argumant can be "fill", "crop", "auto"[default mode]; * Parameters $a_width and $a_height can be "0" or "auto" (the "auto" mode is triggered on then); */ function create_resampled_image( $a_source_file, $a_destination_file, $a_width, $a_height, $a_mode="auto" ) { $_MAX_WIDTH_ = 640; $_MAX_HEIGHT_ = 480; if( !$a_width ) $a_width = "auto"; if( !$a_height ) $a_height = "auto"; if( $a_width == "auto" or $a_height == "auto" ) $a_mode = "auto"; switch( $a_mode ) { case "fill": #no mistakes found if( $a_width >= $_MAX_WIDTH_ and $a_height >= $_MAX_HEIGHT_ ){ $a_width = $_MAX_WIDTH_; $a_height = $_MAX_HEIGHT_; $dest_height = $source_height; $dest_width = $source_width; }elseif( $a_width >= $_MAX_WIDTH_ ){ $a_width = $_MAX_WIDTH_; $dest_height = $source_height; $dest_width = $source_width; }elseif( $a_height >= $_MAX_HEIGHT_ ){ $a_height = $_MAX_HEIGHT_; $dest_height = $source_height; $dest_width = $source_width; } if( ( $a_height/$source_height ) > ( $a_width/$source_width ) ) { $dest_width = $a_width; $dest_height = ($dest_width/$source_width)*$source_height; $dest_left = 0; $dest_top = ( $a_height - $dest_height ) / 2; }else{ $dest_width = ($a_height/$source_height)*$source_width; $dest_height = $a_height; $dest_left = ( $a_width - $dest_width ) / 2; $dest_top = 0; } if( $dest_width > $source_width ){ $dest_width = $source_width; $dest_left = ( $a_width - $dest_width ) / 2; } if( $dest_height > $source_height ){ $dest_height = $source_height; $dest_top = ( $a_height - $dest_height ) / 2; } break; case "crop": #no mistakes found if( $a_width >= $_MAX_WIDTH_ and $a_height >= $_MAX_HEIGHT_ ){ $a_width = $_MAX_WIDTH_; $a_height = $_MAX_HEIGHT_; $dest_height = $source_height; $dest_width = $source_width; }elseif( $a_width >= $_MAX_WIDTH_ ){ $a_width = $_MAX_WIDTH_; $dest_height = $source_height; $dest_width = $source_width; }elseif( $a_height >= $_MAX_HEIGHT_ ){ $a_height = $_MAX_HEIGHT_; $dest_height = $source_height; $dest_width = $source_width; } if( ( $a_height/$source_height ) > ( $a_width/$source_width ) ){ $dest_width = ($a_height/$source_height)*$source_width; $dest_height = $a_height; $dest_left = ( $a_width - $dest_width ) / 2; $dest_top = 0; }else{ $dest_width = $a_width; $dest_height = ($a_width/$source_width)*$source_height; $dest_left = 0; $dest_top = ( $a_height - $dest_height ) / 2; } if( $dest_width > $source_width ){ $dest_width = $source_width; $dest_left = ( $a_width - $dest_width ) / 2; } if( $dest_height > $source_height ){ $dest_height = $source_height; $dest_top = ( $a_height - $dest_height ) / 2; } break; case "auto": #try to change this in future if( $a_width == "auto" and $a_height == "auto" ) { $dest_width = $source_width; $dest_height = $source_height; $a_width = $dest_width; $a_height = $dest_height; $dest_left = 0; $dest_top = 0; }elseif( $a_width == "auto" ){ $dest_width = ($a_height/$source_height)*$source_width; $dest_height = $a_height; $dest_left = 0; $dest_top = 0; $a_width = $dest_width; } else{ $dest_width = $a_width; $dest_height = ($a_width/$source_width)*$source_ $dest_top = 0; $a_height = $dest_height; } if( $a_width >= $_MAX_WIDTH_ and $a_height >= $_MAX_HEIGHT_ ){ $a_width = $_MAX_WIDTH_; $a_height = $_MAX_HEIGHT_; $dest_height = ($a_width/$_MAX_WIDTH_)*$_MAX_HEIGHT_; $dest_width = ($a_height/$_MAX_HEIGHT_)*$_MAX_WIDTH_; }elseif( $a_width >= $_MAX_WIDTH_ ){ $a_width = $_MAX_WIDTH_; $dest_height = $source_height; $dest_width = ($a_height/$source_height)*$source_width; }elseif( $a_height >= $_MAX_HEIGHT_ ){ $a_height = $_MAX_HEIGHT_; $dest_height = $_MAX_HEIGHT_; $dest_width = ($a_height/$source_height)*$source_width; } $dest_left = ( $a_width - $dest_width ) / 2; $dest_top = ( $a_height - $dest_height ) / 2; break; } $mime_type = file_mime_type( $a_source_file ); $src_image = imagecreatefromjpeg( $a_source_file ); $dst_image = imagecreatetruecolor( $a_width, $a_height ); $background = ImageColorAllocate( $dst_image, 255, 255, 255 ); ImageFill( $dst_image, 0, 0, $background imagecopyresampled( $dst_image, $src_image, $dest_left, $dest_top, 0, 0, $dest_width, $dest_height, $source_width, $source_height ); imagejpeg( $dst_image, $a_destination_file imagedestroy( $dst_image ); imagedestroy( $src_image ); } ?>
i jeszcze funkcja mime type:
<?php function file_mime_type( $a_file ) { if( function_exists(finfo_open) ) { $handle = finfo_open( FILEINFO_MIME ); $mime_type = finfo_file( $handle, $a_file ); finfo_close( $handle ); return $mime_type; } else if( function_exists(mime_content_type) ) { return mime_content_type($a_file); }else{ } } ?>