Code前端首页关于Code前端联系我们

phpbmp支持,phpfmp

terry 2年前 (2023-09-30) 阅读数 27 #PHP
文章标签 PHP dirname()

本文目录一览:

  • 1、PHP怎么转换图片格式?
  • 2、我这有个已经实现了php上传图片的功能的类,但是现在还要给上传的每张图片加水印,请高手帮忙……
  • 3、php上传bmp图片类型问题
  • 4、如何在php中打开bmp格式的图片
  • 5、bmp是什么格式?bmp文件格式怎么打开?

PHP怎么转换图片格式?

用下面代码(PHP必须支持GD库)

$input=上传的BMP文件名

$output=要存的jpeg文件名

$image=imagecreatefromwbmp($input);

imagejpeg($image,$output);

imagedestroy($image);

unlink($input);

用GD库还可以加水印、改大小等,网上都有,一搜就行。我是按照PHP手册

你可以去后盾人平台看一下,里面的东西不错

我这有个已经实现了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];

$imgInfo['type']=$temp[2];

return $imgInfo;

}

//辅助图片处理,获取创建的图片资源

private function getImg($name,$imgInfo){

$src=$this-path.$name;

switch($imgInfo['type']){

case 1:

$img=imagecreatefromgif($src);

break;

case 2:

$img=imagecreatefromjpeg($src);

break;

case 3:

$img=imagecreatefrompng($src);

break;

}

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"]);

}else{

$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);

break;

case 2://jpg

$result=imageJPEG($newImg, $this-path.$newName);

break;

case 3://png

$return=imagepng($newImg, $this-path.$newName);

break;

}

imagedestroy($newImg);

return $newName;

}

/**

* 对图片加水印

* 参数对应:需水印图片 水印图片 加水印后图片名前缀

*/

function waterMark($name,$wname,$pre="wa_"){

if(file_exists($this-path.$name)){

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);

}else{

echo '水印图片尺寸大于原图片尺寸';

}

}else{

echo '水印图片'.$this-path.$wname.'不存在,请检查文件名及路径是否填写正确';

}

}else{

echo '图片'.$this-path.$name.'不存在,请检查文件名及路径是否填写正确';

}

}

//辅助图片处理,获取水印图片应处坐标

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_'){

$imgInfo=$this-getInfo($name);

$img=$this-getImg($name,$imgInfo);

$newImg=imagecreatetruecolor($width,$height);

imagecopyresampled($newImg,$img,0,0,$x,$y,$width,$height,$width,$height);

return $this-createNewImage($newImg, $pre.$name, $imgInfo);

}

}

php上传bmp图片类型问题

imagecreatefromwbmp() 这个函数 不能针对bmp文件操作,因为两个根本不是什么相似的文件类型。

php5版本还没有 imagecreatefrombmp() 这个函数(是bmp不是wbmp)

所以无法创建bmp图像。

所以那啥,要么你自己写一个imagecreatefrombmp()进行扩展。

要么就源文件上传。

如何在php中打开bmp格式的图片

如果扩展名不是故意该的话php是一个脚本语言文件。你可以用记事本打开看看里面 如果确认是图片 你可以把php改成jpg或png或bmp试试

bmp是什么格式?bmp文件格式怎么打开?

它采用位映射存储格式,除了图像深度可选以外,不采用其他任何压缩,因此,BMP文件所占用的空间很大。BMP文件的图像深度可选lbit、4bit、8bit及24bit。BMP文件存储数据时,图像的扫描方式是按从左到右、从下到上的顺序。

由于BMP文件格式是Windows环境中交换与图有关的数据的一种标准,因此在Windows环境中运行的图形图像软件都支持BMP图像格式。

典型的BMP图像文件由四部分组成:

1:位图头文件数据结构,它包含BMP图像文件的类型、显示内容等信息;

2:位图信息数据结构,它包含有BMP图像的宽、高、压缩方法,以及定义颜色等信息;

3:调色板,这个部分是可选的,有些位图需要调色板,有些位图,比如真彩色图(24位的BMP)就不需要调色板;

4:位图数据,这部分的内容根据BMP位图使用的位数不同而不同,在24位图中直接使用RGB,而其他的小于24位的使用调色板中颜色索引值。

bmp文件怎么打开?

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门