PHP GD图像处理


PHP GD图像处理


正文

查找文件 php.ini 的位置:

浏览器页面:

phpinfo();

可以使用 php --ini 来定位到 php.ini 的绝对路径。

命令行:

> sudo find / -name php.ini

图片缩放

<?php
/**
 * 用于对图片进行缩放并保存
 * @param $filename
 * @param $file_extension
 * @param int $width
 * @param int $height
 */
function thumb($filename, $file_extension, $width=0, $height=0)
{
    //获取原图像$filename的宽度$width_orig和高度$height_orig
    list($width_orig, $height_orig) = getimagesize($filename);
    //根据参数$width和$height值,换算出等比例缩放的高度和宽度
    if ($width && ($width_orig<$height_orig)) {
        $width = ($height/$height_orig)*$width_orig;
    } else {
        $height = ($width / $width_orig)*$height_orig;
    }

    //将原图缩放到这个新创建的图片资源中
    $image_p = imagecreatetruecolor($width, $height);
    //获取原图的图像资源
    if (strtolower($file_extension) == 'png') {
        $image = imagecreatefrompng($filename);
    } elseif (strtolower($file_extension) == 'jpg' || strtolower($file_extension) == 'jpeg') {
        $image = imagecreatefromjpeg($filename);
    }

    //使用imagecopyresampled()函数进行缩放设置
    imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);

    //将缩放后的图片$image_p保存,100(质量最佳,文件最大)
    if (strtolower($file_extension) == 'png') {
        imagepng($image_p, $filename);
    } else if(strtolower($file_extension) == 'jpg' || strtolower($file_extension) == 'jpeg') {
        imagejpeg($image_p, $filename);
    }

    imagedestroy($image_p);
    imagedestroy($image);
}

看一个封装好的例子:

<?php
class ImageService
{
    /**
     * 缩放图片处理
     * @param $filename
     * @param $file_extension
     * @param int $width
     * @param int $height
     * @return string
     */
    public static function thumbDeal($filename, $file_extension, $width = 0, $height = 0)
    {
        list($width_orig, $height_orig) = getimagesize($filename);
        if ($width && ($width_orig < $height_orig)) {
            $width = ($height / $height_orig) * $width_orig;
        } else {
            $height = ($width / $width_orig) * $height_orig;
        }

        $image_p = imagecreatetruecolor($width, $height);

        $image = null;
        if (strtolower($file_extension) == 'png') {
            $image = imagecreatefrompng($filename);
        } elseif (strtolower($file_extension) == 'jpg' || strtolower($file_extension) == 'jpeg') {
            $image = imagecreatefromjpeg($filename);
        }

        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

        ob_start();
        if (strtolower($file_extension) == 'png') {
            imagepng($image_p);
        } else if (strtolower($file_extension) == 'jpg' || strtolower($file_extension) == 'jpeg') {
            imagejpeg($image_p);
        }

        imagedestroy($image_p);
        imagedestroy($image);

        $stream = ob_get_contents();
        ob_end_clean();

        return $stream;
    }

    /**
     * 缩放图片并保存
     * @param $filename   源文件地址
     * @param $file_extension   文件类型,png、jpg、jpeg
     * @param $destination   目的文件地址
     * @param int $width    目的宽度
     * @param int $height    目的高度
     * @return bool
     */
    public static function thumbSave($filename, $file_extension, $destination, $width = 0, $height = 0)
    {
        $stream = self::thumbDeal($filename, $file_extension, $width, $height);

        $file = fopen($destination, "w");
        fwrite($file, $stream);
        fclose($file);

        return true;
    }
}

如果你并不知道图片格式是png,还是jpg呢?下面处理:

<?php
class ImageService
{
    /**
     * 缩放图片处理
     * @param $filename
     * @param int $width
     * @param int $height
     * @return string
     */
    public static function thumbDeal($filename, $width = 0, $height = 0)
    {
        list($width_orig, $height_orig) = getimagesize($filename);
        if ($width && ($width_orig < $height_orig)) {
            $width = ($height / $height_orig) * $width_orig;
        } else {
            $height = ($width / $width_orig) * $height_orig;
        }

        $image_p = imagecreatetruecolor($width, $height);

        $contents = file_get_contents($filename);
        $image = imagecreatefromstring($contents);

        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

        ob_start();
        imagepng($image_p);

        imagedestroy($image_p);
        imagedestroy($image);

        $stream = ob_get_contents();
        ob_end_clean();

        return $stream;
    }

    /**
     * 缩放图片并保存
     * @param $filename   源文件地址
     * @param $destination   目的文件地址
     * @param int $width    目的宽度
     * @param int $height    目的高度
     * @return bool
     */
    public static function thumbSave($filename, $destination, $width = 0, $height = 0)
    {
        $stream = self::thumbDeal($filename, $width, $height);

        $file = fopen($destination, "w");
        fwrite($file, $stream);
        fclose($file);

        return true;
    }
}

二维码中间粘贴

有时会碰到需求,如把微信二维码中间替换为用户头像,如下处理:

<?php
class ImageUtils
{
    /**
     * 获取图片资源
     * @param string $path
     * @return resource
     */
    public static function getGdImage($path = '')
    {
        $resource = imagecreatefromstring(file_get_contents($path));

        return $resource;
    }

    /**
     * 获取圆形的头像
     * @param string $path
     * @param int $width
     * @param int $height
     * @return resource
     */
    public static function getRoundAvatar($path = '', $width = 200, $height = 200)
    {
        $img = imagecreatetruecolor($width, $height);

        imagesavealpha($img, true);

        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);

        $originImg = self::getGdImage($path);

        // 先缩放再裁剪
        list($avatarWidth, $avatarHeight) = getimagesize($path);

        // 重新取样
        $sourceImg = imagecreatetruecolor($width, $height);
        imagecopyresampled($sourceImg, $originImg, 0, 0, 0, 0, $width, $height, $avatarWidth, $avatarHeight);

        $r = $width / 2;
        for ($x = 0; $x < $width; $x++) {
            for ($y = 0; $y < $height; $y++) {
                try {
                    $rgbColor = imagecolorat($sourceImg, $x, $y);
                } catch (\Exception $e) {
                    continue;
                }
                imagefill($img, $x, $y, $bg);  // 替换成白色
                imagecolortransparent($img, $bg);  // 将原图颜色替换为透明色
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }

        return $img;
    }

    /**
     * 获取带头像的小程序码
     * @param $qrCodePath
     * @param $avatarPath
     * @param int $width
     * @param int $height
     * @return resource
     */
    public static function genAvatarQrCode($qrCodePath, $avatarPath, $width = 200, $height = 200)
    {
        $avatarImg = self::getRoundAvatar($avatarPath, $width, $height);

        $qrCodeImg = self::getGdImage($qrCodePath);

        list($qrCodeWidth, $qrCodeHeight) = getimagesize($qrCodePath);

        $x = ($qrCodeWidth - $width) / 2;
        $y = ($qrCodeHeight - $height) / 2;

        imagecopymerge($qrCodeImg, $avatarImg, $x, $y, 0, 0, $width, $height, 100);

        return $qrCodeImg;
    }

    /**
     * 获取带头像的小程序码
     * @param $qrCodeStream  stream
     * @param $avatarPath
     * @param int $width
     * @param int $height
     * @return resource
     */
    public static function pasteAvatar($qrCodeStream, $avatarPath, $width = 200, $height = 200)
    {
        $avatarImg = self::getRoundAvatar($avatarPath, $width, $height);

        $qrCodeImg = imagecreatefromstring($qrCodeStream);

        $qrCodeWidth = imagesx($qrCodeImg);
        $qrCodeHeight = imagesy($qrCodeImg);

        $x = ($qrCodeWidth - $width) / 2;
        $y = ($qrCodeHeight - $height) / 2;

        imagecopymerge($qrCodeImg, $avatarImg, $x, $y, 0, 0, $width, $height, 100);

        return $qrCodeImg;
    }
}

或者如下:

<?php
class ImageUtils
{
    /**
     * 获取图片资源
     * @param string $path
     * @return resource
     */
    public static function getGdImage($path = '')
    {
        $resource = imagecreatefromstring(file_get_contents($path));

        return $resource;
    }

    /**
     * 获取圆形的头像
     * @param string $path
     * @return resource
     */
    public static function getRoundAvatar($path = '')
    {
        $originImg = self::getGdImage($path);
        $width = imagesx($originImg);
        $height = imagesy($originImg);

        $width = min($width, $height);
        $height = $width;

        $img = imagecreatetruecolor($width, $height);

        imagesavealpha($img, true);

        // 拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);

        imagefill($img, 0, 0, $bg);

        $r = $width / 2;  // 圆半径

        for ($x = 0; $x < $width; $x++) {
            for ($y = 0; $y < $height; $y++) {
                try {
                    $rgbColor = imagecolorat($originImg, $x, $y);
                } catch (\Exception $e) {
                    continue;
                }
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }

        return $img;
    }

    /**
     * 获取带头像的小程序码
     * @param $qrCodePath
     * @param $avatarPath
     * @return resource
     */
    public static function genAvatarQrCode($qrCodePath, $avatarPath)
    {
        $qrCodeImg = self::getGdImage($qrCodePath);
        $avatarImg = self::getRoundAvatar($avatarPath);

        // 二维码图片宽高
        list($qrCodeWidth, $qrCodeHeight) = getimagesize($qrCodePath);
        // 头像图片宽高
        $width = imagesx($avatarImg);
        $height = imagesy($avatarImg);

        $qr_width = $qrCodeWidth / 2.2;  // 组合后中间部分宽度
        $scale = $width / $qr_width;  // 宽度缩放比
        $qr_height = $height / $scale;

        $from_width = ($qrCodeWidth - $qr_width) / 2;

        // 重新组合图片并调整大小
        imagecopyresampled($qrCodeImg, $avatarImg, $from_width, $from_width, 0, 0, $qr_width, $qr_height, $width, $height);

        return $qrCodeImg;
    }
}

Gd库源码

上面用到的Gd库中的个别函数源码:

/**
 * Create a new true color image
 * @link http://php.net/manual/en/function.imagecreatetruecolor.php
 * @param int $width <p>
 * Image width.
 * </p>
 * @param int $height <p>
 * Image height.
 * </p>
 * @return resource an image resource identifier on success, false on errors.
 * @since 4.0.6
 * @since 5.0
 */
function imagecreatetruecolor ($width, $height) {}

/**
 * Create a new image from file or URL
 * @link http://php.net/manual/en/function.imagecreatefrompng.php
 * @param string $filename <p>
 * Path to the PNG image.
 * </p>
 * @return resource an image resource identifier on success, false on errors.
 * @since 4.0
 * @since 5.0
 */
function imagecreatefrompng ($filename) {}

/**
 * Create a new image from file or URL
 * @link http://php.net/manual/en/function.imagecreatefromgif.php
 * @param string $filename <p>
 * Path to the GIF image.
 * </p>
 * @return resource an image resource identifier on success, false on errors.
 * @since 4.0
 * @since 5.0
 */
function imagecreatefromgif ($filename) {}

/**
 * Create a new image from file or URL
 * @link http://php.net/manual/en/function.imagecreatefromjpeg.php
 * @param string $filename <p>
 * Path to the JPEG image.
 * </p>
 * @return resource an image resource identifier on success, false on errors.
 * @since 4.0
 * @since 5.0
 */
function imagecreatefromjpeg ($filename) {}

/**
 * Create a new image from file or URL
 * @link http://php.net/manual/en/function.imagecreatefromwbmp.php
 * @param string $filename <p>
 * Path to the WBMP image.
 * </p>
 * @return resource an image resource identifier on success, false on errors.
 * @since 4.0.1
 * @since 5.0
 */
function imagecreatefromwbmp ($filename) {}

/**
 * Create a new image from file or URL
 * @link http://php.net/manual/en/function.imagecreatefromxbm.php
 * @param string $filename <p>
 * Path to the XBM image.
 * </p>
 * @return resource an image resource identifier on success, false on errors.
 * @since 4.0.1
 * @since 5.0
 */
function imagecreatefromxbm ($filename) {}

/**
 * Create a new image from file or URL
 * @link http://php.net/manual/en/function.imagecreatefromxpm.php
 * @param string $filename <p>
 * Path to the XPM image.
 * </p>
 * @return resource an image resource identifier on success, false on errors.
 * @since 4.0.1
 * @since 5.0
 */
function imagecreatefromxpm ($filename) {}

/**
 * Create a new image from GD file or URL
 * @link http://php.net/manual/en/function.imagecreatefromgd.php
 * @param string $filename <p>
 * Path to the GD file.
 * </p>
 * @return resource an image resource identifier on success, false on errors.
 * @since 4.0.7
 * @since 5.0
 */
function imagecreatefromgd ($filename) {}

/**
 * Create a new image from GD2 file or URL
 * @link http://php.net/manual/en/function.imagecreatefromgd2.php
 * @param string $filename <p>
 * Path to the GD2 image.
 * </p>
 * @return resource an image resource identifier on success, false on errors.
 * @since 4.0.7
 * @since 5.0
 */
function imagecreatefromgd2 ($filename) {}

/**
 * Create a new image from a given part of GD2 file or URL
 * @link http://php.net/manual/en/function.imagecreatefromgd2part.php
 * @param string $filename <p>
 * Path to the GD2 image.
 * </p>
 * @param int $srcX <p>
 * x-coordinate of source point.
 * </p>
 * @param int $srcY <p>
 * y-coordinate of source point.
 * </p>
 * @param int $width <p>
 * Source width.
 * </p>
 * @param int $height <p>
 * Source height.
 * </p>
 * @return resource an image resource identifier on success, false on errors.
 * @since 4.0.7
 * @since 5.0
 */
function imagecreatefromgd2part ($filename, $srcX, $srcY, $width, $height) {}

/**
 * Create a new image from the image stream in the string
 * @link http://php.net/manual/en/function.imagecreatefromstring.php
 * @param string $image <p>
 * A string containing the image data.
 * </p>
 * @return resource An image resource will be returned on success. <b>FALSE</b> is returned if
 * the image type is unsupported, the data is not in a recognised format,
 * or the image is corrupt and cannot be loaded.
 * @since 4.0.4
 * @since 5.0
 */
function imagecreatefromstring ($image) {}

/**
 * Get image width
 * @link http://php.net/manual/en/function.imagesx.php
 * @param resource $image 
 * @return int Return the width of the image or false on 
 * errors.
 * @since 4.0
 * @since 5.0
 */
function imagesx ($image) {}

/**
 * Get image height
 * @link http://php.net/manual/en/function.imagesy.php
 * @param resource $image 
 * @return int Return the height of the image or false on 
 * errors.
 * @since 4.0
 * @since 5.0
 */
function imagesy ($image) {}

/**
 * Set the flag to save full alpha channel information (as opposed to single-color transparency) when saving PNG images
 * @link http://php.net/manual/en/function.imagesavealpha.php
 * @param resource $image 
 * @param bool $saveflag <p>
 * Whether to save the alpha channel or not. Default to false. 
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.3.2
 * @since 5.0
 */
function imagesavealpha ($image, $saveflag) {}

/**
 * Allocate a color for an image
 * @link http://php.net/manual/en/function.imagecolorallocatealpha.php
 * @param resource $image 
 * @param int $red <p>
 * Value of red component.
 * </p>
 * @param int $green <p>
 * Value of green component.
 * </p>
 * @param int $blue <p>
 * Value of blue component.
 * </p>
 * @param int $alpha <p>
 * A value between 0 and 127.
 * 0 indicates completely opaque while 
 * 127 indicates completely transparent.
 * </p>
 * @return int A color identifier or false if the allocation failed.
 * @since 4.3.2
 * @since 5.0
 */
function imagecolorallocatealpha ($image, $red, $green, $blue, $alpha) {}

/**
 * Get the index of the color of a pixel
 * @link http://php.net/manual/en/function.imagecolorat.php
 * @param resource $image 
 * @param int $x <p>
 * x-coordinate of the point.
 * </p>
 * @param int $y <p>
 * y-coordinate of the point.
 * </p>
 * @return int the index of the color.
 * @since 4.0
 * @since 5.0
 */
function imagecolorat ($image, $x, $y) {}

/**
 * Flood fill
 * @link http://php.net/manual/en/function.imagefill.php
 * @param resource $image 
 * @param int $x <p>
 * x-coordinate of start point.
 * </p>
 * @param int $y <p>
 * y-coordinate of start point.
 * </p>
 * @param int $color <p>
 * The fill color. A color identifier created with 
 * imagecolorallocate.
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0
 * @since 5.0
 */
function imagefill ($image, $x, $y, $color) {}

/**
 * Define a color as transparent
 * @link http://php.net/manual/en/function.imagecolortransparent.php
 * @param resource $image 
 * @param int $color [optional] <p>
 * A color identifier created with 
 * <b>imagecolorallocate</b>.
 * </p>
 * @return int The identifier of the new (or current, if none is specified)
 * transparent color is returned. If <i>color</i>
 * is not specified, and the image has no transparent color, the
 * returned identifier will be -1.
 * @since 4.0
 * @since 5.0
 */
function imagecolortransparent ($image, $color = null) {}

/**
 * Set a single pixel
 * @link http://php.net/manual/en/function.imagesetpixel.php
 * @param resource $image 
 * @param int $x <p>
 * x-coordinate.
 * </p>
 * @param int $y <p>
 * y-coordinate.
 * </p>
 * @param int $color <p>
 * A color identifier created with 
 * imagecolorallocate.
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0
 * @since 5.0
 */
function imagesetpixel ($image, $x, $y, $color) {}

/**
 * Copy and resize part of an image with resampling
 * @link http://php.net/manual/en/function.imagecopyresampled.php
 * @param resource $dst_image 
 * @param resource $src_image 
 * @param int $dst_x <p>
 * x-coordinate of destination point.
 * </p>
 * @param int $dst_y <p>
 * y-coordinate of destination point.
 * </p>
 * @param int $src_x <p>
 * x-coordinate of source point.
 * </p>
 * @param int $src_y <p>
 * y-coordinate of source point.
 * </p>
 * @param int $dst_w <p>
 * Destination width.
 * </p>
 * @param int $dst_h <p>
 * Destination height.
 * </p>
 * @param int $src_w <p>
 * Source width.
 * </p>
 * @param int $src_h <p>
 * Source height.
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0.6
 * @since 5.0
 */
function imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {}

/**
 * Copy and merge part of an image
 * @link http://php.net/manual/en/function.imagecopymerge.php
 * @param resource $dst_im <p>
 * Destination image link resource.
 * </p>
 * @param resource $src_im <p>
 * Source image link resource.
 * </p>
 * @param int $dst_x <p>
 * x-coordinate of destination point.
 * </p>
 * @param int $dst_y <p>
 * y-coordinate of destination point.
 * </p>
 * @param int $src_x <p>
 * x-coordinate of source point.
 * </p>
 * @param int $src_y <p>
 * y-coordinate of source point.
 * </p>
 * @param int $src_w <p>
 * Source width.
 * </p>
 * @param int $src_h <p>
 * Source height.
 * </p>
 * @param int $pct <p>
 * The two images will be merged according to pct
 * which can range from 0 to 100. When pct = 0,
 * no action is taken, when 100 this function behaves identically
 * to imagecopy for pallete images, while it
 * implements alpha transparency for true colour images.
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0.1
 * @since 5.0
 */
function imagecopymerge ($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) {}

/**
 * Output a PNG image to either the browser or a file
 * @link http://php.net/manual/en/function.imagepng.php
 * @param resource $image 
 * @param string $filename [optional] <p>
 * The path to save the file to. If not set or &null;, the raw image stream
 * will be outputted directly.
 * </p>
 * <p>
 * &null; is invalid if the quality and
 * filters arguments are not used.
 * </p>
 * @param int $quality [optional] <p>
 * Compression level: from 0 (no compression) to 9.
 * </p>
 * @param int $filters [optional] <p>
 * Allows reducing the PNG file size. It is a bitmask field which may be
 * set to any combination of the PNG_FILTER_XXX 
 * constants. PNG_NO_FILTER or 
 * PNG_ALL_FILTERS may also be used to respectively
 * disable or activate all filters.
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0
 * @since 5.0
 */
function imagepng ($image, $filename = null, $quality = null, $filters = null) {}

/**
 * Output image to browser or file
 * @link http://php.net/manual/en/function.imagegif.php
 * @param resource $image 
 * @param string $filename [optional] <p>
 * The path to save the file to. If not set or &null;, the raw image stream
 * will be outputted directly.
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0
 * @since 5.0
 */
function imagegif ($image, $filename = null) {}

/**
 * Output image to browser or file
 * @link http://php.net/manual/en/function.imagejpeg.php
 * @param resource $image 
 * @param string $filename [optional] <p>
 * The path to save the file to. If not set or &null;, the raw image stream
 * will be outputted directly.
 * </p>
 * <p>
 * To skip this argument in order to provide the 
 * quality parameter, use &null;.
 * </p>
 * @param int $quality [optional] <p>
 * quality is optional, and ranges from 0 (worst
 * quality, smaller file) to 100 (best quality, biggest file). The 
 * default is the default IJG quality value (about 75).
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0
 * @since 5.0
 */
function imagejpeg ($image, $filename = null, $quality = null) {}

/**
 * Output image to browser or file
 * @link http://php.net/manual/en/function.imagewbmp.php
 * @param resource $image 
 * @param string $filename [optional] <p>
 * The path to save the file to. If not set or &null;, the raw image stream
 * will be outputted directly.
 * </p>
 * @param int $foreground [optional] <p>
 * You can set the foreground color with this parameter by setting an
 * identifier obtained from imagecolorallocate.
 * The default foreground color is black.
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0.1
 * @since 5.0
 */
function imagewbmp ($image, $filename = null, $foreground = null) {}

/**
 * Output GD image to browser or file
 * @link http://php.net/manual/en/function.imagegd.php
 * @param resource $image 
 * @param string $filename [optional] <p>
 * The path to save the file to. If not set or &null;, the raw image stream
 * will be outputted directly.
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0.7
 * @since 5.0
 */
function imagegd ($image, $filename = null) {}

/**
 * Output GD2 image to browser or file
 * @link http://php.net/manual/en/function.imagegd2.php
 * @param resource $image 
 * @param string $filename [optional] <p>
 * The path to save the file to. If not set or &null;, the raw image stream
 * will be outputted directly.
 * </p>
 * @param int $chunk_size [optional] <p>
 * Chunk size.
 * </p>
 * @param int $type [optional] <p>
 * Either IMG_GD2_RAW or 
 * IMG_GD2_COMPRESSED. Default is 
 * IMG_GD2_RAW.
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0.7
 * @since 5.0
 */
function imagegd2 ($image, $filename = null, $chunk_size = null, $type = null) {}

/**
 * Destroy an image
 * @link http://php.net/manual/en/function.imagedestroy.php
 * @param resource $image 
 * @return bool true on success or false on failure.
 * @since 4.0
 * @since 5.0
 */
function imagedestroy ($image) {}

/**
 * Allocate a color for an image
 * @link http://php.net/manual/en/function.imagecolorallocate.php
 * @param resource $image 
 * @param int $red <p>Value of red component.</p>
 * @param int $green <p>Value of green component.</p>
 * @param int $blue <p>Value of blue component.</p>
 * @return int A color identifier or <b>FALSE</b> if the allocation failed.
 * @since 4.0
 * @since 5.0
 */
function imagecolorallocate ($image, $red, $green, $blue) {}

函数说明

function imagecreatetruecolor ($width, $height) {}

imagecreatetruecolor() 的作用是创建一个新的图像资源,用于后续在这个基础上的操作。

function imagecolorallocate ($image, $red, $green, $blue) {}

imagecolorallocate() 的作用是获取颜色为图像分配颜色。

function imagefill ($image, $x, $y, $color) {}

imagefill() 的作用是用颜色填充图像。

function imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {}

imagecopyresampled() 的作用是把原图缩放到目标图中,$src_image 是原图,$dst_image 是图标图,后面的参数对应。 这个函数可以用来覆盖原图中的一部分。

/**
 * Get the size of an image
 * @link http://php.net/manual/en/function.getimagesize.php
 * @param string $filename <p>
 * This parameter specifies the file you wish to retrieve information
 * about. It can reference a local file or (configuration permitting) a
 * remote file using one of the supported streams. 
 * </p>
 * @param array $imageinfo [optional] <p>
 * This optional parameter allows you to extract some extended
 * information from the image file. Currently, this will return the
 * different JPG APP markers as an associative array.
 * Some programs use these APP markers to embed text information in 
 * images. A very common one is to embed 
 * IPTC information in the APP13 marker.
 * You can use the iptcparse function to parse the
 * binary APP13 marker into something readable.
 * </p>
 * @return array|bool an array with 7 elements.
 * </p>
 * <p>
 * Index 0 and 1 contains respectively the width and the height of the image.
 * </p>
 * <p>
 * Some formats may contain no image or may contain multiple images. In these
 * cases, getimagesize might not be able to properly
 * determine the image size. getimagesize will return
 * zero for width and height in these cases.
 * </p>
 * <p>
 * Index 2 is one of the IMAGETYPE_XXX constants indicating 
 * the type of the image.
 * </p>
 * <p>
 * Index 3 is a text string with the correct 
 * height="yyy" width="xxx" string that can be used
 * directly in an IMG tag.
 * </p>
 * <p>
 * mime is the correspondant MIME type of the image.
 * This information can be used to deliver images with correct the HTTP 
 * Content-type header:
 * getimagesize and MIME types
 * ]]>
 * </p>
 * <p>
 * channels will be 3 for RGB pictures and 4 for CMYK
 * pictures.
 * </p>
 * <p>
 * bits is the number of bits for each color.
 * </p>
 * <p>
 * For some image types, the presence of channels and
 * bits values can be a bit
 * confusing. As an example, GIF always uses 3 channels
 * per pixel, but the number of bits per pixel cannot be calculated for an
 * animated GIF with a global color table.
 * </p>
 * <p>
 * On failure, false is returned.
 * @since 4.0
 * @since 5.0
 */
function getimagesize ($filename, array &$imageinfo = null) {}

getimagesize() 是PHP的基础函数,获取指定图像的大小。

/**
 * Returns information about a file path
 * @link http://php.net/manual/en/function.pathinfo.php
 * @param string $path <p>
 * The path being checked.
 * </p>
 * @param int $options [optional] <p>
 * You can specify which elements are returned with optional parameter
 * options. It composes from
 * PATHINFO_DIRNAME,
 * PATHINFO_BASENAME,
 * PATHINFO_EXTENSION and
 * PATHINFO_FILENAME. It
 * defaults to return all elements.
 * </p>
 * @return mixed The following associative array elements are returned:
 * dirname, basename,
 * extension (if any), and filename.
 * </p>
 * <p>
 * If options is used, this function will return a 
 * string if not all elements are requested.
 * @since 4.0.3
 * @since 5.0
 */
function pathinfo ($path, $options = null) {}

pathinfo() 是获取文件路径信息,$options 是一些获取参数,如 PATHINFO_EXTENSION 获取文件后缀名。

function imagecreatefrompng ($filename) {}
function imagecreatefromgif ($filename) {}
function imagecreatefromjpeg ($filename) {}
function imagecreatefromwbmp ($filename) {}
function imagecreatefromxbm ($filename) {}
function imagecreatefromxpm ($filename) {}
function imagecreatefromgd ($filename) {}
function imagecreatefromgd2 ($filename) {}
function imagecreatefromgd2part ($filename, $srcX, $srcY, $width, $height) {}

上面这些函数是从相应格式的图片文件创建新的图片资源。

function imagecreatefromstring ($image) {}

imagecreatefromstring() 是通过字符串中的图片流创建新的图片资源。

function imagepng ($image, $filename = null, $quality = null, $filters = null) {}
function imagegif ($image, $filename = null) {}
function imagejpeg ($image, $filename = null, $quality = null) {}
function imagewbmp ($image, $filename = null, $foreground = null) {}
function imagegd ($image, $filename = null) {}
function imagegd2 ($image, $filename = null, $chunk_size = null, $type = null) {}

上面这些函数是把图片资源存入相应格式的文件。

function imagedestroy ($image) {}

imagedestroy() 是释放图片资源。






参考资料

图像处理和 GD https://www.php.net/manual/zh/book.image.php

PHP图像处理(GD库) https://blog.csdn.net/wangdaxia163/article/details/98536467

tp5.1 生成小程序码 并替换 中间的 logo(默认是小程序的头像) https://blog.csdn.net/zhangwjshan/article/details/113779509


返回