18c01-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 64 行
PHP
64 行
<?php// We are going to insert a copyright statement onto a graphic.// Read the graphic into memory, this is a JPEG so we use that function:$gfx = imagecreatefromjpeg('eli.jpg');// Now, we want to place this statement at the bottom// so we need to determine the dimensions of our image:$width = imagesx($gfx);$height = imagesy($gfx);// Define our string we are going to print:$statement = 'Copyright 2006 - eliw.com';// We are going to use the basic built-in monospace fonts to write with:// We would like to use font size of 3. However, if that ends up being// too wide for our image, we need to go smaller. Therefore let's be// smart and do some testing on various font sizes:foreach (range(3, 1) as $fontsize) { // Calculate the width of a single character of this font: $fontw = imagefontwidth($fontsize); // Calculate the full width of this statement. $fullw = strlen($statement) * $fontw; // Is this, plus 4 to give spacing, less than our image? if ($fullw + 4 <= $width) { // Yay, it is. Break out of this loop and continue with this size. break; }}// We need to know the height of a character of this font.$fonth = imagefontheight($fontsize);// We want two colors for this. We need to display a black background// and want white text on top of that. Declare those colors via RGB:$black = imagecolorallocate($gfx, 0, 0, 0);$white = imagecolorallocate($gfx, 255, 255, 255);// We are placing our text in the bottom right corner and we want to leave// a 2 pixel space around our text, but need the black box to hit the edges// of the image. Therefore, draw the rectangle big enough to hold it.imagefilledrectangle($gfx, // The graphics object to draw on $width - $fullw - 4, // The X value of upper left corner $height - $fonth - 4, // The Y value of upper left corner $width, // The X value of lower right corner $height, // The Y value of lower right corner $black); // The color// Now, print this statement out in the bottom right corner.imagestring($gfx, // The graphics object to draw on $fontsize, // The font size to use. $width - $fullw - 2, // X value of upper left corner $height - $fonth - 2, // Y value of upper left corner $statement, // The text to print $white); // The color to do it with. // We are now done with our manipulations.// Let the browser know that we are going to output this as a PNGheader('Content-type: image/png');// And then output our new image:imagepng($gfx);?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?