首页 > 作文

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

更新时间:2023-04-06 16:51:16 阅读: 评论:0

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

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

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

见下图。

首先两张png图片:

生成图片

js片段:

html2canvas($(".head1pic"), {     onrendered: function(canvas) {      url = canvas.todataurl("image/png", 1.0);       sourcepic = "asts/images/demo.png";       maskpic = "asts/images/jinmao.png";          croppicname = "cropdog1";          // ajax php截图          $.ajax({          type: 'post',           url: 'getpicture',          data: {            "sourcepic": sourcepic,            "maskpic": maskpic,            "croppicname": croppicname            },         success: function(data) {             $(".page2bg")[0].tattribute("src", "asts/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/asts/images/yinpian1/page2bg4.png";    // $maskpic="http://bings.local.com/bi_ngs2_2/asts/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,"asts/images/crop/".$croppicname.".png" );    //销毁图片内存    imagedestroy($source);        }   public function imagealphamask( &$picture, $mask ) {    // get sizes and t 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, images立春诗词x( $mask ), imagesy( $m雨霖铃柳永ask ) );    //   imagedestroy( $mask );    //   $mask = $temppic;    // }     // perform pi华丽的xel-bad 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( $pic校园小品剧本搞笑ture, imagecolorat( $picture, $x, $y ) );        //prerve 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;        }        imagetpixel( $newpicture, $x, $y, imagecolorallocatealpha( $newpicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) );      }    }     // copy back to original picture    imagedestroy( $picture );    $picture = $newpicture;  } 

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

本文发布于:2023-04-06 16:50:49,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/ad31adba96c17a309b1e86f9713e30a7.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:php+js实现裁剪任意形状图片.doc

本文 PDF 下载地址:php+js实现裁剪任意形状图片.pdf

标签:图片   纯色   两张   片段
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图