Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中合并两个图像并旋转_Php_Image_Merge_Php Gd - Fatal编程技术网

在PHP中合并两个图像并旋转

在PHP中合并两个图像并旋转,php,image,merge,php-gd,Php,Image,Merge,Php Gd,我想将第一个图像四舍五入并与第二个图像合并。 这是我想要合并的两个图像。 我要圆化的第一个图像。 我希望合并第一个图像的第二个图像 这里有一个代码,我正用于此目的 <?php $srcImg = '1.jpg'; $destImg = '2.jpg'; $dstX = 300; $dstY = 40; $srcX = 0; $srcY = 0; $srcW = 200; $srcH = 226; $pct = 100; $dest = imagecreatefromjpeg

我想将第一个图像四舍五入并与第二个图像合并。
这是我想要合并的两个图像。

我要圆化的第一个图像。
我希望合并第一个图像的第二个图像
这里有一个代码,我正用于此目的

    <?php

$srcImg = '1.jpg';
$destImg = '2.jpg';
$dstX = 300;
$dstY = 40;
$srcX = 0;
$srcY = 0;
$srcW = 200;
$srcH = 226;
$pct = 100;


$dest = imagecreatefromjpeg($destImg);
$src = imageCreateCorners($srcImg,200);

imagealphablending($dest, false);
imagesavealpha($dest, true);

imagecopymerge($dest, $src, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH, $pct); 

header('Content-Type: image/png');
imagejpeg($dest);

imagedestroy($dest);
imagedestroy($src);

 function imageCreateCorners($sourceImageFile, $radius) {
    # test source image
    if (file_exists($sourceImageFile)) {
      $res = is_array($info = getimagesize($sourceImageFile));
      } 
    else $res = false;

    # open image
    if ($res) {
      $w = $info[0];
      $h = $info[1];
      switch ($info['mime']) {
        case 'image/jpeg': $src = imagecreatefromjpeg($sourceImageFile);
          break;
        case 'image/gif': $src = imagecreatefromgif($sourceImageFile);
          break;
        case 'image/png': $src = imagecreatefrompng($sourceImageFile);
          break;
        default: 
          $res = false;
        }
      }

    # create corners
    if ($res) {

      $q = 10; # change this if you want
      $radius *= $q;

      # find unique color
      do {
        $r = rand(0, 255);
        $g = rand(0, 255);
        $b = rand(0, 255);
        }
      while (imagecolorexact($src, $r, $g, $b) < 0);

      $nw = $w*$q;
      $nh = $h*$q;

      $img = imagecreatetruecolor($nw, $nh);
      $alphacolor = imagecolorallocatealpha($img, $r, $g, $b, 127);
      imagealphablending($img, false);
      imagesavealpha($img, true);
      imagefilledrectangle($img, 0, 0, $nw, $nh, $alphacolor);

      imagefill($img, 0, 0, $alphacolor);
      imagecopyresampled($img, $src, 0, 0, 0, 0, $nw, $nh, $w, $h);

      imagearc($img, $radius-1, $radius-1, $radius*2, $radius*2, 180, 270, $alphacolor);
      imagefilltoborder($img, 0, 0, $alphacolor, $alphacolor);
      imagearc($img, $nw-$radius, $radius-1, $radius*2, $radius*2, 270, 0, $alphacolor);
      imagefilltoborder($img, $nw-1, 0, $alphacolor, $alphacolor);
      imagearc($img, $radius-1, $nh-$radius, $radius*2, $radius*2, 90, 180, $alphacolor);
      imagefilltoborder($img, 0, $nh-1, $alphacolor, $alphacolor);
      imagearc($img, $nw-$radius, $nh-$radius, $radius*2, $radius*2, 0, 90, $alphacolor);
      imagefilltoborder($img, $nw-1, $nh-1, $alphacolor, $alphacolor);
      imagealphablending($img, true);
      imagecolortransparent($img, $alphacolor);

      # resize image down
      $dest = imagecreatetruecolor($w, $h);
      imagealphablending($dest, false);
      imagesavealpha($dest, true);
      imagefilledrectangle($dest, 0, 0, $w, $h, $alphacolor);
      imagecopyresampled($dest, $img, 0, 0, 0, 0, $w, $h, $nw, $nh);

      # output image
      $res = $dest;
      imagedestroy($src);
      imagedestroy($img);
      }

    return $res;
    }
  ?>

这个代码对我来说不太好用。有三个问题。
首先,当我尝试使其圆化并增大半径时,它不会形成圆。
第二,在图像周围自动创建黑色。
第三,这个代码有两个慢。
你能帮我做得更好吗。
任何建议。

通过拆掉一个机器,我写了以下内容:

<?php

$srcImg = '1.jpg';
$destImg = '2.jpg';
$dstX = 300;
$dstY = 40;
$srcX = 0;
$srcY = 0;
//$srcW = 200; // replaced with $src_width
//$srcH = 226; // replaced with $src_height
$pct = 100;

// create destination image resource.
$dest = imagecreatefromjpeg($destImg);
$dest_width = imagesx($dest);
$dest_height = imagesy($dest);

// create source image resource and define transparent colour.
$src = imagecreatefromjpeg($srcImg);
$src_width = imagesx($src);
$src_height = imagesy($src);
imagecolortransparent($src, imagecolorallocate($src, 255, 0, 255));

// create a circular mask and use it to crop the source image.
$mask = imagecreatetruecolor($src_width, $src_height);
$black = imagecolorallocate($mask, 0, 0, 0);
$magenta = imagecolorallocate($mask, 255, 0, 255);
imagefill($mask, 0, 0, $magenta);
$r = min($src_width, $src_height);
imagefilledellipse($mask, ($src_width / 2), ($src_height / 2), $r, $r, $black);
imagecolortransparent($mask, $black);
imagecopymerge($src, $mask, 0, 0, 0, 0, $src_width, $src_height, 100);
imagedestroy($mask);

// merge the two images to create the result.
imagecopymerge($dest, $src, $dstX, $dstY, $srcX, 0, $src_width, $src_height, $pct);

// output.
header('Content-type: image/jpeg');
imagejpeg($dest);
imagedestroy($src);
imagedestroy($dest);