如何在不更改 php 上的像素/分辨率的情况下调整文件图像大小


How te resize file image without change the pixels/resolution on php?

我有ImageManipulation.php,想用它重新调整图像大小。 重新调整大小时,它仍然会改变分辨率,如果我上传图像 2100 x 900 和 2,3MB 文件大小,在服务器上将保存 700 x 600 80KB 文件大小。
如何以相同的分辨率保存但文件大小更小?
例如:2100x900 100KB 文件。

这是我的图像压缩功能:

function imageCompression($imgfile="",$thumbsize=0,$savePath=NULL, $compressRatio=100) {
    //echo 'Lets Compression Only';
    if($savePath==NULL) {
        header('Content-type: image/jpeg');
    }
    list($width,$height)=getimagesize($imgfile);
    /* The width and the height of the image also the getimagesize retrieve other information as well   */
    $imgratio=$width/$height; 
    //if($imgratio>1) {
        $newwidth=$thumbsize;
        $newheight=$thumbsize/$imgratio;
    //} else {
    //  $newheight=$thumbsize;       
    //  $newwidth=$thumbsize*$imgratio;
    //}
    $thumb=imagecreatetruecolor($newwidth,$newheight); // Making a new true color image
    $source=imagecreatefromjpeg($imgfile); // Now it will create a new image from the source
    imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);  // Copy and resize the image
    imagejpeg($thumb,$savePath,$compressRatio); //100 --> Compression Ration Quality
    imagedestroy($thumb);
}

按照给出的代码,

<?php 
function compress($source, $destination, $quality) {
    $info = getimagesize($source);
    if ($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($source);
    elseif ($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($source);
    elseif ($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($source);
    imagejpeg($image, $destination, $quality);
    return $destination;
}
$source_img = 'source.jpg';
$destination_img = 'destination .jpg';
$d = compress($source_img, $destination_img, 90);

?>

它应该做你的工作。

请更改源和目标,它将保持相同的分辨率。

一种方法

是使用Adobe Photoshop或任何其他工具转换图像Web格式。 或者将图像更改为.png文件类型它通常更优雅地处理压缩。