Solved

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

Posted on 2010-08-16
7
411 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

786 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