PHP 等比例压缩是指在不改变图片宽高比的情况下,通过减少图片的尺寸来减小文件大小的过程。这种压缩方法可以有效地减少图片占用的存储空间,同时保持图片内容的视觉质量。
<?php
function resizeImage($source, $destination, $width, $height) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($source);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($source);
} else {
return false;
}
$newImage = imagescale($image, $width, $height);
if ($info['mime'] == 'image/jpeg') {
imagejpeg($newImage, $destination, 80);
} elseif ($info['mime'] == 'image/png') {
imagepng($newImage, $destination, 9);
}
imagedestroy($image);
imagedestroy($newImage);
return true;
}
$source = 'path/to/source/image.jpg';
$destination = 'path/to/destination/image.jpg';
$width = 300;
$height = 200;
resizeImage($source, $destination, $width, $height);
?>
memory_limit
配置来解决。通过以上方法,可以有效地进行 PHP 等比例压缩,优化图片资源的使用。
没有搜到相关的文章