note 101064 added to function.imagecolorat
| From: | josevc at gmail dot com | Date: | Wed, 24 Nov 2010 04:19:18 +0000 |
| Subject: | note 101064 added to function.imagecolorat | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174308@lists.php.net to get a copy of this message | ||
I made some corrections to the function created by adtrader117 adding 5 chanels in the scanning to
avoid errors in the border detection of images that have black areas in the center, here is the
code:
<?
function trim_borders($path,$path2){
/* The pad var is essentially a 'feather' value. Unless one of the RGB values rises
* above $pad we are saying to continue. You can try different values but with 10 I
* would still get a decent sized border due to the bleedover of the movie into
* the hat.
*/
$pad = 20;
$border_y = 0;
$border_x = 0;
if(!file_exists($path)) {
if(DEBUG_OUT) echo("Error: $path not found.\n");
return FALSE;
}else{
//if(DEBUG_OUT) echo("Opening: $path ...\n");
}
$im = @imagecreatefromjpeg($path);
if(!$im) return FALSE;
$height = imagesy($im);
$width = imagesx($im);
/* Let's start at 0, 0 and keep going till we hit a color */
//if(DEBUG_OUT) echo("Image - Height: $height / Width: $width\n");
/* Border Height(Y) */
$spacebetsamp = ceil($width/6);
$sample1 = $spacebetsamp;
$sample2 = $spacebetsamp*2;
$sample3 = $spacebetsamp*3;
$sample4 = $spacebetsamp*4;
$sample5 = $spacebetsamp*5;
for($i=0; $i<$height; $i++){
$rgb1 = imagecolorat($im,$sample1,$i);
$r1 = ($rgb1 >> 16) & 0xFF;
$g1 = ($rgb1 >> 8) & 0xFF;
$b1 = $rgb1 & 0xFF;
$rgb2 = imagecolorat($im,$sample2,$i);
$r2 = ($rgb2 >> 16) & 0xFF;
$g2 = ($rgb2 >> 8) & 0xFF;
$b2 = $rgb2 & 0xFF;
...
$blackpixels=0;
if($r1 < $pad and $g1 < $pad and $b1 < $pad) $blackpixels++;
if($r2 < $pad and $g2 < $pad and $b2 < $pad) $blackpixels++;
if($r3 < $pad and $g3 < $pad and $b3 < $pad) $blackpixels++;
if($r4 < $pad and $g4 < $pad and $b4 < $pad) $blackpixels++;
if($r5 < $pad and $g5 < $pad and $b5 < $pad) $blackpixels++;
if($blackpixels != 5){
$border_y = $i;
if($border_y == $height) $border_y = 0;
break;
}else{
//echo "blackpixel: s1($r1,$g1,$b1); s2($r2,$g2,$b2); s3($r3,$g3,$b3);
s4($r4,$g4,$b4);s5($r5,$g5,$b5);<br>";
}
}
/* Border Width(X) */
$spacebetsamp = ceil($height/6);
$sample1 = $spacebetsamp;
$sample2 = $spacebetsamp*2;
$sample3 = $spacebetsamp*3;
$sample4 = $spacebetsamp*4;
$sample5 = $spacebetsamp*5;
for($i=0; $i<$width; $i++){
$rgb1 = imagecolorat($im,$i,$sample1);
$r1 = ($rgb1 >> 16) & 0xFF;
$g1 = ($rgb1 >> 8) & 0xFF;
$b1 = $rgb1 & 0xFF;
...
$blackpixels=0;
if($r1 < $pad and $g1 < $pad and $b1 < $pad) $blackpixels++;
if($r2 < $pad and $g2 < $pad and $b2 < $pad) $blackpixels++;
if($r3 < $pad and $g3 < $pad and $b3 < $pad) $blackpixels++;
if($r4 < $pad and $g4 < $pad and $b4 < $pad) $blackpixels++;
if($r5 < $pad and $g5 < $pad and $b5 < $pad) $blackpixels++;
if($blackpixels != 5){
$border_x = $i;
if($border_x == $width) $border_x = 0;
break;
}
}
/* I am making the border a multiple of 2 since I am sending these values to FFMpeg */
if($border_x != 0){
$border_x /= 2;
$border_x = round($border_x);
$border_x *= 2;
}
if($border_y != 0){
$border_y /= 2;
$border_y = round($border_y);
$border_y *= 2;
}
//if(DEBUG_OUT) echo("Border Width: $border_x / Border Height: $border_y\n");
if($border_x+$border_y >0){
if($border_x){
$sx = $border_x+1;
$dw = $width-($border_x*2);
}else{
$sx =0;
$dw =$width;
}
if($border_y){
$sy = $border_y+1;
$dh = $height-($border_y*2);
}else{
$sy =0;
$dh = $height;
}
$dest = imagecreatetruecolor($dw,$dh);
// Copy
imagecopy($dest, $im, 0, 0, $sx, $sy, $dw, $dh);
imagejpeg($dest,$path2);
}
return array($border_x,$border_y);
}
?>
Sorry for my english
----
Server IP: 69.147.83.197
Probable Submitter: 189.194.221.150
----
Manual Page -- http://www.php.net/manual/en/function.imagecolorat.php
Edit -- https://master.php.net/note/edit/101064
Del: integrated -- https://master.php.net/note/delete/101064/integrated
Del: useless -- https://master.php.net/note/delete/101064/useless
Del: bad code -- https://master.php.net/note/delete/101064/bad+code
Del: spam -- https://master.php.net/note/delete/101064/spam
Del: non-english -- https://master.php.net/note/delete/101064/non-english
Del: in docs -- https://master.php.net/note/delete/101064/in+docs
Del: other reasons-- https://master.php.net/note/delete/101064
Reject -- https://master.php.net/note/reject/101064
Search -- https://master.php.net/manage/user-notes.php