栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在PHP中从图像裁剪空白

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在PHP中从图像裁剪空白

要修剪所有空白,就像您所说的那样,围绕图像的有趣部分,首先我们找出“空白”在哪里停止,然后我们复制这些边界内的所有内容。

//load the image$img = imagecreatefromjpeg("http://ecx.images-amazon.com/images/I/413XvF0yukL._SL500_AA280_.jpg");//find the size of the borders$b_top = 0;$b_btm = 0;$b_lft = 0;$b_rt = 0;//topfor(; $b_top < imagesy($img); ++$b_top) {  for($x = 0; $x < imagesx($img); ++$x) {    if(imagecolorat($img, $x, $b_top) != 0xFFFFFF) {       break 2; //out of the 'top' loop    }  }}//bottomfor(; $b_btm < imagesy($img); ++$b_btm) {  for($x = 0; $x < imagesx($img); ++$x) {    if(imagecolorat($img, $x, imagesy($img) - $b_btm-1) != 0xFFFFFF) {       break 2; //out of the 'bottom' loop    }  }}//leftfor(; $b_lft < imagesx($img); ++$b_lft) {  for($y = 0; $y < imagesy($img); ++$y) {    if(imagecolorat($img, $b_lft, $y) != 0xFFFFFF) {       break 2; //out of the 'left' loop    }  }}//rightfor(; $b_rt < imagesx($img); ++$b_rt) {  for($y = 0; $y < imagesy($img); ++$y) {    if(imagecolorat($img, imagesx($img) - $b_rt-1, $y) != 0xFFFFFF) {       break 2; //out of the 'right' loop    }  }}//copy the contents, excluding the border$newimg = imagecreatetruecolor(    imagesx($img)-($b_lft+$b_rt), imagesy($img)-($b_top+$b_btm));imagecopy($newimg, $img, 0, 0, $b_lft, $b_top, imagesx($newimg), imagesy($newimg));//finally, output the imageheader("Content-Type: image/jpeg");imagejpeg($newimg);

我的旧示例假设图像的所有侧面都具有相同的“边框”,只是为了澄清注释:)

//load the image$img = imagecreatefromjpeg("img.jpg");//find the size of the border.$border = 0;while(imagecolorat($img, $border, $border) == 0xFFFFFF) {  $border++;}//copy the contents, excluding the border//This pre assumes that the border is the same size on all sides of the image.$newimg = imagecreatetruecolor(imagesx($img)-($border*2), imagesy($img)-($border*2));imagecopy($newimg, $img, 0, 0, $border, $border, imagesx($newimg), imagesy($newimg));//finally, if you want, overwrite the original imageimagejpeg($newimg, "img.jpg");


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/395424.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号