Picture background management
Before we learned how to create a canvas, we started from a blank, and then filled it with color, drawn points, text, etc. . Now we can load the background directly from an existing image. There are several commonly used functions:
imagecreatefrompng(), imagecreatefromjpeg(), imagecreatefromgif(). They indicate loading the corresponding image format.
In addition, we learn another function, getimagesize(), which returns an array with four cells. Index 0 contains the pixel value of the image width, and index 1 contains the pixel value of the image height. Index 2 is the mark of the image type: 1 = GIF, 2 = JPG, 3 = PNG,...
<?php /** * 向不同的格式的图片中间画一个字符 */ function image($imgfile, $string) { list($width, $height, $type) = getimagesize($imgfile); $types = ['1' => 'git', '2' => 'jpeg', '3' => 'png']; $createFunc = 'imagecreatefrom' . $types[$type]; $im = $createFunc($imgfile); $textColor = imagecolorallocate($im, 255, 0, 0); $x = ($width - imagefontwidth(5) * strlen($string)) / 2; $y = ($height - imagefontheight(5)) / 2; imagestring($im, 5, $x, $y, $string, $textColor); header("Content-type:image/".$types[$type]); $output = 'image'.$types[$type]; $output($im); imagedestroy($im); } //image('timg.jpg', 'jpg'); image('1.png', 'png');
Picture scaling and cropping
The following is a function that is very powerful and can Complete image scaling and cropping functions.
The function prototype is:
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresampled() Copies a square area in one image to another image, smoothly interpolating pixel values, so, in particular, reducing The size of the image is reduced while still maintaining great clarity.
Parameters
dst_image Target image connection resource.
src_image Source image connection resource.
dst_x Target X coordinate point.
dst_y Target Y coordinate point.
src_x X coordinate point of the source.
src_y Y coordinate point of the source.
dst_w Target width.
dst_h Target height.
src_w The width of the source image.
src_h The height of the source image.
Picture thumbnail
/** * 图片等比例缩放 * @param string img 原图路径 * @param int width 新图宽度 * @param int height 新图高度 */ function thumb($img, $width = 0, $height = 0) { // 获取图片信息 list($srcW, $srcH, $srcType) = getimagesize($img); $types = [1 => 'gif', 2 => 'jpeg', 3 => 'png']; $type = $types[$srcType]; // 原图资源句柄 $create = 'imagecreatefrom' . $type; $srcIm = $create($img); $ratioOrig = $srcW / $srcH; if ($width / $height > $ratioOrig) { $width = $height * $ratioOrig; } else { $height = $width / $ratioOrig; } // 新图资源 $im = imagecreatetruecolor($width, $height); imagecopyresampled($im, $srcIm, 0, 0, 0, 0, $width, $height, $srcW, $srcH); $output = 'image' . $type; header("content-type:image/$type"); $output($im); imagedestroy($im); imagedestroy($srcIm); } //thumb('timg.jpg', 200, 150); thumb('timg.jpg', 300, 10000); // 如想按某一边等比例缩放,只要把另一边值设为无限大即可
Picture cropping
/** * @param string $img 裁剪图路径 * @param int $x 裁剪点x坐标 * @param int $y 裁剪点y坐标 * @param int $width 图片裁剪的宽度 * @param int $height 图片裁剪的高度 */ function crop ($img, $x, $y, $width, $height) { // 获取图片信息 $im = imagecreatetruecolor($width, $height); $srcType = getimagesize($img)[2]; $types = [1 => 'gif', 2 => 'jpeg', 3=> 'png']; $type = $types[$srcType]; // 原图资源句柄 $create = 'imagecreatefrom' . $type; $srcIm = $create($img); // 裁剪图片 imagecopyresampled($im, $srcIm, 0, 0, $x, $y, $width, $height, $width, $height); // 输出图片 $output = 'image'.$type; header("content-type:image/$type"); $output($im); imagedestroy($im); imagedestroy($srcIm); }
Image watermark
Using imagecopyresampled can complete the function of image watermark, but here we learn another function imagecopy.
bool imagecopy( resource $dst_im, resource $src_im, int $dst_x, int $dst_y, int $src_x, int $src_y, int $src_w, int $src_h )
Copy the part of the src_im image whose coordinates start from src_x, src_y, have a width of src_w, and a height of src_h to the positions of the coordinates of dst_x and dst_y in the dst_im image.
<?php // 图片水印 $dst = imagecreatefromjpeg('timg.jpg'); $src = imagecreatefromjpeg('php.jpg'); list($srcW, $srcH) = getimagesize('php.jpg'); imagecopy($dst, $src, 0, 0, 0, 0, $srcW, $srcH); header("content-type:image/jpeg"); imagejpeg($dst); imagedestroy($dst); imagedestroy($src);
The above is the detailed content of PHP image processing (Part 2). For more information, please follow other related articles on the PHP Chinese website!