0

我需要上传一个图像文件,然后创建一个更宽的图像,并在其中调整原始大小。看互联网,我发现了一些可以做到这一点的东西:

    function resize_image($source_image, $destination_width, $destination_height, $type = 2) {
    // $type (1=crop to fit, 2=letterbox)
    $source_width = imagesx($source_image);
    $source_height = imagesy($source_image);
    $source_ratio = $source_width / $source_height;
    $destination_ratio = $destination_width / $destination_height;
    if ($type == 1) {
        // crop to fit
        if ($source_ratio > $destination_ratio) {
            // source has a wider ratio
            $temp_width = (int)($source_height * $destination_ratio);
            $temp_height = $source_height;
            $source_x = (int)(($source_width - $temp_width) / 2);
            $source_y = 0;
        } else {
            // source has a taller ratio
            $temp_width = $source_width;
            $temp_height = (int)($source_width / $destination_ratio);
            $source_x = 0;
            $source_y = (int)(($source_height - $temp_height) / 2);
        }
        $destination_x = 0;
        $destination_y = 0;
        $source_width = $temp_width;
        $source_height = $temp_height;
        $new_destination_width = $destination_width;
        $new_destination_height = $destination_height;
    } else {
        // letterbox
        if ($source_ratio < $destination_ratio) {
            // source has a taller ratio
            $temp_width = (int)($destination_height * $source_ratio);
            $temp_height = $destination_height;
            $destination_x = (int)(($destination_width - $temp_width) / 2);
            $destination_y = 0;
        } else {
            // source has a wider ratio
            $temp_width = $destination_width;
            $temp_height = (int)($destination_width / $source_ratio);
            $destination_x = 0;
            $destination_y = (int)(($destination_height - $temp_height) / 2);
        }
        $source_x = 0;
        $source_y = 0;
        $new_destination_width = $temp_width;
        $new_destination_height = $temp_height;
    }
    $destination_image = imagecreatetruecolor($destination_width, $destination_height);
    if ($type > 1) {
        imagefill($destination_image, 0, 0, imagecolorallocate ($destination_image, 255, 255, 255));
    }
    imagecopyresampled($destination_image, $source_image, $destination_x, $destination_y, $source_x, $source_y, $new_destination_width, $new_destination_height, $source_width, $source_height);
    return $destination_image;
}

我上传图片后调用这个函数

/// Set the working path
$destination_path = getcwd().DIRECTORY_SEPARATOR;
// 
$result = 0;

/// Upload File   
    $target_path = $destination_path . 'img'.DIRECTORY_SEPARATOR . basename( $_FILES['myFile']['name']);

if(move_uploaded_file($_FILES['myFile']['tmp_name'], $target_path)) {

// If it's all ok I set the destination path for the wider image
// 
$pathDestinazione = $destination_path . $_POST['pathRidimensionata'] . basename($_FILES['myFile']['name']);

// collect some infos about the source file
list($width, $height, $type, $attr) = getimagesize($target_path);

// Set the desidered dimension 
$desideredWidth = $_POST['width'];
$desideredHeight = $_POST['height'];

//I begin to create the new image 

$thumb = resize_image($target_path,$desideredWidth,$desideredHeight,2);   
imagejpeg($thumb, $pathDestinazione, 60);

//if everything it's ok
$result = 1;
}else{
    $result = $_FILES['myFile']['error'];
    }

但结果总是一个白色的图像......我不明白是错误的,但我真的是 php 新手!

4

1 回答 1

0

您的函数有参数 $source_image - 这是图像句柄类型。这应该是函数 imagecreatefromjpeg 或其他(gif、png)的结果。但是你调用函数并作为字符串传递给文件名。

您应该从上传的文件创建图像,然后调用函数。

于 2013-02-22T16:05:18.487 回答