十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍了php+js如何实现裁剪任意形状图片,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
成都创新互联2013年至今,先为凤台等服务建站,凤台等地企业,进行企业商务咨询服务。为凤台企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。1、能够嵌入动态文本于HTML页面。2、对浏览器事件做出响应。3、读写HTML元素。4、在数据被提交到服务器之前验证数据。5、检测访客的浏览器信息。6、控制cookies,包括创建和修改等。7、基于Node.js技术进行服务器端编程。
最近做到相关的项目,由于项目使用html2canvas,但是不支持css mask属性,故,利用php后台来裁剪。
准备两张图片,一张是镂空PNG图案,一张是任意纯色图片。
便能够在纯色图片上裁剪出镂空的图案为PNG文件。
见下图。
首先两张PNG图片:
生成图片
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如何实现裁剪任意形状图片”这篇文章对大家有帮助,同时也希望大家多多支持创新互联网站建设公司,,关注创新互联行业资讯频道,更多相关知识等着你来学习!