问答详情
源自:4-3 GD库实现图片缩略图—保存和销毁图片

缩略图如何不变形

举例,如果原图是500x300, 缩略图是100x100, 如何让缩略图不变形,显示的是等比缩放后的局部;

提问者:尼古拉斯邓 2016-03-11 23:06

个回答

  • hei_hu
    2016-03-12 00:35:04


    $src = "1.jpg";
    $info = getimagesize($src);
    $type = image_type_to_extension($info[2],false);
    $fun = "imagecreatefrom{$type}";
    $image = $fun($src);

    $target = imagecreatetruecolor(300,300);
    imagecopy($target,$image,0,0,0,0,300,300);

    $image_thumb = imagecreatetruecolor(100,100);
    imagecopyresampled($image_thumb,$image,0,0,0,0,100,100,$info[0],$info[1]);
    imagedestroy($image);
    $image = $image_thumb;

    header("Content-type: image/jpeg");
    $funs = "image{$type}";
    $funs($image_thumb);