PHP GD 给图像添加填充

16

我在网络上找到了一些关于PHP+GD图像处理的内容,但似乎没有给我想要的。

我让某人上传任意尺寸的图像,然后我写的脚本将该图像缩小到不超过200像素宽和200像素高,并保持长宽比。因此,最终图像可以是150像素乘以200像素,例如。现在我想进一步操纵图像,并在图像周围添加垫片,使其达到200像素乘以200像素,同时不影响原始图像。例如:

未加垫片的图像 143x200

添加垫片后的图像 200x200

这里是我用来调整图像大小的代码,我已经尝试了一些东西,但显然在实施添加垫片的第二个过程时出现了问题。

list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType);
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
switch($imageType) {
    case "image/gif":
        $source=imagecreatefromgif($image); 
        break;
    case "image/pjpeg":
    case "image/jpeg":
    case "image/jpg":
        $source=imagecreatefromjpeg($image); 
        break;
    case "image/png":
    case "image/x-png":
        $source=imagecreatefrompng($image); 
        break;
}
imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
imagejpeg($newImage,$image,80);
chmod($image, 0777);

我认为在调用imagecopyresampled()后需要使用imagecopy()。这样,图像已经是我想要的尺寸,我只需要创建一个恰好为200 x 200的图像,并将$newImage粘贴到其中心(水平和垂直居中)。我需要创建一个全新的图像并合并两个图像吗?或者是否有一种方法可以仅填充我已经创建的图像($newImage)?谢谢提前回答,我找到的所有教程都没有帮助我,我在stackoverflow上唯一适用的教程是用于安卓的:(

1个回答

20
  1. 打开原始图像
  2. 创建一个新的空白图像
  3. 用你想要的背景色填充新图像
  4. 使用ImageCopyResampled将原始图像调整大小并复制到新图像中心位置
  5. 保存新图像

除了您的switch语句,您还可以使用

$img = imagecreatefromstring( file_get_contents ("path/to/image") );

这将自动检测图像类型(如果图像类型受您的安装支持)。

附有代码示例更新。

$orig_filename = 'c:\temp\380x253.jpg';
$new_filename = 'c:\temp\test.jpg';

list($orig_w, $orig_h) = getimagesize($orig_filename);

$orig_img = imagecreatefromstring(file_get_contents($orig_filename));

$output_w = 200;
$output_h = 200;

// determine scale based on the longest edge
if ($orig_h > $orig_w) {
    $scale = $output_h/$orig_h;
} else {
    $scale = $output_w/$orig_w;
}

    // calc new image dimensions
$new_w =  $orig_w * $scale;
$new_h =  $orig_h * $scale;

echo "Scale: $scale<br />";
echo "New W: $new_w<br />";
echo "New H: $new_h<br />";

// determine offset coords so that new image is centered
$offset_x = ($output_w - $new_w) / 2;
$offset_y = ($output_h - $new_h) / 2;

    // create new image and fill with background colour
$new_img = imagecreatetruecolor($output_w, $output_h);
$bgcolor = imagecolorallocate($new_img, 255, 0, 0); // red
imagefill($new_img, 0, 0, $bgcolor); // fill background colour

    // copy and resize original image into center of new image
imagecopyresampled($new_img, $orig_img, $offset_x, $offset_y, 0, 0, $new_w, $new_h, $orig_w, $orig_h);

    //save it
imagejpeg($new_img, $new_filename, 80);

switch语句的见解很棒,那绝对比我的switch语句更高效。不过我对imagecopyresampled函数有些困惑。虽然原始图像的调整大小已经完成并且正常工作,但它只是粘贴到了一个200x200像素的背景上。我的理解是,imagecopyresampled函数会再次调整原始图像的大小? - MaurerPower
太好了!我最终使用了一个稍作修改的版本来获得我想要的确切结果,但这确实让我走上了正确的道路!感谢bumberbox!已接受并点赞! - MaurerPower
这很棒 - 只是一个快速的提示,如果您只想填充图像而不调整其大小,则可以设置 $scale = 1 - But those new buttons though..
偏移量,最棒的偏移量节庆活动! - ecv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接