PHP图像处理核心函数说明
getimagesize(string $filename[, array &$imageinfo]):用于获取指定图像文件的尺寸信息。
imagecreatetruecolor(int $x_size, int $y_size):创建一个指定宽高的真彩色图像资源。
imagecreatefromjpeg(string $filename):从指定的JPEG文件或URL加载并创建一个新的图像资源。
imagecopyresized(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):将源图像的指定区域拷贝到目标图像,并在过程中调整其大小。
imagejpeg(resource $image[, string $filename[, int $quality]]):以JPEG格式将图像数据输出至浏览器或保存至文件,可选参数指定压缩质量。
具体实现代码如下:
<?php
/*
Created by http://www.cnphp.info
*/
// 定义源文件路径及缩放比例参数
//$imgfile = 'smp.jpg';
//$percent = 0.2;
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($imgfile);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$thumb = ImageCreateTrueColor($newwidth,$newheight);
$source = imagecreatefromjpeg($imgfile);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb);
?>