如何在 PHP 中调整图片大小?
可以使用 ImageMagick 或 GD 函数来调整图像大小。如果使用 GD 的函数,那么对原始数码相机图像进行采样时也会减小图像文件大小。我们将在下面的代码中看到如何使用 GD 来调整图像大小。
function image_resize($file_name, $width, $height, $crop=FALSE) {
list($wid, $ht) = getimagesize($file_name);
$r = $wid / $ht;
if ($crop) {
if ($wid > $ht) {
$wid = ceil($wid-($width*abs($r-$width/$height)));
} else {
$ht = ceil($ht-($ht*abs($r-$w/$h)));
}
$new_width = $width;
$new_height = $height;
} else {
if ($width/$height > $r) {
$new_width = $height*$r;
$new_height = $height;
} else {
$new_height = $width/$r;
$new_width = $width;
}
}
$source = imagecreatefromjpeg($file_name);
$dst = imagecreatetruecolor($new_width, $new_height);
image_copy_resampled($dst, $source, 0, 0, 0, 0, $new_width, $new_height, $wid, $ht);
return $dst;
}
$img_to_resize = image_resize(‘path-to-jpg-image’, 250, 250);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
JavaScript
PHP