Can php remove the background of JPG images and add watermarks?

Source: Internet
Author: User
Tags imagejpeg transparent color
Found this section from the Internet, but I do not run it, and this is for PNG and GIF format
$fnew = "A.png";
$img = file_get_contents ($fnew);
$im = imagecreatefromstring ($img);
$BG = Imagecolorat ($im, 0, 0);
Imagecolorset ($im, $BG, 0, 0, 255);
Imagepng ($im);
Imagedestroy ($im);
Do you have a good way? Thank you


Reply to discussion (solution)


If this picture, I want to turn the back of the various blue background into white, and then the above the overall reduction of the apple pattern can be reduced on the net to move a bit, I can add the bottom of the black text watermark, the pattern upward is because the water supply printing position, do not block the text, thank you.

It's possible to add watermarks.

But if you can go to the background, what do you take PS for?

If the background is a single color, is it possible?

BOOL Imagefill (resource image, int x, int y, int color)


Imagefill () performs an area fill with a color color at the coordinates x, y (0, 0) of the image image (that is, the same as x, Y point color, and adjacent points are populated).


If the background is a single color, is it possible?

function addbg ($SRC, $w, $h)
{
$BG = Imagecreatetruecolor ($w, $h);
$white = Imagecolorallocate ($BG, 255,255,255);
Imagefill ($BG, 0,0, $white);//Fill background

Get Target picture information
$info =getimagesize ($SRC);
$width = $info [0];//target image width
$height = $info [1];//target Image Height
Switch ($info [2]) {
Case 1:
$img = Imagecreatefromgif ($SRC);
Break
Case 2:
$img = Imagecreatefromjpeg ($SRC);
Break
Case 3:
$img = Imagecreatefrompng ($SRC);
Break
Default
Exit (' Unsupported image format ');
Break
}
if ($height < $h)
{
$x = 0;
$y = ($h-$height)/2;//Vertical Center
}
if ($width < $w)
{
$x = ($w-$width)/2;//Horizontal Center
$y = 0;
}
if ($height < $h && $width < $w) {
$x = ($w-$width)/2;
$y = ($h-$height)/2;
}
Imagecopymerge ($BG, $img, $x, $y, 0,0, $width, $height, 100);
Imagejpeg ($BG, $SRC, 100);
Imagedestroy ($BG);
Imagedestroy ($IMG);
return $SRC;
}
I don't have any effect on my surrogate.
addbg ("97972188.jpg", $pieces [0], $pieces [1]);

Watermark is simple ... Go to the background more complex ...


Add watermark I have achieved, mainly to remove the background, the background is a single color can be removed also line, if it is gray, into white bottom on the line. Thank you, Master.

I'm not very familiar with image manipulation. But you can give me a little idea:

If it is a single background, then I suggest you create a transparent PNG may be faster, I remember when creating a PNG can be specified as a transparent color.

I found a piece of code, should feel more like, but how to change or no clue, how to change the good, please expert advice, thank you
$o _pic = ' 97972188.jpg ';

Levels to be processed in the image
$begin _r = 178;
$begin _g = 178;
$begin _b = 178;

List ($src _w, $src _h, $src _type) = getimagesize ($o _pic);//Get Original image information

$file _ext = Get_ext ($o _pic);//Get extension
$target _im = Imagecreatetruecolor ($src _w, $src _h);//New diagram


if ($file _ext = = ' jpg ')//convert JPG start
{
$src _im = imagecreatefromjpeg ($o _pic);
echo $src _w;
Imagecopymerge ($target _im, $src _im,0,0,0,0, $src _w, $src _h,100);

for ($x = 0; $x < $src _w; $x + +)
{
for ($y = 0; $y < $src _h; $y + +)
{
$rgb = Imagecolorat ($src _im, $x, $y);

$r = ($rgb >>) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
Change the level value of the start setting to white
if ($r > $begin _r && $g > $begin _g && $b > $begin _b) {
Imagecolortransparent ($target _im, Imagecolorallocate ($target _im, $r, $g, $b));
}
}
}

}
Header ("Content:image/jpeg");
Imagejpeg ($target _im, ' c.jpg ');
Imagedestroy ($target _im);

Will this method be too slow? I see a pixel that is a pixel loop.

Http://stackoverflow.com/questions/16660729/php-change-background-color-to-transparent

This maybe help.

Well, it's more or less the same code as you posted.

Has the effect, but the effect is still not good, the raw edge is too many, the picture is still some color not to remove

Some colors seem to be similar, not absolutely the same, but the general eye can not distinguish, so similar color that is not good control

Some colors seem to be similar, not absolutely the same, but the general eye can not distinguish, so similar color that is not good control

I'm afraid there is no way, unless you fix it.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.