【问题标题】:Problem pasting one image into another PHP将一个图像粘贴到另一个 PHP 时出现问题
【发布时间】:2020-11-05 06:48:57
【问题描述】:

我想把$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);

我得到的结果是这样的:

如您所见,青色正在影响原始图像:

关于我错了什么的任何想法?谢谢!

【问题讨论】:

    标签: php image png gd paste


    【解决方案1】:

    解决办法是:

    将图像创建为 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;
    }
    

    结果是:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      相关资源
      最近更新 更多