0

我正在尝试使用此类:http ://www.verot.net/php_class_upload_samples.htm

如果有人熟悉调整大小和填充图像的其余部分,请帮助我。

这个想法是,如果你上传一张 600x600 的图片,我想保存两张图片:1-600x600 和 1-300x300。到目前为止,它工作得很好。但是,如果用户选择上传大小为 749x1202 的图片,我希望脚本使其在 300x300 和 600x600 中看起来不错,其中无用的空间被白色填充,因此图像仍然是 600x600。对于较小的图像(例如 249x400)也应如此。

到目前为止,这是我的代码:

// Set the upload directory
$uploadDir = '../../htdocs/public/product_images/'.$id.'/';
$thumbDir = '../../htdocs/public/product_images/'.$id.'/thumbs/';

@mkdir($uploadDir, 0755, true);
@mkdir($thumbDir, 0755, true);


// Store the file content in a variable
$file = file_get_contents('php://input');

// Save the file to the server
file_put_contents($uploadDir . $filename, $file);

$targetFile =  str_replace('//','/',$uploadDir) . $filename;
$targetThumb = str_replace('//','/',$thumbDir) . $filename;
copy ($targetFile,$targetThumb);


$image = new upload($targetFile);
if ($image->uploaded) {
    $image->image_resize          = true;
    $image->image_ratio_fill      = true;
    $image->image_x               = 600;
    $image->image_y               = 600;
    $image->file_overwrite        = true;
    $image->process($uploadDir);
} 

$image = new upload($targetThumb);
if ($image->uploaded) {
    $image->image_resize          = true;
    $image->image_ratio_fill      = true;
    $image->image_x               = 300;
    $image->image_y               = 300;
    $image->file_overwrite        = true;
    $image->process($thumbDir);
}

我可以用另一种方式做到这一点,但如果有人能指导我一点,我将非常感激。

4

1 回答 1

0

您可以做的是在保持纵横比的情况下缩放图像,因此至少有一个尺寸符合您的要求(即您获得 128x300),然后创建空图像(画布)并将这两个图像合并在画布上居中。但是我只需将这个 128x300 像素的图像写在磁盘上,并在 HTML/CSS 中强制执行 300x300 像素的容器大小 - 这会更好,因为您的文件更小,并且您可以通过更改 CSS 轻松更改画布的即 bg 颜色 - 使用以前的技术,如果 bg 不透明(如果是,那么为什么不使用 HTML)就不会那么容易了?

于 2012-11-22T13:43:52.397 回答