简体   繁体   English

PHP 图像调整大小 - 自动设置高度和宽度

[英]PHP Image Resize - Set Height & Width Auto

Cant get image to resize with the following code.无法使用以下代码调整图像大小。

Want to set a height of 300px and width of auto for all uploads.想要为所有上传设置 300px 的高度和 auto 的宽度。

Currently setting is to 100 x 100 just for testing purposes.当前设置为 100 x 100 仅用于测试目的。 This does nothing on the upload.这对上传没有任何作用。

 // Escape user inputs for security $id = mysqli_real_escape_string($link, $_POST['id']); $target_dir = "images/breweryimages/"; $size = getimagesize($_FILES["fileToUpload"]["name"]); $base_file = rand(100000000000,10000000000000) . "-" . basename($_FILES["fileToUpload"]["name"]); $target_file = $target_dir . $base_file; $image = getimagesize($_FILES['fileToUpload']); $file_width=$image[0]; $file_height=$image[1]; $file_width = 100; $file_height = 100; $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 50000000000) { echo "Sorry, your file is too large."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["fileToUpload"]["name"]) . " " . $file_width . "has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } }

Hopefully this will suit your needs希望这能满足您的需求

static public function square_crop($src_image, $dest_image, $dst_w = 100, $dst_h = 100, $jpg_quality = 90) {

    // Get dimensions of existing image
    $image = getimagesize($src_image);

    // Check for valid dimensions
    if( $image[0] <= 0 || $image[1] <= 0 ) return false;

    // Determine format from MIME-Type
    $image['format'] = strtolower(preg_replace('/^.*?\//', '', $image['mime']));

    // Import image
    switch( $image['format'] ) {
        case 'jpg':
        case 'jpeg':
            $image_data = imagecreatefromjpeg($src_image);
        break;
        case 'png':
            $image_data = imagecreatefrompng($src_image);
        break;
        case 'gif':
            $image_data = imagecreatefromgif($src_image);
        break;
        default:
            // Unsupported format
            return false;
        break;
    }

    // Verify import
    if( $image_data == false ) return false;

    // Calculate measurements
    if( $image[0] & $image[1] ) {
        // For landscape images
        $x_offset = ($image[0] - $image[1]) / 2;
        $y_offset = 0;
        $square_size = $image[0] - ($x_offset * 2);
    } else {
        // For portrait and square images
        $x_offset = 0;
        $y_offset = ($image[1] - $image[0]) / 2;
        $square_size = $image[1] - ($y_offset * 2);
    }

    // Resize and crop
    $canvas = imagecreatetruecolor($dst_w, $dst_h);
    if( imagecopyresampled(
        $canvas,
        $image_data,
        0,
        0,
        $x_offset,
        $y_offset,
        $dst_w,
        $dst_h,
        $square_size,
        $square_size
    )) {

        // Create thumbnail
        switch( strtolower(preg_replace('/^.*\./', '', $dest_image)) ) {
            case 'jpg':
            case 'jpeg':
                return imagejpeg($canvas, $dest_image, $jpg_quality);
            break;
            case 'png':
                return imagepng($canvas, $dest_image);
            break;
            case 'gif':
                return imagegif($canvas, $dest_image);
            break;
            default:
                // Unsupported format
                return false;
            break;
        }

    } else {
        return false;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM