PHP GD缩放图像

时间:2015-08-29 20:56:02

标签: php resize gd aspect-ratio

我正在尝试使用GD,我正在尝试使用大图像。我想要一张最初为640x640的图像在我正在GD中创建的图像上调整为130x130。但是,使用我的代码,它只会从左上角裁剪130x130的图像。换句话说,我没有在130x130中获得整个图像。我一直在尝试我能找到的每个片段,但仍然没有运气来控制它。这是我的代码;

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demoApp" ng-controller="mainController as ctrl">
    <ul>
        <li ng-repeat="item in ctrl.items">{{item.name}}</li>
    </ul>
    <button ng-click="ctrl.changeOrder()">re-order</button>
</div>

我想将指定为$ image2Url的图像缩小到130x130,无论它原来是什么尺寸。我保持宽高比对我来说很重要。

我一直在尝试我能找到的不同片段,但仍然没有运气......我已经能够将原始图像调整到我想要的大小,但不能在我的GD脚本中的最终图像中调整。

1 个答案:

答案 0 :(得分:1)

如果您使用的是PHP版本&gt; = 5.5,则应使用imagescale()。如果没有,请在加载$image2后使用以下权限:

$image3 = imagecreatetruecolor(130,130);
list($image2w, $image2h) = getimagesize($image2Url);
imagecopyresampled($image3, $image2, 0, 0, 0, 0, 130, 130, $image2w, $image2h);

// then use $image3 instead of $image2