2

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

我不关心像素化图像。

举以下例子

在此处输入图像描述

我知道这是可行的,但我一直在计算 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;

?>
4

1 回答 1

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;
于 2012-07-02T21:58:38.463 回答