<?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;
list
( $source_width, $source_height ) = getimagesize( $a_source_file ); 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
//ustawiamy maksymalne wymiary
if (($a_width > $_MAX_WIDTH_) || ($a_width == 'auto' && $source_width > $_MAX_WIDTH_)) $a_width = $_MAX_WIDTH_;
if (($a_height > $_MAX_HEIGHT_) || ($a_height == 'auto' && $source_height > $_MAX_HEIGHT_)) $a_height = $_MAX_HEIGHT_;
//sprawdzamy szerokość, jeżeli nie jest automatyczna, oraz większa od maksymalnej - zmieniamy
if ($a_width != 'auto' && $a_width < $source_width) {
$ws_x = $a_width / $source_width;
$dest_width = $a_width;
$dest_height = ceil($ws_x * $source_height); }
else {
//jako że następny warunek wymaga tych zmiennych, przypisujemy im wartości bez zmi
any
$dest_width = $source_width;
$dest_height = $source_height;
$a_width = $source_width;
}
// ok, teraz sprawdzamy, czy wysokość którą otrzymaliśmy mieści się w skali
if ($a_height != 'auto' && $a_height < $dest_height) {
// ups, nie mieści się. Obliczamy nową szerokość i ustawiamy wysokość na maksymaln
ą dopuszczalną wartość
$ws_y = $a_height / $dest_height;
$dest_width = ceil($ws_y * $dest_width); $dest_height = $a_height;
}
$dest_left = 0;
$dest_top = 0;
$a_width = $dest_width;
$a_height = $dest_height;
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 );
ImageColorTransparent($dst_image, $background);
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 );
imagepng( $dst_image, $a_destination_file);
imagedestroy( $dst_image );
imagedestroy( $src_image );
}
create_resampled_image('input.jpg','output.png',200,200,'fill');
?>