How to use PHP to adjust image resolution and format conversion

PHPz
Release: 2023-08-18 21:48:02
Original
1532 people have browsed it

How to use PHP to adjust image resolution and format conversion

How to use PHP to realize image resolution adjustment and format conversion

Introduction:
In website development, we often encounter the need to adjust image resolution and format The need to convert image formats. This article will introduce how to use PHP to implement these functions.

  1. Adjust image resolution

Adjusting image resolution can help us optimize page loading speed and reduce bandwidth consumption. The following is a sample code for adjusting image resolution using the PHP GD library:

<?php
function resizeImage($sourceImage, $targetImage, $newWidth, $newHeight) {
    list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceImage);
    $sourceType = image_type_to_mime_type($sourceType);
    $sourceGdImage = false;
    switch ($sourceType) {
        case 'image/jpeg':
            $sourceGdImage = imagecreatefromjpeg($sourceImage);
            break;
        case 'image/png':
            $sourceGdImage = imagecreatefrompng($sourceImage);
            break;
        case 'image/gif':
            $sourceGdImage = imagecreatefromgif($sourceImage);
            break;
        default:
            return false;
    }
    if (!$sourceGdImage) {
        return false;
    }
    $targetGdImage = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($targetGdImage, $sourceGdImage, 0, 0, 0, 0, $newWidth, $newHeight, $sourceWidth, $sourceHeight);
    imagejpeg($targetGdImage, $targetImage);
    imagedestroy($sourceGdImage);
    imagedestroy($targetGdImage);
    return true;
}

// 调用示例
$sourceImage = 'source.jpg';
$targetImage = 'target.jpg';
$newWidth = 800;
$newHeight = 600;
resizeImage($sourceImage, $targetImage, $newWidth, $newHeight);
?>
Copy after login

In this example, we define a resizeImage function that receives the source image path and the target image path. , the new width and height as parameters. The function first uses getimagesize to obtain the width, height and type of the source image, and creates a GD image resource based on the type. Then use imagecreatetruecolor to create the target image, use the imagecopyresampled function to copy the source image to the target image, and resize it. Finally, use imagejpeg to save the target image. Note that here we only provide support for JPEG format images. If you need to process images in other formats, you can refer to the processing methods in the sample code.

  1. Convert image format

Converting image format can help us use images in different formats in different scenarios. For example, the thumbnails of the website use JPEG format and the original images use PNG. Format. The following is a sample code for converting image formats using the PHP GD library:

<?php
function convertImageFormat($sourceImage, $targetImage, $targetFormat) {
    list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceImage);
    $sourceGdImage = false;
    switch ($sourceType) {
        case IMAGETYPE_JPEG:
            $sourceGdImage = imagecreatefromjpeg($sourceImage);
            break;
        case IMAGETYPE_PNG:
            $sourceGdImage = imagecreatefrompng($sourceImage);
            break;
        case IMAGETYPE_GIF:
            $sourceGdImage = imagecreatefromgif($sourceImage);
            break;
        default:
            return false;
    }
    if (!$sourceGdImage) {
        return false;
    }
    $targetGdImage = imagecreatetruecolor($sourceWidth, $sourceHeight);
    imagecopy($targetGdImage, $sourceGdImage, 0, 0, 0, 0, $sourceWidth, $sourceHeight);
    switch ($targetFormat) {
        case 'jpeg':
            imagejpeg($targetGdImage, $targetImage);
            break;
        case 'png':
            imagepng($targetGdImage, $targetImage);
            break;
        case 'gif':
            imagegif($targetGdImage, $targetImage);
            break;
        default:
            return false;
    }
    imagedestroy($sourceGdImage);
    imagedestroy($targetGdImage);
    return true;
}

// 调用示例
$sourceImage = 'source.jpg';
$targetImage = 'target.png';
$targetFormat = 'png';
convertImageFormat($sourceImage, $targetImage, $targetFormat);
?>
Copy after login

In this example, we define a convertImageFormat function that receives the source image path, target image path and Target format as parameter. The function first uses getimagesize to obtain the width, height and type of the source image, and uses the corresponding imagecreatefrom function to create GD image resources according to different image types. Then use imagecreatetruecolor to create the target image, and use imagecopy to copy the source image to the target image. Finally, according to the target format, different output functions are called to save the target image.

Conclusion:
By using the PHP GD library, we can easily achieve resolution adjustment and format conversion of images. The above sample code can be used as a reference to adjust and expand according to actual needs. Hope this article can be helpful to you.

The above is the detailed content of How to use PHP to adjust image resolution and format conversion. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!