引言
图片文件读取
1. getimagesize()
list($width, $height, $type, $attr) = getimagesize('path/to/image.jpg');
2. imagecreatefromjpeg()
$image = imagecreatefromjpeg('path/to/image.jpg');
3. imagecreatefrompng()
$image = imagecreatefrompng('path/to/image.png');
4. imagecreatefromgif()
$image = imagecreatefromgif('path/to/image.gif');
图片处理
1. 调整图片大小
$destination = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($destination, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagecreatetruecolor() 函数用于创建一个空的图像资源,imagecopyresampled() 函数用于将原图像的一部分复制到新的图像资源中,并调整大小。
2. 裁剪图片
$destination = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($destination, $image, 0, 0, $x, $y, $new_width, $new_height, $width - $x, $height - $y);
3. 添加水印
$watermark = imagecreatefrompng('path/to/watermark.png');
imagecopy($image, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark));
图片保存
1. imagejpeg()
imagejpeg($image, 'path/to/image.jpg');
imagejpeg() 函数用于将图像资源保存为JPEG格式。
2. imagepng()
imagepng($image, 'path/to/image.png');
imagepng() 函数用于将图像资源保存为PNG格式。
3. imagegif()
imagegif($image, 'path/to/image.gif');
imagegif() 函数用于将图像资源保存为GIF格式。