Resize image in PHP

前端 未结 13 1805
长情又很酷
长情又很酷 2020-11-22 04:18

I\'m wanting to write some PHP code which automatically resizes any image uploaded via a form to 147x147px, but I have no idea how to go about it (I\'m a relative PHP novice

13条回答
  •  不要未来只要你来
    2020-11-22 04:58

    (IMPORTANT: In the case of animation (animated webp or gif) resizing, the result will be a not animated, but resized image from the first frame! (The original animation remains intact...)

    I created this to my php 7.2 project (example imagebmp sure (PHP 7 >= 7.2.0) :php/manual/function.imagebmp) about techfry.com/php-tutorial, with GD2, (so nothing 3rd party library) and very similar to the answer of Nico Bistolfi, but works with the all five basic image mimetype (png, jpeg, webp, bmp and gif), creating a new resized file, without modifying the original one, and the all stuff in one function and ready to use (copy and paste to your project). (You can set the extension of the new file with the fifth parameter, or just leave it, if you want keep the orignal):

    function createResizedImage(
        string $imagePath = '',
        string $newPath = '',
        int $newWidth = 0,
        int $newHeight = 0,
        string $outExt = 'DEFAULT'
    ) : ?string
    {
        if (!$newPath or !file_exists ($imagePath)) {
            return null;
        }
    
        $types = [IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_BMP, IMAGETYPE_WEBP];
        $type = exif_imagetype ($imagePath);
    
        if (!in_array ($type, $types)) {
            return null;
        }
    
        list ($width, $height) = getimagesize ($imagePath);
    
        $outBool = in_array ($outExt, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']);
    
        switch ($type) {
            case IMAGETYPE_JPEG:
                $image = imagecreatefromjpeg ($imagePath);
                if (!$outBool) $outExt = 'jpg';
                break;
            case IMAGETYPE_PNG:
                $image = imagecreatefrompng ($imagePath);
                if (!$outBool) $outExt = 'png';
                break;
            case IMAGETYPE_GIF:
                $image = imagecreatefromgif ($imagePath);
                if (!$outBool) $outExt = 'gif';
                break;
            case IMAGETYPE_BMP:
                $image = imagecreatefrombmp ($imagePath);
                if (!$outBool) $outExt = 'bmp';
                break;
            case IMAGETYPE_WEBP:
                $image = imagecreatefromwebp ($imagePath);
                if (!$outBool) $outExt = 'webp';
        }
    
        $newImage = imagecreatetruecolor ($newWidth, $newHeight);
    
        //TRANSPARENT BACKGROUND
        $color = imagecolorallocatealpha ($newImage, 0, 0, 0, 127); //fill transparent back
        imagefill ($newImage, 0, 0, $color);
        imagesavealpha ($newImage, true);
    
        //ROUTINE
        imagecopyresampled ($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    
        // Rotate image on iOS
        if(function_exists('exif_read_data') && $exif = exif_read_data($imagePath, 'IFD0'))
        {
            if(isset($exif['Orientation']) && isset($exif['Make']) && !empty($exif['Orientation']) && preg_match('/(apple|ios|iphone)/i', $exif['Make'])) {
                switch($exif['Orientation']) {
                    case 8:
                        if ($width > $height) $newImage = imagerotate($newImage,90,0);
                        break;
                    case 3:
                        $newImage = imagerotate($newImage,180,0);
                        break;
                    case 6:
                        $newImage = imagerotate($newImage,-90,0);
                        break;
                }
            }
        }
    
        switch (true) {
            case in_array ($outExt, ['jpg', 'jpeg']): $success = imagejpeg ($newImage, $newPath);
                break;
            case $outExt === 'png': $success = imagepng ($newImage, $newPath);
                break;
            case $outExt === 'gif': $success = imagegif ($newImage, $newPath);
                break;
            case  $outExt === 'bmp': $success = imagebmp ($newImage, $newPath);
                break;
            case  $outExt === 'webp': $success = imagewebp ($newImage, $newPath);
        }
    
        if (!$success) {
            return null;
        }
    
        return $newPath;
    }
    

提交回复
热议问题