Skip to content

Instantly share code, notes, and snippets.

@Yaffle
Created January 6, 2012 15:31
Show Gist options
  • Select an option

  • Save Yaffle/1571073 to your computer and use it in GitHub Desktop.

Select an option

Save Yaffle/1571073 to your computer and use it in GitHub Desktop.
было:
if($fitwidth>$originalsize[0]) $fitwidth=$originalsize[0];
if($fitheight>$originalsize[1]) $fitheight=$originalsize[1];
if(($h[0]/$fitwidth)>($h[1]/$fitheight))
{
$x=($originalsize[0] - $h[1]/$fitheight*$fitwidth) / 2;
$y=0;
//$fitheight=$h[1]*$fitwidth/$h[0];
}else{
//$fitwidth=$h[0]*$fitheight/$h[1];
$x=0;
$y=($originalsize[1] - $h[0]/$fitwidth *$fitheight) / 2;
}
//echo $fitwidth." ".$fitheight;
if($os[2]==2 or ($os[2]>=9 && $os[2]<=12))$i = ImageCreateFromJPEG($image_from);
if($os[2]==3)$i=ImageCreateFromPng($image_from);
$o = ImageCreateTrueColor($fitwidth, $fitheight);
imagecopyresampled($o, $i, 0, 0, $x, $y, $fitwidth, $fitheight, $originalsize[0]-2*$x, $originalsize[1]-2*$y);
imagejpeg($o, $image_to, $quality);
стало:
if($fitwidth>$originalsize[0]) $fitwidth=$originalsize[0];
if($fitheight>$originalsize[1]) $fitheight=$originalsize[1];
$x = 0;
$y = 0;
$fw = $fitwidth;
$fh = $fitheight;
if(($h[0]/$fitwidth)>($h[1]/$fitheight))
{
//$x=($originalsize[0] - $h[1]/$fitheight*$fitwidth) / 2;
$y=0;
$fh=$h[1]*$fw/$h[0];
}else{
$fw=$h[0]*$fh/$h[1];
$x=0;
// $y=($originalsize[1] - $h[0]/$fitwidth *$fitheight) / 2;
}
//echo $fitwidth." ".$fitheight;
if($os[2]==2 or ($os[2]>=9 && $os[2]<=12))$i = ImageCreateFromJPEG($image_from);
if($os[2]==3)$i=ImageCreateFromPng($image_from);
$o = ImageCreateTrueColor($fitwidth, $fitheight);
$backgroundColor = imagecolorallocate($o, 255, 255, 255);
imagefill($o, 0, 0, $backgroundColor);
imagecopyresampled($o, $i, ($fitwidth - $fw) / 2, ($fitheight - $fh) / 2, $x, $y, $fw, $fh, $originalsize[0]-2*$x, $originalsize[1]-2*$y);
imagejpeg($o, $image_to, $quality);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment