Application of PHP images-generate images with watermark text-Generate images with watermark icon-Generate thumbnails (//For comment content, without affecting file execution)

Source: Internet
Author: User
Tags imagecopy imagejpeg switch loop

<?php
////////////////Defining the watermark Text function begins//////////////////
function Watertext ($i, $t = ' All rights reserved ', $s =20, $c = ' white ', $p =9) {
$img = Imagecreatefromjpeg ($i); Imagecreatefromjpeg-creates a new image from a file or URL.

$CC = ";
Switch loop, the color of the circular watermark text begins//////
Switch ($c) {
Case ' white ':
$CC = Imagecolorallocatealpha ($img, 255,255,255,50);
Break
Case ' Red ':
$CC = Imagecolorallocatealpha ($img, 255,0,0,50);
Break
Case ' green ':
$CC = Imagecolorallocatealpha ($img, 0,255,0,50);
Break
Case ' Blue ':
$CC = Imagecolorallocatealpha ($img, 0,0,255,50);
Break
Default
$CC = Imagecolorallocatealpha ($img, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,100));
Break
}
Switch loop, the color of the loop watermark text ends//////

imagecolorallocatealpha-assigning color + alpha to an image
$c = Imagecolorallocatealpha ($img, 255,255,255,80);

Define font for watermark text
$font = DirName (__file__). '    /h.ttf '; dirname-returns the directory portion of the path

Imagettfbbox-gets the range of text that uses TrueType fonts
$r = Imagettfbbox ($s, 0, $font, iconv (' GBK ', ' utf-8 ', $t));
$TTFW = $r [2]-$r [0] + 10;
$TTFH = $r [1] + 10;


$x = Imagesx ($img)-$TTFW; imagesx-Get Image width
$y = Imagesy ($img)-$TTFH; imagesy-Get Image Height

imagettftext-writing text to an image with TrueType fonts
Imagettftext ($img, $s, 0, $x, $y, $CC, $font, Iconv (' GBK ', ' utf-8 ', $t));
Imagejpeg ($img, $i); imagejpeg-output image to browser or file.
Imagedestroy ($IMG); Imagedestroy-destroying an image
}
////////////////Define watermark text function end//////////////////


////////////////Defining the watermark icon function begins//////////////////
function Watericon ($i) {
$img = Imagecreatefromjpeg ($i); Imagecreatefromjpeg-creates a new image from a file or URL.

Find the Watermark icon file created under a folder.
$l = DirName (__file__). '    /logo.png '; dirname-returns the directory portion of the path
$logo = Imagecreatefrompng ($l); Imagecreatefrompng-creates a new image from a file or URL.


$xx = Imagesx ($logo); imagesx-Get watermark Image width
$yy = Imagesy ($logo); imagesy-Get watermark Image Height


$x = Imagesx ($img)-$xx-15; Defines the x-coordinate of the watermark icon in the picture
$y = Imagesy ($img)-$yy-15; Defines the y-coordinate of the watermark icon in the picture


Imagecopy ($img, $logo, $x, $y, 0,0, $xx, $yy); imagecopy-part of a copy image

Imagejpeg ($img, $i); imagejpeg-output image to browser or file.
Imagedestroy ($IMG); Imagedestroy-destroying an image
Imagedestroy ($logo); Imagedestroy-destroying an image
}
A.jpg s_a.jpg
////////////////define watermark icon function End//////////////////




Define the thumbnail function to start//////////
function THUMD ($i, $w =170, $h =0) {//default width is 170, Gaocheng scale
$WW = $w;
$HH = $h;

$img = Imagecreatefromjpeg ($i); Imagecreatefromjpeg-creates a new image from a file or URL.
$IW = Imagesx ($img); imagesx-Get watermark Image width
$ih = Imagesy ($img); imagesy-Get watermark Image Height

If the height is 0, the high proportionally automatically calculates
if ($h ==0) {
$HH = $w/$iw * $IH;
}

$new = Imagecreatetruecolor ($WW, $hh); imagecreatetruecolor-Create a new true color image

imagecopyresampled-resampling and resizing copies of portions of images
Imagecopyresampled ($new, $img, 0,0,0,0, $WW, $hh, $IW, $ih);

pathinfo-return the file path information
$path = PathInfo ($i, pathinfo_dirname). ' /s_ '. PathInfo ($i, pathinfo_basename);
Imagejpeg ($new, $path); imagejpeg-output image to browser or file.
Imagedestroy ($new); Imagedestroy-destroying an image
Imagedestroy ($IMG); Imagedestroy-destroying an image
}
////////define thumbnail function End//////////

Application of PHP images-generate images with watermark text-Generate images with watermark icon-Generate thumbnails (//For comment content, without affecting file execution)

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.