欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php+js实现裁剪任意形状图片

程序员文章站 2023-12-06 08:23:22
最近做到相关的项目,由于项目使用html2canvas,但是不支持css mask属性,故,利用php后台来裁剪。 准备两张图片,一张是镂空png图案,一张是任意纯色图片...

最近做到相关的项目,由于项目使用html2canvas,但是不支持css mask属性,故,利用php后台来裁剪。

准备两张图片,一张是镂空png图案,一张是任意纯色图片。

便能够在纯色图片上裁剪出镂空的图案为png文件。

见下图。

首先两张png图片:

php+js实现裁剪任意形状图片php+js实现裁剪任意形状图片

生成图片

php+js实现裁剪任意形状图片

 js片段:

html2canvas($(".head1pic"), {
     onrendered: function(canvas) { 
     url = canvas.todataurl("image/png", 1.0);
       sourcepic = "assets/images/demo.png";
       maskpic = "assets/images/jinmao.png";
          croppicname = "cropdog1";
          // ajax php截图
          $.ajax({
          type: 'post',
           url: 'getpicture',
          data: {
            "sourcepic": sourcepic,
            "maskpic": maskpic,
            "croppicname": croppicname
            },
         success: function(data) {
             $(".page2bg")[0].setattribute("src", "assets/images/crop/cropdog1.png"); 
            },
         error: function(data) {
              console.log(data)
              }
            });
       }
 });

php的片段:

 public function actiongetpicture()
  {  
     $request = yii::$app->request;
    $sourcepic=$request->post('sourcepic');
    $maskpic=$request->post('maskpic'); 
    $croppicname=$request->post('croppicname'); 
 
    // $sourcepic="http://bings.local.com/bi_ngs2_2/assets/images/yinpian1/page2bg4.png";
    // $maskpic="http://bings.local.com/bi_ngs2_2/assets/images/jinmao.png";
    $source = imagecreatefrompng( $sourcepic );
    $mask = imagecreatefrompng( $maskpic); 
    // apply mask to source
    // imagealphamask( $source, $mask );
    $this->imagealphamask ($source, $mask );
    // output
    header( "content-type: image/png"); 
    // 生成截取后的图片并保存在本地
    imagepng( $source,"assets/images/crop/".$croppicname.".png" );
    //销毁图片内存
    imagedestroy($source);
      
  }
 
  public function imagealphamask( &$picture, $mask ) {
    // get sizes and set up new picture
    $xsize = imagesx( $picture );
    $ysize = imagesy( $picture );
    $newpicture = imagecreatetruecolor( $xsize, $ysize );
    imagesavealpha( $newpicture, true );
    imagefill( $newpicture, 0, 0, imagecolorallocatealpha( $newpicture, 100, 100, 0, 127 ) );
 
    // resize mask if necessary
    // if( $xsize != imagesx( $mask ) || $ysize != imagesy( $mask ) ) {
    //   $temppic = imagecreatetruecolor( $xsize, $ysize );
    //   imagecopyresampled( $temppic, $mask, 0, 0, 0, 0, $xsize, $ysize, imagesx( $mask ), imagesy( $mask ) );
    //   imagedestroy( $mask );
    //   $mask = $temppic;
    // }
 
    // perform pixel-based alpha map application
    for( $x = 0; $x < $xsize; $x++ ) {
      for( $y = 0; $y < $ysize; $y++ ) {
        $alpha = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) );
        //small mod to extract alpha, if using a black(transparent) and white
        //mask file instead change the following line back to jules's original:
        // $alpha = 127 - floor($alpha['black'] / 2);
        //or a white(transparent) and black mask file:
        // $alpha = floor($alpha['black'] / 2);
        $alpha = $alpha['alpha'];
        $color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) );
        //preserve alpha by comparing the two values
        if ($color['alpha'] > $alpha)
          $alpha = $color['alpha'];
        //kill data for fully transparent pixels
        if ($alpha == 127) {
          $color['red'] = 0;
          $color['blue'] = 0;
          $color['green'] = 0;
        }
        imagesetpixel( $newpicture, $x, $y, imagecolorallocatealpha( $newpicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) );
      }
    }
 
    // copy back to original picture
    imagedestroy( $picture );
    $picture = $newpicture;
  } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。