T
Keep an old, far from perfect, but a work function that will squeeze rather than cut images:// ИЗМЕНЕНИЕ РАЗМЕРОВ ИЗОБРАЖЕНИЯ И ЗАПИСЬ НА ДИСК
// $image_path_original ... str путь к изменяемой картинке
// $max_width ............. максимальная ширина изображения
// $max_height ............ максимальная высотв изображения
// $ext ................... расширение изображения (gif или jpg)
// $image_path_save ....... путь, куда будет записано измененное изображение
function image_resize($image_path_original, $max_width, $max_height, $ext, $image_path_save)
{
$max_width = intval($max_width);
$max_height = intval($max_height);
$size = GetImageSize($image_path_original);
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if ( ($width <= $max_width) && ($height <= $max_height) )
{
$tn_width = $width;
$tn_height = $height;
}
else if ( ($x_ratio * $height) < $max_height)
{
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else
{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
if ($ext == "gif")
{
$fp = fopen($image_path_original,"rb");
$result = fread($fp,13);
$this[m_signature] = mb_substr($result,0,3);
$this[m_version] = mb_substr($result,3,3);
$this[m_width] = ord(mb_substr($result,6,1)) + ord(mb_substr($result,7,1)) * 256;
$this[m_height] = ord(mb_substr($result,8,1)) + ord(mb_substr($result,9,1)) * 256;
$this[m_colorFlag] = ord(mb_substr($result,10,1)) >> 7;
$this[m_background] = ord(mb_substr($result,11));
if($this[m_colorFlag])
{
$tableSizeNeeded = ($this[m_background] + 1) * 3;
$result = fread($fp, $tableSizeNeeded);
$this[m_transparentRed] = ord(mb_substr($result,$this[m_background] * 3,1));
$this[m_transparentGreen] = ord(mb_substr($result,$this[m_background] * 3 + 1,1));
$this[m_transparentBlue] = ord(mb_substr($result,$this[m_background] * 3 + 2,1));
}
fclose($fp);
$src = imagecreatefromgif($image_path_original);
$dst = imagecreate($tn_width, $tn_height);
if($this[m_version] == '89a' && $this[m_colorFlag] == 1)
{
$transparent = imagecolorallocate($src, $this[m_transparentRed], $this[m_transparentGreen], $this[m_transparentBlue]);
imagecolortransparent ($src, $transparent);
}
$transparent = imagecolorallocate($dst, 0, 0, 0);
imagecolortransparent ($dst, $transparent);
}
else
{
$src = ImageCreateFromJpeg($image_path_original);
$dst = imagecreatetruecolor($tn_width,$tn_height);
}
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height);
if ($ext == "gif")
{
imagegif($dst, $image_path_save);
}
else
imagejpeg($dst, $image_path_save, 80);
ImageDestroy($src);
ImageDestroy($dst);
}