从上传创建最大x x y图像,保持比例和居中

时间:2012-07-02 18:59:00

标签: php image file-upload gd

我正在尝试使用GD库创建一个脚本,该脚本将采用任意大小的上传图像,并创建最大的x大拇指,保持它的比例,如果图像太小,它会变得最大它的x / y比率并将其放大,使其保持居中。

我并不担心像素化像素。

采取以下示例

enter image description here

我知道这是可行的,但我不得不计算x / y坐标

这是我到目前为止的地方。我正在尝试创建具有定义尺寸的3个拇指

<?php

    $sizes = array(
        array(
            'width' => 640,
            'height' => 360
        ),
        array(
            'width' => 222,
            'height' => 166
        ),
        array(
            'width' => 140,
            'height' => 105
        )
    );

    if (isset($_FILES['image'])) {

        $file = $_FILES['image'];

        foreach($sizes as $size) {

            if (strpos($file['type'], 'jpeg') !== false || strpos($file['type'], 'jpg') !== false) {

                $resource = imagecreatefromjpeg($file['tmp_name']);
            }
            else if (strpos($file['type'], 'png') !== false) {

                $resource = imagecreatefrompng($file['tmp_name']);
            }
            else if (strpos($file['type'], 'gif') !== false) {

                $resource = imagecreatefromgif($file['tmp_name']);
            }
            else {

                echo "bad file type " . $file['type'];
                exit;
            }

            list($width, $height) = getimagesize($file['tmp_name']);

            $tmpImage = imagecreatetruecolor($size['width'], $size['height']);

            /*

            need to do some calculations here

            */

            imagecopyresampled($tmpImage, $resource, 0, 0, 0, 0, $width, $height, $size['width'], $size['height']);

            ob_start();
            imagepng($tmpImage);
            $image = ob_get_clean();

            echo '<img src="data:image/png;base64,' . base64_encode($image) . '" />';

            imagedestroy($tmpImage);
        }

        var_dump($file, $width, $height);
    }

    exit;

?>

1 个答案:

答案 0 :(得分:2)

供您考虑:

<?php

$sizes = array(
    array(
        'width' => 640,
        'height' => 360
    ),
    array(
        'width' => 222, 
        'height' => 166
    ),
    array(
        'width' => 140,
        'height' => 105
    )
);

if (isset($_FILES['image'])) {

    $file = $_FILES['image'];

    foreach($sizes as $size) {

        if (strpos($file['type'], 'jpeg') !== false || strpos($file['type'], 'jpg') !== false) {

            $original_image = imagecreatefromjpeg($file['tmp_name']);
        }
        else if (strpos($file['type'], 'png') !== false) {

            $original_image = imagecreatefrompng($file['tmp_name']);
        }
        else if (strpos($file['type'], 'gif') !== false) {

            $original_image = imagecreatefromgif($file['tmp_name']);
        }
        else {

            echo "bad file type " . $file['type'];
            exit;
        }

        $thumbnail_image = imagecreatetruecolor($size['width'], $size['height']);

        $original_size = getimagesize($file['tmp_name']);
        $original_width = $original_size[0];
        $original_height = $original_size[1];
        $adjusted_original_width = $original_size[0];
        $adjusted_original_height = $original_size[1];

        $fitting = false;
        $original_aspect_ratio = $original_width/$original_height; // larger is wider
        $ideal_thumbnail_aspect_ratio = $size['width']/$size['height']; // larger is wider
        if($original_aspect_ratio > $ideal_thumbnail_aspect_ratio) $fitting = 'wide'; // the original is wider than the ideal
        else if($original_aspect_ratio < $ideal_thumbnail_aspect_ratio) $fitting = 'tall'; // the original is taller than the ideal

        $src_x = 0;
        $src_y = 0;

        if($fitting=='wide') {
            $adjusted_original_width = $original_height * $size['width']/$size['height'];
            $src_x = ($original_width-$adjusted_original_width)/2;
        }
        else {
            $adjusted_original_height = $original_width * $size['height']/$size['width'];
            $src_y = ($original_height-$adjusted_original_height)/2;
        }

        $thumbnail_image = imagecreatetruecolor($size['width'], $size['height']);

        imagecopyresampled($thumbnail_image, $original_image, 0, 0, $src_x, $src_y, $size['width'], $size['height'], $adjusted_original_width, $adjusted_original_height);

        ob_start();
        imagepng($thumbnail_image);
        $image = ob_get_clean();

        echo '<img src="data:image/png;base64,' . base64_encode($image) . '" />';

        imagedestroy($thumbnail_image);
    }

    var_dump($file, $original_width, $original_height);
}

exit;