Solved

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

Posted on 2010-08-16
7
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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