PHP uses the GD library function to embed the watermark, the image becomes smaller

Source: Internet
Author: User
Tags imagecopy imagejpeg strcmp unsupported
Gentlemen
I will be a JPG image, through the Setwater function embedded watermark (picture), the number of pixels unchanged, but the total size has become a lot smaller (from 1.4M to 350k or so)!! It should be a function that affects the richness of each pixel of the image or the degree of color change. Is it imagecopy? Or a imagecreatetruecolor?
Please expert pointing!!!! Paste the Embed watermark function below:
Embed watermark program, text watermark and picture watermark Add separately!!!
function Setwater ($IMGSRC, $MARKIMG, $markText, $TextColor, $markPos, $fontType, $markType, $fontSize)
{
/*
Parameter description:

$IMGSRC: Target image, with relative directory address,
$MARKIMG: Watermark picture, with relative directory address, support PNG and GIF two formats, such as watermark picture in the execution file mark directory, can be written as: Mark/mark.gif
$markText: Watermark text added to a picture
$TextColor: Font color for watermark text
$markPos: Image watermark Add location, value range: 0~9
0: Random position, randomly pick a position between 1~8
1: Top left 2: Top Center 3: Top right 4: Left center
5: Picture Center 6: Right Center 7: Bottom left 8: Bottom Center 9: Bottom Right
$fontType: Specific font library with relative directory address
$markType: How to add a watermark to a picture, IMG represents a picture, and text means to add a watermark in text
*/
$srcInfo = @getimagesize ($IMGSRC);
$SRCIMG _w = $srcInfo [0];
$SRCIMG _h = $srcInfo [1];

Switch ($srcInfo [2])
{
Case 1:
$SRCIM = @imagecreatefromgif ($IMGSRC);
if ($srcim ==false)
echo "Open failed! ";
Break
Case 2:

$SRCIM = @imagecreatefromjpeg ($IMGSRC);

if ($srcim ==false)
echo "Open failed! ";
Break
Case 3:
$SRCIM = @imagecreatefrompng ($IMGSRC);
if ($srcim ==false)
echo "Open failed! ";
Break
Default
Die ("Unsupported image file type");
Exit
}


if (!strcmp ($markType, "img"))
{
Print_r ("
Hello! IMG ");
if (!file_exists ($markImg) | | | empty ($MARKIMG))
{
Return
}

$markImgInfo = @getimagesize ($MARKIMG);
$MARKIMG _w = $markImgInfo [0];
$MARKIMG _h = $markImgInfo [1];

if ($srcImg _w < $markImg _w | | $srcImg _h < $MARKIMG _h)
{
Return
}
Switch ($markImgInfo [2])
{
Case 1:
$markim = @imagecreatefromgif ($MARKIMG);
if ($markim ==false)
echo "Open failed! ";
Break
Case 2:
$markim = @imagecreatefromjpeg ($MARKIMG);
if ($markim ==false)
echo "Open failed! ";
Break
Case 3:
$markim = @imagecreatefrompng ($MARKIMG);
if ($markim ==false)
echo "Open failed! ";
Break
Default
Die ("Unsupported watermark image file type");
Exit
}

$logow = $MARKIMG _w;
$logoh = $MARKIMG _h;
}

if (!strcmp ($markType, "text"))
{
if (!empty ($markText))
{
if (!file_exists ($fontType))
{
Print_r ("
Hello! No Fonttype ");
Return
}
}
Else
{
Print_r ("
Hello! No Marktext ");
Return
}


$box = Imagettfbbox ($fontSize, 0, $fontType, $markText);
$logow = Max ($box [2], $box [4])-min ($box [0], $box [6]);
$logoh = Max ($box [1], $box [3])-min ($box [5], $box [7]);
}

if ($markPos = = 0)
{
$markPos = rand (1, 9);
}

Switch ($markPos)
{
Case 1:
$x = +25;
$y = +95;
Break
Case 2:
$x = ($srcImg _w-$logow)/2;
$y = +5;
Break
Case 3:
$x = $srcImg _w-$logow-105;
$y = +85;
Break
Case 4:
$x = +5;
$y = ($srcImg _h-$logoh)/2;
Break
Case 5:
$x = ($srcImg _w-$logow)/2;
$y = ($srcImg _h-$logoh)/2;
Break
Case 6:
$x = $srcImg _w-$logow-5;
$y = ($srcImg _h-$logoh)/2;
Break
Case 7:
$x = +5;
$y = $srcImg _h-$logoh-5;
Break
Case 8:
$x = ($srcImg _w-$logow)/2;
$y = $srcImg _h-$logoh-5;
Break
Case 9:
$x = $srcImg _w-$logow-5;
$y = $srcImg _h-$logoh-5;
Break
Default
Die ("This position does not support");
Exit
}

$DST _img=imagecreatetruecolor ($srcImg _w, $srcImg _h); Create a blank background, true color picture

Imagecopy ($dst _img, $SRCIM, 0, 0, 0, 0, $srcImg _w, $srcImg _h);

if (!strcmp ($markType, "img"))
{
Imagecopy ($dst _img, $markim, $x, $y, 0, 0, $logow, $logoh);
Imagedestroy ($markim);
}

if (!strcmp ($markType, "text"))
{
Print_r ("
Hello! Text2 ");
$rgb = Explode (', ', $TextColor);
$color = Imagecolorallocate ($dst _img, $rgb [0], $rgb [1], $rgb [2]);
Imagettftext ($dst _img, $fontSize, 0, $x, $y, $color, $fontType, $markText);
}

Switch ($srcInfo [2])
{
Case 1:
Imagegif ($dst _img, $IMGSRC);
Break
Case 2:
Print_r ("hello!");
Imagejpeg ($dst _img, $IMGSRC);
Break
Case 3:
Imagepng ($dst _img, $IMGSRC);
Break
Default
Die ("Unsupported watermark image file type");
Exit
}
Imagedestroy ($dst _img);
Imagedestroy ($SRCIM);

}


Reply to discussion (solution)

Imagejpeg
The third parameter sets the quality of the picture, which is not the best by default. Is this the cause of it?

Yes, thanks for the advice! Online search function, not a function of the scrutiny, just can use it. Reply

  • Related Article

    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.