Crop Whitespace from Image in PHP

Crop whitespace from image in PHP

To trim all whitespace, as you call it, surrounding the interesting part of the image, first we find out where the "whitespace" stops, and then we copy everything inside of those borders.

//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;

//top
for(; $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
}
}
}

//bottom
for(; $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
}
}
}

//left
for(; $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
}
}
}

//right
for(; $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 image
header("Content-Type: image/jpeg");
imagejpeg($newimg);

My old example, that assumes an identical "border" on all sides of the image, just to clarify the comments :)

//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 code 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 image
imagejpeg($newimg, "img.jpg");

trim whitespace around PNG in PHP

This is a totally untested guess. However, I would assume that alpha transparency is causing the issue. The answer that you mentioned might need a bit of work.

Perhaps try using imagecopymerge() to lay the image over a white canvas, and then go through the steps mentioned in the answer.

Crop darkspace from photo in PHP

So i went ahead and played around with IDEA#1 and it is working exceptionally well. I am all set and need no further help at the moment...though still think that IDEA #2 is a better all-around solution, so if anyone wants to provide some pointers for it, i will play around with the ideas and try to come up with additional code examples to post here. For now, here is my working version of IDEA#1 as described above and tested on several cell-phone images:

//MANY THANKS TO: http://stackoverflow.com/questions/1669683/crop-whitespace-from-image-in-php    

$OriginalImageFileLocation = 'sampleimage.jpg';

//load the image into a variable (and throw an error if image does not exist)
if( !$img = imagecreatefromjpeg("$OriginalImageFileLocation") )
{ exit("Could not use image $OriginalImageFileLocation"); }

// This function only can detect the background if it is "pure black"...so convert to greyscale then crank the contrast
imagefilter($img, IMG_FILTER_GRAYSCALE);
imagefilter($img, IMG_FILTER_CONTRAST, -100);

//find the size of the borders
$b_top = 0;
$b_btm = 0;
$b_lft = 0;
$b_rt = 0;

//top
for(; $b_top < imagesy($img); ++$b_top) {
for($x = 0; $x < imagesx($img); ++$x) {
if(imagecolorat($img, $x, $b_top) != 0x000000) {
break 2; //out of the 'top' loop
}
}
}

//bottom
for(; $b_btm < imagesy($img); ++$b_btm) {
for($x = 0; $x < imagesx($img); ++$x) {
if(imagecolorat($img, $x, imagesy($img) - $b_btm-1) != 0x000000) {
break 2; //out of the 'bottom' loop
}
}
}

//left
for(; $b_lft < imagesx($img); ++$b_lft) {
for($y = 0; $y < imagesy($img); ++$y) {
if(imagecolorat($img, $b_lft, $y) != 0x000000) {
break 2; //out of the 'left' loop
}
}
}

//right
for(; $b_rt < imagesx($img); ++$b_rt) {
for($y = 0; $y < imagesy($img); ++$y) {
if(imagecolorat($img, imagesx($img) - $b_rt-1, $y) != 0x000000) {
break 2; //out of the 'right' loop
}
}
}

//now re-initialize the original image since the greyscaled version was just for gathering cordinates
$img = imagecreatefromjpeg("$OriginalImageFileLocation");

// OPTIONAL: make the final image a bit prettier (and greyscale for long-term consistency)
imagefilter($img, IMG_FILTER_GRAYSCALE);
imagefilter($img, IMG_FILTER_BRIGHTNESS, +15);
imagefilter($img, IMG_FILTER_CONTRAST, -20);

//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 image
header("Content-Type: image/jpeg");
imagejpeg($newimg);

How can I trim white space off image with Intervention?

trim() returns the trimmed image, but you are doing nothing with the return value. Try calling save() to persist the image to the filesystem:

Image::make($this->tempFileName)->trim('top-left', null, 60)->save();
// ^^^^^^

resize / crop image and add white space

Using Class upload 0.32 from voret.net instead - this does what I want!

Trim whitespace from an image then resize image with 'padding'

This Photoshop script will do what you want. Pleas bear in mind that the trim function only works of the bottom right or top left pixel colour. This won't work if that border area is "dirty"

var srcDoc = app.activeDocument;

//trim image to transparent width
srcDoc.trim(TrimType.TOPLEFT, true, true, true, true);

var w = srcDoc.width.value;
var h = srcDoc.height.value;

if (w<h)
{
var imageWidth = 380;
var imageHeight = Math.floor(w*(380/h))
}
else
{
var imageHeight = 380;
var imageWidth = Math.floor(h*(380/w))
}

var resizeRes = 72
var resizeMethod = ResampleMethod.BICUBIC;

//resize
srcDoc.resizeImage(imageWidth, imageHeight, resizeRes, resizeMethod)

// adjust canvas size
srcDoc.resizeCanvas(400, 400, AnchorPosition.MIDDLECENTER);


Related Topics



Leave a reply



Submit