Solved

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

Posted on 2010-08-16
7
414 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
Industry Leaders: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
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.

752 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