note 97836 modified in function.imagecopy by danbrown
| From: | danbrown@php.net | Date: | Tue, 11 May 2010 17:48:04 +0000 |
| Subject: | note 97836 modified in function.imagecopy by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169152@lists.php.net to get a copy of this message | ||
There is function to crop blank edges from image.
<?php
/**
* $image image cursor (from imagecreatetruecolor)
* $backgound image curosr (from imagecolorallocate)
* $paddng int
*/
function imageCrop($image, $background = false, $padding = 0) {
if($background)
$background = imagecolorallocate($image, 255, 255, 255);
$top = imageSY($image);
$left = imageSX($image);
$bottom = 0;
$right = 0;
for ($x = 0 ; $x < imagesx($image) ; $x++) {
for ($y = 0 ; $y < imagesy($image) ; $y++) {
// if there match
if(imagecolorat($image, $x, $y) != $background) {
if($x < $left)
$left = $x;
if($x > $right)
$right = $x;
if($y > $bottom)
$bottom = $y;
if($y < $top)
$top = $y;
}
}
}
$right++;
$bottom++;
// create new image with padding
$img = imagecreatetruecolor($right-$left+$padding*2,$bottom-$top+$padding*2);
// fill the background
imagefill($img, 0, 0, $background);
// copy
imagecopy($img, $image, $padding, $padding, $left, $top, $right-$left, $bottom-$top);
// destroy old image cursor
imagedestroy($image);
return $img;
}
?>
--was--
There is function to crop blank edges from image.
/**
* $image image cursor (from imagecreatetruecolor)
* $backgound image curosr (from imagecolorallocate)
* $paddng int
*/
function imageCrop($image, $background = false, $padding = 0) {
if($background)
$background = imagecolorallocate($image, 255, 255, 255);
$top = imageSY($image);
$left = imageSX($image);
$bottom = 0;
$right = 0;
for ($x = 0 ; $x < imagesx($image) ; $x++) {
for ($y = 0 ; $y < imagesy($image) ; $y++) {
// if there match
if(imagecolorat($image, $x, $y) != $background) {
if($x < $left)
$left = $x;
if($x > $right)
$right = $x;
if($y > $bottom)
$bottom = $y;
if($y < $top)
$top = $y;
}
}
}
$right++;
$bottom++;
// create new image with padding
$img = imagecreatetruecolor($right-$left+$padding*2,$bottom-$top+$padding*2);
// fill the background
imagefill($img, 0, 0, $background);
// copy
imagecopy($img, $image, $padding, $padding, $left, $top, $right-$left, $bottom-$top);
// destroy old image cursor
imagedestroy($image);
return $img;
}
http://php.net/manual/en/function.imagecopy.php