所以我继续尝试使用 IDEA#1,它运行得非常好。我已经准备好了,目前不需要进一步的帮助......虽然仍然认为 IDEA #2 是一个更好的全方位解决方案,所以如果有人想为它提供一些指示,我会尝试这些想法并尝试想出额外的代码示例发布在这里。现在,这是我上面描述的 IDEA#1 的工作版本,并在几个手机图像上进行了测试:
//MANY THANKS TO: http://stackoverflow.com/questions/1669683/crop-whitespace-from-image-in-php
$OriginalImageFileLocation = 'sampleimage.jpg';
//load the image into a variable (and throw an error if image does not exist)
if( !$img = imagecreatefromjpeg("$OriginalImageFileLocation") )
{ exit("Could not use image $OriginalImageFileLocation"); }
// This function only can detect the background if it is "pure black"...so convert to greyscale then crank the contrast
imagefilter($img, IMG_FILTER_GRAYSCALE);
imagefilter($img, IMG_FILTER_CONTRAST, -100);
//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) != 0x000000) {
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) != 0x000000) {
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) != 0x000000) {
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) != 0x000000) {
break 2; //out of the 'right' loop
}
}
}
//now re-initialize the original image since the greyscaled version was just for gathering cordinates
$img = imagecreatefromjpeg("$OriginalImageFileLocation");
// OPTIONAL: make the final image a bit prettier (and greyscale for long-term consistency)
imagefilter($img, IMG_FILTER_GRAYSCALE);
imagefilter($img, IMG_FILTER_BRIGHTNESS, +15);
imagefilter($img, IMG_FILTER_CONTRAST, -20);
//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);