PHP 图像通过拉伸调整为正好 256 x 256 像素


PHP image resize to exactly 256 x 256 pixels with stretching

我想将图像大小调整为正好 256 x 256 像素。

我目前正在使用...

$image_p = imagecreatetruecolor($imgMaxWidth, $imgMaxHeight);
imagecopyresampled($image_p, $source, 0, 0, 0, 0, $imgMaxWidth, $imgMaxHeight, $imgWidth, $imgHeight);

它只接受Maximum WidthMaximum Height

如果我上传 615 x 339 像素的图像,我将获得一个调整为 256 x 141 像素的图像。

你可以帮我吗?

我不知道

你在做什么,但你的代码应该可以工作

我已经试过了,它有效:

<?php
// The file
$filename = 'test.jpg';
$source = imagecreatefromjpeg($filename);
// Set your height and width
$imgMaxWidth = 256;
$imgMaxHeight = 256;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($imgWidth, $imgHeight) = getimagesize($filename);
// This is the code you post
$image = imagecreatetruecolor($imgMaxWidth, $imgMaxHeight);
imagecopyresampled($image, $source, 0, 0, 0, 0, $imgMaxWidth, $imgMaxHeight, $imgWidth, $imgHeight);
// Output
imagejpeg($image, null, 100);
?>