php GD为图像添加填充
我在网上找到了一些关于PHP+GD的关于图像处理的东西,但是没有一个能提供我想要的东西 我有人上传任何尺寸的图像,我写的脚本将图像的大小调整为不超过200px宽200px高,同时保持纵横比。例如,最终图像可能是150px乘以200px。我想做的是进一步处理图像,并在图像周围添加一个垫子,将其以200px乘以200px的方式填充,同时不影响原始图像。例如: 我必须调整图像大小的代码就在这里,我尝试了一些方法,但在实现添加填充的第二个过程时肯定遇到了问题php GD为图像添加填充,php,image-processing,gd,Php,Image Processing,Gd,我在网上找到了一些关于PHP+GD的关于图像处理的东西,但是没有一个能提供我想要的东西 我有人上传任何尺寸的图像,我写的脚本将图像的大小调整为不超过200px宽200px高,同时保持纵横比。例如,最终图像可能是150px乘以200px。我想做的是进一步处理图像,并在图像周围添加一个垫子,将其以200px乘以200px的方式填充,同时不影响原始图像。例如: 我必须调整图像大小的代码就在这里,我尝试了一些方法,但在实现添加填充的第二个过程时肯定遇到了问题 list($imagewidth, $i
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
)?提前感谢,我找到的所有教程都让我一无所获,我在SO上找到的唯一适用的教程是android:(
打开原始图像
创建一个新的空白图像
用您想要的背景色填充新图像
使用ImageCopyResampled可调整原始图像的大小并将其复制到新图像的中心位置
保存新图像
除了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
$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);
$orig_filename='c:\temp\380x253.jpg';
$new_filename='c:\temp\test.jpg';
list($orig_w,$orig_h)=getimagesize($orig_filename);
$orig\U img=imagecreatefromstring(文件获取内容($orig\U文件名));
$output_w=200;
$output_h=200;
//根据最长边确定比例
如果($orig\U h>$orig\U w){
$scale=$output\u h/$orig\u h;
}否则{
$scale=$output\u w/$orig\u w;
}
//计算新图像尺寸
$new\u w=$orig\u w*$比例;
$new_h=$orig_h*$scale;
回声“标度:$Scale
”;
回声“新W:$New_W
”;
回声“新H:$New_H
”;
//确定偏移坐标,使新图像居中
$offest_x=($output_w-$new_w)/2;
$offest_y=($output_h-$new_h)/2;
//创建新图像并填充背景色
$new\u img=imagecreatetruecolor($output\w,$output\h);
$bgcolor=imagecolorallocate($new_img,255,0,0);//红色
imagefill($new_img,0,0,$bgcolor);//填充背景色
//将原始图像复制并调整大小到新图像的中心
图像复制重采样($new\u img,$orig\u img,$offest\u x,$offest\u y,0,0,$new\u w,$new\u h,$orig\u w,$orig\u h);
//省省吧
图像JPEG($new\u img,$new\u filename,80);
对switch语句的洞察非常好,这肯定比我的switch语句更有效。但我对imagecopyresampled感到困惑。原始图像的大小调整是完整的和有效的,它只是将其粘贴到200x200 px的背景上。我的理解是imagecopyresampled将调整原始图像的大小再次使用图像?效果很好!我最终使用了一点修改版本来获得我想要的准确结果,但这让我走上了正确的道路!感谢bumberbox!接受并投票!这很好-只是一个简短的提示,如果你只想填充图像,而不想调整其大小,你可以设置$scale=1
提供最大偏移量永远的节日!