php GD为图像添加填充 [英] php GD add padding to image

查看:172
本文介绍了php GD为图像添加填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上发现了一些关于图像操作的关于PHP + GD的内容,但似乎没有一个能给我我想要的东西。

I have found a few things on the web about PHP+GD regarding image manipulation, but none seem to give me what I am looking for.

我有人上传任何尺寸的图像,我写的脚本调整图像大小不超过200像素宽,200像素高,同时保持宽高比。因此,最终图像可以是150像素×200像素。我想要做的是然后进一步操作图像并在图像周围添加一个垫子,将其填充到200px到200px,同时不影响原始图像。例如:

I have someone upload an image of any dimensions, the script I have written resizes the image to no more than 200px wide by 200px high while maintaining aspect ratio. So final image could be 150px by 200px for example. What I want to do is then manipulate the image further and add a matting around the image to pad it to 200px by 200px while not affecting the original image. For example:

我需要调整图像大小的代码在这里,我已经尝试了一些方法,但是在实现添加填充的辅助过程中肯定存在问题。

The code I have to get the image resized is here, I have tried a few things, but am definitely having an issue implementing the secondary process of adding the padding.

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);

我想我需要使用 imagecopy() imagecopyresampled()调用之后。那样图像已经是我想要的尺寸,我只需要创建一个200 x 200的图像并将$ newImage粘贴到其中心(vert和horiz)。我是否需要创建一个全新的图像并合并这两个图像,或者是否有办法填充我已经创建的图像( $ newImage )?在此先感谢,我发现的所有教程都让我无处可去,而我在SO上找到的唯一适用的是android:(

I am thinking I need to use imagecopy() right after the imagecopyresampled() call. That way the image is already the size I want, I just need to create an image exactly 200 x 200 and paste $newImage into the center (vert and horiz) of that. Do I need to create an entirely new image and merge the two, or is there a way to just pad the image I alread have created ($newImage)? Thanks in advance, all the tutorials I have found have led me nowhere, and the only applicable one I found on SO was for android :(

推荐答案


  1. 打开原始图像

  2. 创建一个新的空白图像。

  3. 用背景填充新图像您想要的颜色

  4. 使用ImageCopyResampled调整大小并复制以新图像为中心的原始图像

  5. 保存新图像

  1. Open the original image
  2. Create a new blank image.
  3. Fill the new image with the background colour your want
  4. Use ImageCopyResampled to resize&copy the original image centered onto the new image
  5. Save the new image

您也可以使用

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

这将自动检测到图片类型(如果您的安装支持图片类型)

This will auto detect the image type (if the imagetype is supported by your install)

更新代码示例

$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
$offest_x = ($output_w - $new_w) / 2;
$offest_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, $offest_x, $offest_y, 0, 0, $new_w, $new_h, $orig_w, $orig_h);

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

这篇关于php GD为图像添加填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆