最近做到相关的项目,由于项目使用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;
  } 

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

标签:
php,js,裁剪

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“php+js实现裁剪任意形状图片”

暂无“php+js实现裁剪任意形状图片”评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?