note 99282 added to function.imagecolorat

From: Date: Sun, 08 Aug 2010 14:51:15 +0000
Subject: note 99282 added to function.imagecolorat
Groups: php.notes 
Request: Send a blank email to php-notes+get-171469@lists.php.net to get a copy of this message
Here's a function I made for finding the size of the black border surrounding some movie thumbnails where the movie itself has had black padding added in order to maintain aspect ratio. <?php define("DEBUG_OUT",TRUE); $border_size = find_border_size($path); print_r($border_size); /* * $border = max(find_border_size("img.jpg")); * $thumb_size_x = 180+(2*$border); * $thumb_size_y = 240+(2*$border); * $thumb_size = $thumb_size_x . "x" . $thumb_size_y; (String to use in the -s param of ffmpeg) * $crop_cmd = "-croptop $border -cropbottom $border -cropright $border -cropleft $border"; */ function find_border_size($path) { /* 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) */ $center_width = ceil($width/2); for($i=0; $i<$height; $i++) { $rgb = imagecolorat($im,$center_width,$i); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; if(DEBUG_OUT) echo("Height: ($center_width,$i) R: $r / G: $g / B: $b\n"); if($r >= $pad || $g >= $pad || $b >= $pad) { $border_y = $i; if($border_y == $height) $border_y = 0; break; } } /* Border Width(X) */ $center_height = ceil($height/2); for($i=0; $i<$width; $i++) { $rgb = imagecolorat($im,$i,$center_height); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; if(DEBUG_OUT) echo("Width: ($i,$center_width) R: $r / G: $g / B: $b\n"); if($r >= $pad || $g >= $pad || $b >= $pad) { $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"); return array($border_x,$border_y); } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 173.80.217.114 ---- Manual Page -- http://www.php.net/manual/en/function.imagecolorat.php Edit -- https://master.php.net/note/edit/99282 Del: integrated -- https://master.php.net/note/delete/99282/integrated Del: useless -- https://master.php.net/note/delete/99282/useless Del: bad code -- https://master.php.net/note/delete/99282/bad+code Del: spam -- https://master.php.net/note/delete/99282/spam Del: non-english -- https://master.php.net/note/delete/99282/non-english Del: in docs -- https://master.php.net/note/delete/99282/in+docs Del: other reasons-- https://master.php.net/note/delete/99282 Reject -- https://master.php.net/note/reject/99282 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#171469) next »