Home >Backend Development >PHP Tutorial >PHP image processing (Part 2)

PHP image processing (Part 2)

齐天大圣
齐天大圣Original
2020-05-03 12:45:53156browse

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 = [&#39;1&#39; => &#39;git&#39;, &#39;2&#39; => &#39;jpeg&#39;, &#39;3&#39; => &#39;png&#39;];
    
    $createFunc = &#39;imagecreatefrom&#39; . $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 = &#39;image&#39;.$types[$type];
    $output($im);
    imagedestroy($im);
}
//image(&#39;timg.jpg&#39;, &#39;jpg&#39;);
image(&#39;1.png&#39;, &#39;png&#39;);

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 => &#39;gif&#39;, 2 => &#39;jpeg&#39;, 3 => &#39;png&#39;];
    $type  = $types[$srcType];
    
    // 原图资源句柄
    $create = &#39;imagecreatefrom&#39; . $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 = &#39;image&#39; . $type;
    header("content-type:image/$type");
    $output($im);
    imagedestroy($im);
    imagedestroy($srcIm);
}
//thumb(&#39;timg.jpg&#39;, 200, 150);
thumb(&#39;timg.jpg&#39;, 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 => &#39;gif&#39;, 2 => &#39;jpeg&#39;, 3=> &#39;png&#39;];
    $type = $types[$srcType];
    // 原图资源句柄
    $create = &#39;imagecreatefrom&#39; . $type;
    $srcIm = $create($img);
    // 裁剪图片
    imagecopyresampled($im, $srcIm, 0, 0,
         $x, $y, $width, $height, $width, $height);
    // 输出图片
    $output = &#39;image&#39;.$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(&#39;timg.jpg&#39;);
$src = imagecreatefromjpeg(&#39;php.jpg&#39;);

list($srcW, $srcH) = getimagesize(&#39;php.jpg&#39;);
imagecopy($dst, $src, 0, 0, 0, 0, $srcW, $srcH);

header("content-type:image/jpeg");
imagejpeg($dst);
imagedestroy($dst);
imagedestroy($src);

PHP image processing (Part 2)

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn