Solved

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

Posted on 2010-08-16
7
415 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
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.

617 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