PHP图像操作函数大全
发布时间:2023-07-06 08:07:51
PHP提供了许多图像操作函数,可以用来处理图像文件。下面将列举一些常用的图像操作函数及其用法。
1. imagecreatefromjpeg():从JPEG文件创建一副新图像。
$img = imagecreatefromjpeg('image.jpg');
2. imagecreatefrompng():从PNG文件创建一副新图像。
$img = imagecreatefrompng('image.png');
3. imagecreatefromgif():从GIF文件创建一副新图像。
$img = imagecreatefromgif('image.gif');
4. imagecreate():创建一个新的空白图像。
$img = imagecreate(500, 300);
5. imagecopy():拷贝图像的一部分到另一个图像。
imagecopy($destImg, $srcImg, $destX, $destY, $srcX, $srcY, $srcWidth, $srcHeight);
6. imagescale():按比例缩放图像。
$newImg = imagescale($img, 200, 150);
7. imagecopyresampled():重采样拷贝部分图像并调整大小。
imagecopyresampled($destImg, $srcImg, $destX, $destY, $srcX, $srcY, $destWidth, $destHeight, $srcWidth, $srcHeight);
8. imagejpeg():将图像输出到浏览器或文件。
imagejpeg($img, 'newimage.jpg');
9. imagepng():将图像输出到浏览器或文件(PNG格式)。
imagepng($img, 'newimage.png');
10. imagegif():将图像输出到浏览器或文件(GIF格式)。
imagegif($img, 'newimage.gif');
11. imagesavealpha():设置图像保存的Alpha通道。
imagesavealpha($img, true);
12. imagecolorallocate():分配一个颜色给图像。
$red = imagecolorallocate($img, 255, 0, 0);
13. imagettftext():将TrueType字体的文本写入图像。
imagettftext($img, $fontSize, $angle, $x, $y, $color, $fontFile, $text);
14. imagestring():在图像上绘制字符串。
imagestring($img, $font, $x, $y, $text, $color);
15. imagerotate():旋转图像。
$newImg = imagerotate($img, $angle, $bgColor);
以上是一些常用的PHP图像操作函数,通过使用这些函数,你可以轻松地对图像进行裁剪、缩放、旋转等操作,以及将处理后的图像保存到文件或输出到浏览器中。
