php resize image on upload

后端 未结 12 1475
别跟我提以往
别跟我提以往 2020-11-27 04:09

I got a form where the user is inserting some data and also uploading an image.

To deal with the image, I got the following code:

define(\"MAX_SIZE\"         


        
12条回答
  •  感动是毒
    2020-11-27 04:21

    // This was my example that I used to automatically resize every inserted photo to 100 by 50 pixel and image format to jpeg hope this helps too

    if($result){
    $maxDimW = 100;
    $maxDimH = 50;
    list($width, $height, $type, $attr) = getimagesize( $_FILES['photo']['tmp_name'] );
    if ( $width > $maxDimW || $height > $maxDimH ) {
        $target_filename = $_FILES['photo']['tmp_name'];
        $fn = $_FILES['photo']['tmp_name'];
        $size = getimagesize( $fn );
        $ratio = $size[0]/$size[1]; // width/height
        if( $ratio > 1) {
            $width = $maxDimW;
            $height = $maxDimH/$ratio;
        } else {
            $width = $maxDimW*$ratio;
            $height = $maxDimH;
        }
        $src = imagecreatefromstring(file_get_contents($fn));
        $dst = imagecreatetruecolor( $width, $height );
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1] );
    
        imagejpeg($dst, $target_filename); // adjust format as needed
    
    
    }
    
    move_uploaded_file($_FILES['pdf']['tmp_name'],"pdf/".$_FILES['pdf']['name']);
    

提交回复
热议问题