要实现的功能是,一张1000x1000的图片,修改成,大小还是1000X1000,但是是原图缩小成800X800后,放到1000X1000的画布上的图。
/**
* 更改图片画布大小
* @static public
* @param string $source 原文件名
* @param string $width $height 要生成的图片宽,高
* @param string $posX $posX 图片添加到画布位置
* @param string $$savename 修改后的图片名
* @return void
*/
function waterZoom($source, $width, $height,$posX,$posY, $savename=null) {
//检查文件是否存在
if (!file_exists($source))
return false;
$dst_im = imagecreatefromjpeg($source);
$dst_info = getimagesize($source);
$im = @imagecreatetruecolor($width, $height);
$cc = @imagecolorallocate($im,255,255,255);
imagefill($im, 0, 0, $cc);
@imagecopy($im, $dst_im, $posX, $posY, 0, 0, $dst_info[0],$dst_info[1]);
header("Content-type:image/jpeg");
if (!$savename) {
$savename = $source;
@unlink($source);
}
imagejpeg($im, $savename);
imagedestroy($im);
} 执行语句:
waterZoom($img,1000,1000,100,100);
原图是:
修改后: