$maxwidth = 120;
$maxheight = 150;
$img = imagecreatefromjpeg($jpgimage);
$width = imagesx($img);
$height = imagesy($img);
if ($height > $width)
{
$ratio = $maxheight / $height;
$newheight = $maxheight;
$newwidth = $width * $ratio;
}
else
{
$ratio = $maxwidth / $width;
$newwidth = $maxwidth;
$newheight = $height * $ratio;
}
$newimg = imagecreatetruecolor($newwidth,$newheight);
$palsize = ImageColorsTotal($img);
for ($i = 0; $i < $palsize; $i++)
{
$colors = ImageColorsForIndex($img, $i);
ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']);
}
imagecopyresized($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($newimg,$outputfile);
, , ( ), . , , , , . , 200x300 100x150, 300x200 120x80.
, , 120, , , - , 200x300, 120x180 - , , .
Letterboxing , x y, , imagecopyresized(). - 100x150, , 10, 10px 120x150. Letterboxing 120x80 X 0, Y 35, 120x150 35px .
$newimg $maxwidth, $maxheight, $newwidth, $newheight, imagecopyresized() .
, :
if ($height > $width)
{
$ratio = $maxheight / $height;
$newheight = $maxheight;
$newwidth = $width * $ratio;
$writex = round(($maxwidth - $newwidth) / 2);
$writey = 0;
{
else
{
$ratio = $maxwidth / $width;
$newwidth = $maxwidth;
$newheight = $height * $ratio;
$writex = 0;
$writey = round(($maxheight - $newheight) / 2);
}
$newimg = imagecreatetruecolor($maxwidth,$maxheight);
imagecolorallocate($newimg,0,0,0);
imagecopyresized($newimg, $img, $writex, $writey, 0, 0, $newwidth, $newheight, $width, $height);
, .