Ovako nesto je vec bilo na forumu, ja sam samo malo doradio i mogu ti reci da radi skroz super :)... Evo ga kod funkcije za resize:
Code:
public function paddedImage ($src_filename, $dest_filename, $_width, $_height, $bg_R, $bg_G, $bg_B, $quality) {
if(!is_dir($src_filename))
{
$ext = strtolower(pathinfo($src_filename, PATHINFO_EXTENSION));
switch(strtolower($ext))
{
case "jpg":
case "jpeg":
$src = imagecreatefromjpeg($src_filename);
break;
case "png":
$src = imagecreatefrompng($src_filename);
break;
case "gif":
$src = imagecreatefromgif($src_filename);
break;
}
$width = @imagesx($src);
$height = @imagesy($src);
if($width != 0 && $height != 0)
{
$ratio_w = $_width / $width;
$ratio_h = $_height / $height;
$ratio = ($ratio_w < $ratio_h) ? $ratio_w : $ratio_h;
$new_width = $width * $ratio;
$new_height = $height * $ratio;
$padd_x = ($_width - $new_width) / 2;
$padd_y = ($_height - $new_height) / 2;
$dest = imagecreatetruecolor($_width, $_height);
$background_color = imagecolorallocate($dest, $bg_R, $bg_G, $bg_B);
imagefilledrectangle($dest, 0, 0, $_width, $_height, $background_color);
imagecopyresampled($dest, $src, $padd_x, $padd_y, 0, 0, $new_width, $new_height, $width, $height);
if ($dest_filename) {
imagejpeg($dest, $dest_filename, $quality);
} else {
header('Content-Type: image/jpeg');
imagejpeg($dest, null, $quality);
}
imagedestroy($dest);
imagedestroy($src);
}
}
}
Funkcija se poziva ovako:
Code:
paddedImage($src_filename, $dest_filename, 313, 235, 255, 255, 255, 100);
Prvi i drugi parametri su jasni, treci i cetvrti su dimenzije resized slike, ovo 255, 255, 255 je pozadinska boja, tj. bela u ovom slucaju i 100 je naravno kvalitet jpg fajla :).
Nadam se da sam pomogao bar malo ;).