imagescale
使用给定的新宽度和高度缩放图像
PHP 5.5.0及以上版本。
imagescale() 函数用于调整图像的大小。它可以将图像缩放到指定的宽度和高度,保持原始图像的宽高比。
imagescale(resource $image, int $width, int $height = null, int $flags = 0): resource
成功时,返回一个包含新图像的资源。如果失败,返回 false。
以下是一个使用 imagescale() 函数的示例代码:
<?php // 载入源图像 $image = imagecreatefromjpeg('source.jpg'); <p>// 调整图像大小,宽度设置为200px,高度自动调整<br> $scaled_image = imagescale($image, 200);</p> <p>// 保存缩放后的图像<br> imagejpeg($scaled_image, 'scaled_image.jpg');</p> <p>// 销毁图像资源<br> imagedestroy($image);<br> imagedestroy($scaled_image);<br> ?><br>
首先使用 imagecreatefromjpeg() 函数载入原始图像。接着,使用 imagescale() 将图像的宽度缩放为 200 像素,并自动调整高度以保持宽高比。最后,使用 imagejpeg() 保存缩放后的图像,并销毁图像资源以释放内存。