将一张图片粘贴到另一张图片时出现问题 PHP
Problem pasting one image into another PHP
我想把$original图片粘贴到$fondo图片的中间,我写了下面的代码:
<?php
header('Content-Type: image/png');
// The file
$fondo = imagecreate(1000,1000); // Create 1000x1000 image
$color_fondo = imagecolorallocate($fondo,197,237,206); // Set color of background
$original = imagecreatefromstring(file_get_contents('test.jpg')); // Load image
$wb = imagesx($fondo); // Bakground width
$wi = imagesx($original); // Image width
$hb = imagesy($fondo);
$hi = imagesy($original);
//Want to center in the middle of the image, so calc ($wb/2-$wi/2)
imagecopy($fondo,$original,($wb/2-$wi/2),($hb/2-$hi/2),0,0,imagesx($original),imagesy($original));
imagepng($fondo);
我得到的结果是这样的:
如您所见,青色正在影响原始图像:
对我的错误有什么看法吗?谢谢!
解决方法是:
将图像创建为 imagecreatetruecolor 而不是 imagecreate
$fondo = imagecreatetruecolor(1000,1000); // Create 1000x1000 image
$color_fondo = imagecolorallocate($fondo,197,237,206); //Desired color
然后,需要用
imagefill($fondo,0,0,$color_fondo);
此外,使用此功能裁剪为圆形
function circulo ($original,$radio){
$src = imagecreatefromstring(file_get_contents($original));
$w = imagesx($src);
$h = imagesy($src);
$newpic = imagecreatetruecolor($w,$h);
imagealphablending($newpic,false);
$transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127); //Hacer transparente la imagen
$r=$w/$radio; //Radio del circulo
for($x=0;$x<$w;$x++)
for($y=0;$y<$h;$y++){
$c = imagecolorat($src,$x,$y);
$_x = $x - $w/2;
// echo $_x."\n";
$_y = $y - $h/2;
if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
imagesetpixel($newpic,$x,$y,$c);
}else{
imagesetpixel($newpic,$x,$y,$transparent);
}
}
return $newpic;
}
结果是:
我想把$original图片粘贴到$fondo图片的中间,我写了下面的代码:
<?php
header('Content-Type: image/png');
// The file
$fondo = imagecreate(1000,1000); // Create 1000x1000 image
$color_fondo = imagecolorallocate($fondo,197,237,206); // Set color of background
$original = imagecreatefromstring(file_get_contents('test.jpg')); // Load image
$wb = imagesx($fondo); // Bakground width
$wi = imagesx($original); // Image width
$hb = imagesy($fondo);
$hi = imagesy($original);
//Want to center in the middle of the image, so calc ($wb/2-$wi/2)
imagecopy($fondo,$original,($wb/2-$wi/2),($hb/2-$hi/2),0,0,imagesx($original),imagesy($original));
imagepng($fondo);
我得到的结果是这样的:
如您所见,青色正在影响原始图像:
对我的错误有什么看法吗?谢谢!
解决方法是:
将图像创建为 imagecreatetruecolor 而不是 imagecreate
$fondo = imagecreatetruecolor(1000,1000); // Create 1000x1000 image
$color_fondo = imagecolorallocate($fondo,197,237,206); //Desired color
然后,需要用 imagefill($fondo,0,0,$color_fondo);
此外,使用此功能裁剪为圆形
function circulo ($original,$radio){
$src = imagecreatefromstring(file_get_contents($original));
$w = imagesx($src);
$h = imagesy($src);
$newpic = imagecreatetruecolor($w,$h);
imagealphablending($newpic,false);
$transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127); //Hacer transparente la imagen
$r=$w/$radio; //Radio del circulo
for($x=0;$x<$w;$x++)
for($y=0;$y<$h;$y++){
$c = imagecolorat($src,$x,$y);
$_x = $x - $w/2;
// echo $_x."\n";
$_y = $y - $h/2;
if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
imagesetpixel($newpic,$x,$y,$c);
}else{
imagesetpixel($newpic,$x,$y,$transparent);
}
}
return $newpic;
}
结果是: