php 在不损失质量的情况下调整图像大小

php resize image without loss quality

我已经实现了这个方法(通过遵循 php 教程)来创建图像预览:

function createPreview($image_path, $filename) {

    header('Content-Type: image/jpeg');
    $thumb = imagecreatetruecolor(350, 350);
    $source = imagecreatefromjpeg($image_path);

    list($width, $height) = getimagesize($image_path);

    imagecopyresized($thumb, $source, 0, 0, 0, 0, 350, 350, $width, $height);

    imagejpeg($thumb, $filename."_prev.jpg");
}

但我注意到缩放后的图像质量下降了很多。如何保持缩放图像的质量(我不能使用 imagick,我的服务器不支持它)

imagejpeg 默认使用 75 质量。所以你需要明确定义它。

imagejpeg($thumb, $filename."_prev.jpg", 100);

另外,使用imagecopyresampled

imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.

使用PHP Image Magician 调整图像大小 http://phpimagemagician.jarrodoberto.com/

使用 imagecopyresampled 而不是 imagecopyresized(参数相同,只需更改函数即可):)

下面的 PHP 代码可以在不损失质量的情况下调整图像大小,并且调整后的图像将根据纵横比适应上述调整大小的宽度和高度。

从一个文件夹 (img) 读取图像并将同一图像调整到所需宽度和高度的代码到另一个提到的文件夹 (resizedImage)。

希望对您有所帮助。

谢谢

<?php
function resizeImage($SrcImage,$DestImage, $thumb_width,$thumb_height,$Quality)
{
    list($width,$height,$type) = getimagesize($SrcImage);
    switch(strtolower(image_type_to_mime_type($type)))
    {
        case 'image/gif':
            $NewImage = imagecreatefromgif($SrcImage);
            break;
        case 'image/png':
            $NewImage = imagecreatefrompng($SrcImage);
            break;
        case 'image/jpeg':
            $NewImage = imagecreatefromjpeg($SrcImage);
            break;
        default:
            return false;
            break;
    }
    $original_aspect = $width / $height;
    $positionwidth = 0;
    $positionheight = 0;
    if($original_aspect > 1)    {
        $new_width = $thumb_width;
        $new_height = $new_width/$original_aspect;
        while($new_height > $thumb_height) {
            $new_height = $new_height - 0.001111;
            $new_width  = $new_height * $original_aspect;
            while($new_width > $thumb_width) {
                $new_width = $new_width - 0.001111;
                $new_height = $new_width/$original_aspect;
            }

        }
    } else {
        $new_height = $thumb_height;
        $new_width = $new_height/$original_aspect;
        while($new_width > $thumb_width) {
            $new_width = $new_width - 0.001111;
            $new_height = $new_width/$original_aspect;
            while($new_height > $thumb_height) {
                $new_height = $new_height - 0.001111;
                $new_width  = $new_height * $original_aspect;
            }
        }
    }
    if($width < $new_width && $height < $new_height){
        $new_width = $width;
        $new_height = $height;
        $positionwidth = ($thumb_width - $new_width) / 2;
        $positionheight = ($thumb_height - $new_height) / 2;
    }elseif($width < $new_width && $height > $new_height){
        $new_width = $width;
        $positionwidth = ($thumb_width - $new_width) / 2;
        $positionheight = 0;
    }elseif($width > $new_width && $height < $new_height){
        $new_height = $height;
        $positionwidth = 0;
        $positionheight = ($thumb_height - $new_height) / 2;
    } elseif($width > $new_width && $height > $new_height){
        if($new_width < $thumb_width) {
            $positionwidth = ($thumb_width - $new_width) / 2;
        } elseif($new_height < $thumb_height) {
            $positionheight = ($thumb_height - $new_height) / 2;
        }
    }
    $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
    /********************* FOR WHITE BACKGROUND  *************************/
        //$white = imagecolorallocate($thumb, 255,255,255);
        //imagefill($thumb, 0, 0, $white);
    if(imagecopyresampled($thumb, $NewImage,$positionwidth, $positionheight,0, 0, $new_width, $new_height, $width, $height)) {
        if(imagejpeg($thumb,$DestImage,$Quality)) {
            imagedestroy($thumb);
            return true;
        }
    }
}
function resize($source,$destination,$newWidth,$newHeight)
{
    ini_set('max_execution_time', 0);
    $ImagesDirectory = $source;
    $DestImagesDirectory = $destination;
    $NewImageWidth = $newWidth;
    $NewImageHeight = $newHeight;
    $Quality = 100;
    $imagePath = $ImagesDirectory;
    $destPath = $DestImagesDirectory;
    $checkValidImage = getimagesize($imagePath);
    if(file_exists($imagePath) && $checkValidImage)
    {
        if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
            echo " --> ".$source.'  --> Resize Successful!<BR><BR>';
        else
            echo " --> ".$source.'  --> Resize Failed!<BR><BR>';
    }
}
function getDirContents($filter = '', &$results = array()) {
    // Source FOLDER
    $files = scandir($_SERVER['DOCUMENT_ROOT'].'/imageresize/img/');
    $fileCount = 1;
    foreach($files as $key => $value){
        $ext = explode(".",$value);
        $fname = $ext[0].round(microtime(true)*1000);
        $filename = $fname.".".$ext[1];
        // Source PATH
        $path = realpath($_SERVER['DOCUMENT_ROOT'].'/imageresize/img/'.$value); 

        if(!is_dir($path)) {
            if(empty($filter) || preg_match($filter, $path)){ 
                echo "Image # ".$fileCount;
                $results[] = $path;
                // Destination PATH
                $destination = $_SERVER['DOCUMENT_ROOT'].'/imageresize/resizedImage/'.$value;

                // Change the desired "WIDTH" and "HEIGHT"
                $newWidth = 400; // Desired WIDTH
                $newHeight = 350; // Desired HEIGHT

                resize($path,$destination,$newWidth,$newHeight);
                $fileCount++;
            }
        } elseif($value != "." && $value != "..") {
            getDirContents($path, $filter, $results);
        }
    }
    return $results;
} 
getDirContents();
exit;
?>