Solved

take whitespace out of an image to #ffffff -#eeeeee

Posted on 2010-08-16
7
405 Views
Last Modified: 2012-05-10
this takes the whitespace out
from
around an image

#ffffff

but can you do
#ffffff - #eeeeee

because it is not working for some images that have (offwhite)
//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);

Open in new window

0
Comment
Question by:rgb192
  • 4
  • 3
7 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 33448999
What you really want to know is: Are each of the RGB values of the color at this location above 0xEE

btw: imagecolorat returns a index to lookup the color at that location

imagecolorsforindex returns an array RGB of that indexed color.

use something like this
$this_pixel = imagecolorsforindex(imagecolorat($img, $x, $b_top));
if ($this_pixel['red'] > 0xEE and $this_pixel['green'] > 0xEE and $this_pixel['blue'] > 0xEE)
{
  // It's almost white
}

Open in new window

0
 

Author Comment

by:rgb192
ID: 33449108
i understand what you are saying
but how can I put that in the code
0
 
LVL 19

Expert Comment

by:Michael701
ID: 33449183

//top

for($b_top=0; $b_top < imagesy($img); ++$b_top) 

  {

  for($x = 0; $x < imagesx($img); ++$x) 

    {

    $this_pixel = imagecolorsforindex(imagecolorat($img, $x, $b_top));

    if ($this_pixel['red'] < 0xEE or $this_pixel['green'] < 0xEE or $this_pixel['blue'] < 0xEE)

       break 2; //out of the 'top' loop

    }

  }

}

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:rgb192
ID: 33449368
Wrong parameter count for imagecolorsforindex()

no change to image
0
 

Author Comment

by:rgb192
ID: 33449412
0
 
LVL 19

Accepted Solution

by:
Michael701 earned 500 total points
ID: 33450560
oops,

$this_pixel = imagecolorsforindex($img, imagecolorat($img, $x, $b_top));
0
 

Author Closing Comment

by:rgb192
ID: 33451040
thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now