Scale Image Using PHP and Maintaining Aspect Ratio

前端 未结 10 1986
孤独总比滥情好
孤独总比滥情好 2020-11-30 07:38

Basically I want to upload an image (which i\'ve sorted) and scale it down to certain constraints such as max width and height but maintain the aspect ratio of the original

10条回答
  •  甜味超标
    2020-11-30 08:16

    Here is a comprehensive application that I worked hard on it to include most common operations like scale up & scale down, thumbnail, preserve aspect ratio, convert file type, change quality/file size and more...

     $old_y){
            $new_x    =   $newWidth;
            $new_y    =   $old_y / $old_x * $newWidth;
        } elseif($old_x < $old_y){
            $new_y    =   $newHeight;
            $new_x    =   $old_x / $old_y * $newHeight;
        } elseif($old_x == $old_y){
            $new_x    =   $newWidth;
            $new_y    =   $newHeight;
        }
    } elseif ($newWidth){
        $new_x    =   $newWidth;
        $new_y    =   $old_y / $old_x * $newWidth;
    } elseif ($newHeight){
        $new_y    =   $newHeight;
        $new_x    =   $old_x / $old_y * $newHeight;
    } else {
        $new_x    =   $old_x;
        $new_y    =   $old_y;
    }
    
    $dst_img = ImageCreateTrueColor($new_x, $new_y);
    
    if ($palette_8bit){//////// Reduce to 8bit - 256 colors ////////
        $transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127); 
        imagecolortransparent($dst_img, $transparent);
        imagefill($dst_img, 0, 0, $transparent);
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_x,$new_y,$old_x,$old_y);// Great quality resize.
        imagetruecolortopalette($dst_img, false, 255);
        imagesavealpha($dst_img, true);
    } else {
        // Check image and set transparent for png or white background for jpg
        if ($dst_type == 'png'){
            imagealphablending($dst_img, false);
            imagesavealpha($dst_img, true);
            $transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
            imagefilledrectangle($dst_img, 0, 0, $new_x, $new_y, $transparent);
        } elseif ($dst_type == 'jpg'){
            $white = imagecolorallocate($dst_img, 255, 255, 255);
            imagefilledrectangle($dst_img, 0, 0, $new_x, $new_y, $white);
        }
    
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_x,$new_y,$old_x,$old_y);// Great quality resize.
    }
    
    // Skip the save to parameter using NULL, then set the quality; imagejpeg($dst_img);=> Default quality
    if ($dst_file){
        if ($dst_type == 'png'){
            imagepng($dst_img, $dst_file, $quality);
        } elseif ($dst_type == 'jpg'){
            imagejpeg($dst_img, $dst_file, $quality);
        }
    } else {
        header('Content-Disposition: Attachment;filename=' . $dst_name . '.' . $dst_type);// comment this line to show image in browser instead of download
        header('Content-type: ' . $dst_content);
        if ($dst_type == 'png'){
            imagepng($dst_img, NULL, $quality);
        } elseif ($dst_type == 'jpg'){
            imagejpeg($dst_img, NULL, $quality);
        }
    }
    imagedestroy($src_img);
    imagedestroy($dst_img);
    //##// END : Resize image (Scale Up & Down) (thumbnail, bigger image, preserve aspect ratio) END //##//
    

提交回复
热议问题