GD库的方形缩略图功能

时间:2014-12-07 23:56:45

标签: php

我在一个生成缩略图的函数中得到了这段代码。

我的问题是我似乎无法弄清楚如何制作缩略图正方形(250x250)。

我的代码是:

function make_thumb($img_name,$filename,$new_w,$new_h)
{
$ext=getExtension($img_name);

if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
$src_img=imagecreatefromjpeg($img_name);

if(!strcmp("png",$ext))
$src_img=imagecreatefrompng($img_name);

$old_w = imagesx($src_img);
$old_h = imagesy($src_img);

$original_aspect = $old_w / $old_h;
$thumb_aspect = $new_w / $new_h;

if ( $original_aspect >= $thumb_aspect )
{
   $thumb_h = $new_h;
   $thumb_w = $old_w / ($old_h / $new_h);
}
else
{
   $thumb_w = $new_w;
   $thumb_h = $old_h / ($old_w / $new_w);
}

$dst_img = imagecreatetruecolor( $thumb_w, $thumb_h );

imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_w,$old_h);

if(!strcmp("png",$ext))
   imagepng($dst_img,$filename);
else
   imagejpeg($dst_img,$filename);

imagedestroy($dst_img);
imagedestroy($src_img);
}

我似乎无法理解如何在保持图像居中/调整大小的同时裁剪方形图像。

有没有人有想法或能指出我正确的方向?我看了整个S / O和Google:/

0 个答案:

没有答案