php resize image on upload

// resize on upload
$maxDim = 800;
$file_name = $_FILES['myFile']['tmp_name'];
list($width, $height, $type, $attr) = getimagesize( $file_name );
if ( $width > $maxDim || $height > $maxDim ) {
    $target_filename = $file_name;
    $ratio = $width/$height;
    if( $ratio > 1) {
        $new_width = $maxDim;
        $new_height = $maxDim/$ratio;
    } else {
        $new_width = $maxDim*$ratio;
        $new_height = $maxDim;
    }
    $src = imagecreatefromstring( file_get_contents( $file_name ) );
    $dst = imagecreatetruecolor( $new_width, $new_height );
    imagecopyresampled( $dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
    imagedestroy( $src );
    imagepng( $dst, $target_filename ); // adjust format as needed
    imagedestroy( $dst );
}

3.89
9
Awgiedawgie 440220 points

                                    $maxDim = 800;
$file_name = $_FILES['myFile']['tmp_name'];
list($width, $height, $type, $attr) = getimagesize( $file_name );
if ( $width > $maxDim || $height > $maxDim ) {
    $target_filename = $file_name;
    $ratio = $width/$height;
    if( $ratio > 1) {
        $new_width = $maxDim;
        $new_height = $maxDim/$ratio;
    } else {
        $new_width = $maxDim*$ratio;
        $new_height = $maxDim;
    }
    $src = imagecreatefromstring( file_get_contents( $file_name ) );
    $dst = imagecreatetruecolor( $new_width, $new_height );
    imagecopyresampled( $dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
    imagedestroy( $src );
    imagepng( $dst, $target_filename ); // adjust format as needed
    imagedestroy( $dst );
}

3.89 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
resize image from user in php php create image resize resizing image with php php image file resize how to resize image with php how to resize images with php howto resize images with php php correct way to resize uploaded image imageresize php resizing an image in a database php resizing image using php before uploading php resize image before display resizing image in php resize image before upload php php image downsizer resize image with php how to increase image upload size in php resize image php before upload php photo resize imageResize in php resize image upload php new Image Resize php resize image php library resize image php class php scale image resising image in php image resize php example resize image php function resize image file with php php resize imgs php resize image for web upload image with resize php how image reszie in php php resize im image upload file size php how i can resize the image in php php change image size on upload why file upload change photo size php upload image with resize in php php on upload resize and compress image php images resize and image resizer in php resize image using php interventation php fileupload resize image resize_image php resize image php script php send resized image how to resize an image in php on upload php upload image dimensions resize upload image php img resize php php image resize and upload nulled php image resize and upload using img-resize php php image resize library php imageresize resize image using php image resize php php resize image to file size store resize image php resize image resolution with code php resize image php with content resize image php file upload resize image php how to resize image in php before upload php script to get and resize image php image resize function resize php image resize image in php persent image resizer php class image resizer php resize image before upload in php how to resize image in php package image resize in php php upload resize image code php image resize image resize function for php function imgae resize function for php function image resize in php resize image in php png resize image in php image upload with resize in php resize images at upload php how to set upload image size in php how to shrink size of image after upload in php php resize image php resize image on upload
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source