<?php
function forceConstraints($srcFile, $srcType, $dstType, $dstWidth, $dstHeight, $dstPath)
{
if ($srcType == "jpg")
$handle = @imagecreatefromjpeg($srcFile);
else if ($srcType == "png")
$handle = @imagecreatefrompng($srcFile);
else if ($srcType == "gif")
$handle = @imagecreatefromgif($srcFile);
else
return false;
if (!$handle) return false;
$srcWidth = @imagesx($handle);
$srcHeight = @imagesy($handle);
$newHandle = @imagecreatetruecolor($dstWidth, $dstHeight);
if (!$newHandle)
return false;
if($srcHeight < $srcWidth)
{
$ratio = (double)($srcHeight / $dstHeight);
$cpyWidth = round($dstWidth * $ratio); if ($cpyWidth > $srcWidth)
{
$ratio = (double)($srcWidth / $dstWidth);
$cpyWidth = $srcWidth;
$cpyHeight = round($dstHeight * $ratio); $xOffset = 0;
$yOffset = round(($srcHeight - $cpyHeight) / 2
); } else {
$cpyHeight = $srcHeight;
$xOffset = round(($srcWidth - $cpyWidth) / 2
); $yOffset = 0;
}
} else {
$ratio = (double)($srcWidth / $dstWidth);
$cpyHeight = round($dstHeight * $ratio); if ($cpyHeight > $srcHeight)
{
$ratio = (double)($srcHeight / $dstHeight);
$cpyHeight = $srcHeight;
$cpyWidth = round($dstWidth * $ratio); $xOffset = round(($srcWidth - $cpyWidth) / 2
); $yOffset = 0;
} else {
$cpyWidth = $srcWidth;
$xOffset = 0;
$yOffset = round(($srcHeight - $cpyHeight) / 2
); }
}
if (!@imagecopyresampled($newHandle, $handle, 0, 0, $xOffset, $yOffset, $dstWidth, $dstHeight, $cpyWidth, $cpyHeight))
return false;
@imagedestroy($handle);
if ($dstType == "png")
@imagepng($newHandle, $dstPath.".png");
else if ($dstType == "jpg")
@imagejpeg($newHandle, $dstPath.".jpg", 100);
else if ($dstType == "gif")
@imagegif($newHandle, $dstPath.".gif");
else
return false;
@imagedestroy($newHandle);
return true;
}
?>
Funkcja wyciągnieta prosto z komenatrzy w manualu

! Przeglądaj je czasami.