我有一个简短的问题,我不太确定要设置.我在其他地方见过例子但没有特别喜欢我的情况.我想使用PHP调整图像大小,因此它们是可读的,而不仅仅是像你使用HTML一样令人兴奋.如果它们不是250像素宽,或160像素高,我怎样才能调整图片大小以使其成比例但适合该空间?
谢谢!
PHP不直接操作图像.您将需要使用图像处理库(如gd或ImageMagick)来实现此目标.
在ImageMagick中,图像大小调整是这样完成的:
$thumb = new Imagick('myimage.gif');
$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
Run Code Online (Sandbox Code Playgroud)
使用GD,你可以这样做:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
Run Code Online (Sandbox Code Playgroud)