用下面代码(PHP必须支持GD库)
$input=上传的BMP文件名
$output=要存的jpeg文件名
$image=imagecreatefromwbmp($input);
imagejpeg($image,$output);
imagedestroy($image);
unlink($input);
用GD库还可以加水印、改大小等,网上都有,一搜就行.我是按照PHP手册
你可以去后盾人平台看一下,里面的东西不错
给你一个封装的图片处理的类吧!包含缩放和剪切功能!
构造方法只需要传递图片所在路径即可!对应方法及参数都有注释!
请给予最佳答案!!
php
class Img{
private $path;
//构造方法,初始化图片信息
function __construct($path='./imgs/'){
$this-path=rtrim($path,'/').'/';
}
/**
* 对图片进行缩放
* 参数对应:文件名 缩放后宽度 缩放后高度 缩放后图片名前缀
*/
function thumb($name,$width,$height,$pre="th_"){
if(file_exists($this-path.$name)){
$imgInfo=$this-getInfo($name);
$img=$this-getImg($name,$imgInfo);
$newSize=$this-getNewSize($name,$width,$height,$imgInfo);
$newImg=$this-getNewInfo($img,$newSize,$imgInfo);
return $this-createNewImage($newImg, $pre.$name, $imgInfo);
}else{
echo '图片'.$this-path.$name.'不存在,请检查文件名及路径是否填写正确';
//辅助图片处理,获取图片的宽、高、类型属性
private function getInfo($name){
$temp=getImageSize($this-path.$name);
$imgInfo['width']=$temp[0];
$imgInfo['height']=$temp[1];
return $imgInfo;
//辅助图片处理,获取创建的图片资源
private function getImg($name,$imgInfo){
$src=$this-path.$name;
switch($imgInfo['type']){
case 1:
$img=imagecreatefromgif($src);
break;
$img=imagecreatefromjpeg($src);
$img=imagecreatefrompng($src);
return $img;
private function getNewSize($name,$width,$height,$imgInfo){
$newSize['width']=$imgInfo['width'];
$newSize['height']=$imgInfo['height'];
if($width$imgInfo['width']){
$newSize['width']=$width;
if($height$imgInfo['height']){
$newSize['height']=$height;
if($imgInfo["width"]*$newSize["width"] $imgInfo["height"] * $newSize["height"]){
$newSize["height"]=round($imgInfo["height"]*$newSize["width"]/$imgInfo["width"]);
$newSize["width"]=round($imgInfo["width"]*$newSize["height"]/$imgInfo["height"]);
print_r($newSize);
return $newSize;
//辅助图片处理,获取缩放的图片资源
private function getNewInfo($img,$newSize,$imgInfo){
$newImg=imagecreatetruecolor($newSize['height'],$newSize['height']);
$otsc=imagecolortransparent($img);
if($otsc =0 $otsc = imagecolorstotal($img)){
$tran=imagecolorsforindex($img, $otsc);
$newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]);
imagefill($newImg, 0, 0, $newt);
imagecolortransparent($newImg, $newt);
imagecopyresized($newImg, $img, 0, 0, 0, 0, $newSize["width"], $newSize["height"], $imgInfo["width"], $imgInfo["height"]);
imagedestroy($img);
return $newImg;
//辅助图片处理,创建新的图片
private function createNewImage($newImg, $newName, $imgInfo){
switch($imgInfo["type"]){
case 1://gif
$result=imageGif($newImg, $this-path.$newName);
$result=imageJPEG($newImg, $this-path.$newName);
$return=imagepng($newImg, $this-path.$newName);
imagedestroy($newImg);
return $newName;
* 对图片加水印
* 参数对应:需水印图片 水印图片 加水印后图片名前缀
function waterMark($name,$wname,$pre="wa_"){
if(file_exists($this-path.$wname)){
$info=$this-getInfo($name);
$winfo=$this-getInfo($wname);
if($p=$this-getPosition($info,$winfo)){
$img=$this-getImg($name,$info);
$wImg=$this-getImg($wname,$winfo);
imagecopy($img, $wImg, $p["x"], $p["y"], 0, 0, $winfo["width"], $winfo["height"]);
imagedestroy($wImg);
return $this-createNewImage($img,$pre.$name,$info);
echo '水印图片尺寸大于原图片尺寸';
echo '水印图片'.$this-path.$wname.'不存在,请检查文件名及路径是否填写正确';
//辅助图片处理,获取水印图片应处坐标
private function getPosition($info,$winfo){
if($info['width']$winfo['width']||$info['height']$winfo['height']){
return false;
$x=$info['width']-$winfo['width'];
$y=$info['height']-$winfo['height'];
return array('x'=$x,'y'=$y);
* 图片剪切函数
* 对应参数:原图片 X坐标 Y坐标 宽度 高度
function cut($name,$x,$y,$width,$height,$pre='cx_'){
$newImg=imagecreatetruecolor($width,$height);
imagecopyresampled($newImg,$img,0,0,$x,$y,$width,$height,$width,$height);
imagecreatefromwbmp() 这个函数 不能针对bmp文件操作,因为两个根本不是什么相似的文件类型.
所以无法创建bmp图像.
所以那啥,要么你自己写一个imagecreatefrombmp()进行扩展.
要么就源文件上传.
如果扩展名不是故意该的话php是一个脚本语言文件.你可以用记事本打开看看里面 如果确认是图片 你可以把php改成jpg或png或bmp试试
由于BMP文件格式是Windows环境中交换与图有关的数据的一种标准,所以呢在Windows环境中运行的图形图像软件都支持BMP图像格式.
典型的BMP图像文件由四部分组成:
①.:位图头文件数据结构,它包含BMP图像文件的类型、显示内容等信息;
bmp文件怎么打开?
以上就是土嘎嘎小编为大家整理的phpbmp支持相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!