当前位置:首页 » 编程语言 » php编辑图片

php编辑图片

发布时间: 2025-05-16 12:42:05

‘壹’ php图片可以等比例的缩放吗

可以。

等比例缩放的方法是:

1、载入选区--自由变换。如下图:

2、按住shift+alt键,使用鼠标调整大小,这种情况下,选区会按照等比例的方法进行缩放的。

‘贰’ PHP图片生成

给你一个php 图像处理类,完全能实现你的功能,你自己研究一下吧

<?php
class image
{
var $w_pct = 50; //透明度
var $w_quality = 80; //质量
var $w_minwidth = 300; //最小宽
var $w_minheight = 300; //最小高
var $thumb_enable; //是否生成缩略图
var $watermark_enable; //是否生水印
var $interlace = 0; //图像是否为隔行扫描的
var $fontfile; //字体文件
var $w_img ; //默认水印图

function __construct()
{
global $SITE_CONFING;
$this->thumb_enable = $SITE_CONFING['thumb_enable'];
$this->watermark_enable = $SITE_CONFING['watermark_enable'];
$this->set($SITE_CONFING['watermark_minwidth'], $SITE_CONFING['watermark_minheight'], $SITE_CONFING['watermark_quality'], $SITE_CONFING['watermark_pct'], $SITE_CONFING['watermark_fontfile'],$SITE_CONFING['watermark_img']);
}

function image()
{
$this->__construct();
}

function set($w_minwidth = 300, $w_minheight = 300, $w_quality = 80, $w_pct = 100,$fontfile,$w_img)
{
$this->w_minwidth = $w_minwidth;
$this->w_minheight = $w_minheight;
$this->w_quality = $w_quality;
$this->w_pct = $w_pct;
$this->fontfile = $fontfile;
$this->w_img = $w_img;
}

function info($img)
{
$imageinfo = getimagesize($img); //返回图像信息数组 0=>宽的像素 1=>高的像素 2=>是图像类型的标记 3 =>是文本字符串,内容为“height="yyy" width="xxx"”,
if($imageinfo === false) return false;
$imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //获取图像文件类型 $imageinfo[2]是图像类型的标记
$imagesize = filesize($img); //图像大小
$info = array(
'width'=>$imageinfo[0],
'height'=>$imageinfo[1],
'type'=>$imagetype,
'size'=>$imagesize,
'mime'=>$imageinfo['mime']
);
return $info;
}

function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 50, $suffix='_thumb', $autocut = 0)
{
if(!$this->thumb_enable || !$this->check($image)) return false;
$info = $this->info($image); //获取图片信息
if($info === false) return false;
$srcwidth = $info['width']; //源图宽
$srcheight = $info['height']; //源图高
$pathinfo = pathinfo($image);
$type = $pathinfo['extension']; //取得扩展名
if(!$type) $type = $info['type']; //如果没有取到,用$info['type']
$type = strtolower($type);
unset($info);
$scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); //获取缩略比例
//获取按照源图的比列
$createwidth = $width = (int)($srcwidth*$scale); //取得缩略宽
$createheight = $height = (int)($srcheight*$scale); //取得缩略高
$psrc_x = $psrc_y = 0;
if($autocut) //按照缩略图的比例来获取
{
if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) //如果缩略图按比列比源图窄的话
{
$width = $maxheight/$height*$width; //宽按照相应比例做处理
$height = $maxheight; //高不变
}
elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)//如果缩略图按比列比源图宽的话
{
$height = $maxwidth/$width*$height;
$width = $maxwidth;
}
$createwidth = $maxwidth;
$createheight = $maxheight;
}
$createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的图像处理函数
$srcimg = $createfun($image); //新建图像
if($type != 'gif' && function_exists('imagecreatetruecolor'))
$thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一个真彩色图像
else
$thumbimg = imagecreate($width, $height); //新建一个基于调色板的图像

if(function_exists('imageresampled')) //重采样拷贝部分图像并调整大小,真彩
//imageresampled(新图,源图,新图左上角x距离,新图左上角y距离,源图左上角x距离,源图左上角y距离,新图宽,新图高,源图宽,源图高)
imageresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
else //拷贝部分图像并调整大小,调色板
imageresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
if($type=='gif' || $type=='png')
{
//imagecolorallocate 为一幅图像分配颜色
$background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 给基于调色板的图像填充背景色, 指派一个绿色
// imagecolortransparent 将某个颜色定义为透明色
imagecolortransparent($thumbimg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
}
// imageinterlace 激活或禁止隔行扫描
if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
$imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
//imagejpeg imagegif imagepng
if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //获取文件名
//aaa.gif aaa_thumb.gif
$imagefun($thumbimg, $filename); //新建图像
imagedestroy($thumbimg); //销毁缩略图
imagedestroy($srcimg); //销毁源图
return $filename;
}
//watermark(源图,生成文件,生成位置,水印文件,水印文本,背景色)
function watermark($source, $target = '', $w_pos = 0, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc')
{
if(!$this->watermark_enable || !$this->check($source)) return false;
if(!$target) $target = $source;
if ($w_img == '' && $w_text == '')
$w_img = $this->w_img;
$source_info = getimagesize($source);
$source_w = $source_info[0]; //获取宽
$source_h = $source_info[1]; //获取高
if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false; //宽和高达不到要求直接返回
switch($source_info[2]) //新建图片
{
case 1 :
$source_img = imagecreatefromgif($source);
break;
case 2 :
$source_img = imagecreatefromjpeg($source);
break;
case 3 :
$source_img = imagecreatefrompng($source);
break;
default :
return false;
}
if(!empty($w_img) && file_exists($w_img)) //水印文件
{
$ifwaterimage = 1; //是否水印图
$water_info = getimagesize($w_img); //水印信息
$width = $water_info[0];
$height = $water_info[1];
switch($water_info[2])
{
case 1 :
$water_img = imagecreatefromgif($w_img);
break;
case 2 :
$water_img = imagecreatefromjpeg($w_img);
break;
case 3 :
$water_img = imagecreatefrompng($w_img);
break;
default :
return;
}
}
else
{
$ifwaterimage = 0;
//imagettfbbox 本函数计算并返回一个包围着 TrueType 文本范围的虚拟方框的像素大小。
//imagettfbbox ( 字体大小, 字体角度, 字体文件,文件 )
$temp = imagettfbbox(ceil($w_font*1.2), 0, $this->fontfile, $w_text);//取得使用 truetype 字体的文本的范围
$width = $temp[4] - $temp[6]; //右上角 X 位置 - 左上角 X 位置
$height = $temp[3] - $temp[5]; //右下角 Y 位置- 右上角 Y 位置
unset($temp);
}
switch($w_pos)
{
case 0: //随机位置
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
case 1: //左上角
$wx = 5;
$wy = 5;
break;
case 2: //上面中间位置
$wx = ($source_w - $width) / 2;
$wy = 0;
break;
case 3: //右上角
$wx = $source_w - $width;
$wy = 0;
break;
case 4: //左面中间位置
$wx = 0;
$wy = ($source_h - $height) / 2;
break;
case 5: //中间位置
$wx = ($source_w - $width) / 2;
$wy = ($source_h - $height) / 2;
break;
case 6: //底部中间位置
$wx = ($source_w - $width) / 2;
$wy = $source_h - $height;
break;
case 7: //左下角
$wx = 0;
$wy = $source_h - $height;
break;
case 8: //右面中间位置
$wx = $source_w - $width;
$wy = ($source_h - $height) /2;
break;
case 9: //右下角
$wx = $source_w - $width;
$wy = $source_h - $height ;
break;
default: //随机
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
}
if($ifwaterimage) //如果有水印图
{
//imagemerge 拷贝并合并图像的一部分
//参数(源图,水印图,拷贝到源图x位置,拷贝到源图y位置,从水印图x位置,从水印图y位置,高,宽,透明度)
imagemerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
}
else
{
if(!empty($w_color) && (strlen($w_color)==7))
{
$r = hexdec(substr($w_color,1,2)); //获取红色
$g = hexdec(substr($w_color,3,2)); //获取绿色
$b = hexdec(substr($w_color,5)); //获取蓝色
}
else
{
return;
}
//imagecolorallocate 基于调色板的图像填充背景色
//imagestring 水平地画一行字符串
//imagestring(源图,字体大小,位置X,位置Y,文字,颜色)
//参数($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);
//imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
}
//输出到文件或者浏览器
switch($source_info[2])
{
case 1 :
imagegif($source_img, $target); //以 GIF 格式将图像输出到浏览器或文件
break;
case 2 :
imagejpeg($source_img, $target, $this->w_quality); //以 JPEG 格式将图像输出到浏览器或文件
break;
case 3 :
imagepng($source_img, $target); //以 PNG 格式将图像输出到浏览器或文件
break;
default :
return;
}
if(isset($water_info))
{
unset($water_info); //销毁
}
if(isset($water_img))
{
imagedestroy($water_img); //销毁
}
unset($source_info);
imagedestroy($source_img);
return true;
}
//gd库必须存在,后缀为jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
function check($image)
{
return extension_loaded('gd') &&
preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) &&
file_exists($image) &&
function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
//imagecreatefromjpeg
//imagecreatefromgif
//imagecreatefrompng
}
}

/**
缩略图
1.新建一个图像资源 通过 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
2.imageresampled 拷贝图像,并调整大小

水印:图片水印,文字水印
1. 创建图像
2.加水印
图片水印:imagemerge 把2张图合并在一起
文字水印:imagettftext 向图像写入文字

*/
?>

‘叁’ 用php代码怎么以背景图片加上文字生成新的图片,然后在标题处绝对调用该图片

<?php
ob_clean(); //清除输出缓存
header("Content-type:image/jpeg"); //设置输出类型
$img="images/test.jpg"; //背景图片名
if(isset($_GET["img"]))$img=$_GET["img"]; //也可以通过img参数传入
$im=imagecreatefromjpeg($img); //读入背景图片
$text="文字内容"; //要加上的文字内容
if(isset($_GET["text"]))$text=$_GET["text"]; //也可以通过text参数传入
$fontFile="xxx.ttf"; //字体文件名,必须要
$fontSize=36; //字体尺寸
$fontColor=ImageColorAllocate($im,0,0,0); //字体颜色,这里是黑色
$textAngle=0; //文字显示的角度,0表示水平显示
$textLeft=20; //文字显示的x坐标
$textTop=60; //文字显示的y坐标
imagefttext($im,$fontSize,$textAngle,$textLeft,$textTop,$fontColor,$fontFile,$text); //把文字覆盖到图片上
Imagejpeg($im); //输出图片
ImageDestroy($im); //销毁图片
?>
把以上文字保存为php文件,比如 img.php
然后在需要调用图片的地方用 <img src="img.php?img=背景图片文件路径&text=要加上的文字"/> 来调用
比如 <img src="img.php?img=images/back.jpg&text=你好"/>

‘肆’ php怎么修改图片的尺寸大小并且覆盖原图

<?php
$imgsrc = "http://www.nowamagic.net/images/3.jpg";
$width =
780;
$height = 420;
resizejpg($imgsrc,$imgdst,$width,$height);

function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight)
{
//$imgsrc
jpg格式图像路径 $imgdst jpg格式图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度

//取得图片的宽度,高度值
$arr = getimagesize($imgsrc);
header("Content-type:
image/jpg");
$imgWidth = $imgwidth;
$imgHeight = $imgheight;
//
Create image and define colors
$imgsrc = imagecreatefromjpeg($imgsrc);

$image = imagecreatetruecolor($imgWidth, $imgHeight); //创建一个彩色的底图

imageresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0],
$arr[1]);
imagepng($image);
imagedestroy($image);
}
?>

‘伍’ php图片加字将字旋转位置

根据文字所在图片的位置方向,计算文字的位置。
php给图片添加文字水印,可控制位置,旋转,多行文字,主要包括php给图片添加文字水印可控制位置,旋转,多行文字使用实例、应用技巧、基本知识点总结和需要注意事项。

‘陆’ PHP 长方形图片 不变形 裁剪 生成 正方形

在处理图片时,我们经常会遇到将长方形图片裁剪为正方形的需求。以PHP为例,我们可以使用imageresampled()函数实现这一目标。假设我们有一个150x150像素的图片,需要将其裁剪成200x200像素的正方形,可以使用如下代码:

$x = (200-150)/2; imageresampled($thumb, $img_r2, 0, 0, $x, 0, $thumb_w, $thumb_h, 150, 150);

其中,$thumb表示目标图像,$img_r2表示源图像,$x计算出的值用于确定源图像的起始点,$thumb_w和$thumb_h为目标图像的宽度和高度,150和150分别为源图像的宽度和高度。

imageresampled()函数是PHP中用于重采样拷贝图像区域并调整大小的关键函数。其功能包括:

1. 将一幅图像中的一块正方形区域拷贝到另一个图像中。

2. 平滑地插入像素值。

3. 减小了图像的大小而仍然保持了极大的清晰度。

如果函数执行成功,返回值为TRUE,否则返回FALSE。

dst_image和src_image分别是目标图像和源图像的标识符。

当源和目标的宽度和高度不同时,会进行相应的图像收缩和拉伸。坐标指的是左上角。

此外,该函数还可以用于在同一幅图内部拷贝区域,但如果区域交迭的话则结果不可预知。

需要注意的是,对于调色板图像,重采样或过滤图像通常需要多于255种颜色,这可能会导致计算新的被重采样的像素及其颜色时产生误差。对于这种情况,建议使用真彩色图像作为目标图像,例如使用imagecreatetruecolor()创建的。

最后,imageresampled()函数需要GD库2.0.1或更高版本(推荐2.0.28及以上版本)支持。

‘柒’ 求php图片缩放处理函数

在PHP网站开发过程中,如果建立的网站涉及大量的图片处理,必然涉及到图片的上传和缩放,保持图片不失真,进行图片缩放。使用之前需要下载安装GD库,以支持PHP图片处理。下面结合代码讲解具体的PHP图片缩放处理的思路。
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);

if(($maxwidth && $pic_width > $maxwidth) ($maxheight && $pic_height > $maxheight))
{
if($maxwidth && $pic_width>$maxwidth)
{
$widthratio = $maxwidth/$pic_width;
$resizewidth_tag = true;
}

if($maxheight && $pic_height>$maxheight)
{
$heightratio = $maxheight/$pic_height;
$resizeheight_tag = true;
}

if($resizewidth_tag && $resizeheight_tag)
{
if($widthratio<$heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}

if($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;

$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;

if(function_exists("imageresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imageresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imageresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}

$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}
参数说明:
$im 图片对象,应用函数之前,需要用imagecreatefromjpeg()读取图片对象,如果PHP环境支持PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng();
$maxwidth 定义生成图片的最大宽度(单位:像素)
$maxheight 生成图片的最大高度(单位:像素)
$name 生成的图片名
$filetype 最终生成的图片类型(.jpg/.png/.gif)
代码注释:
第3~4行:读取需要缩放的图片实际宽高
第8~26行:通过计算实际图片宽高与需要生成图片的宽高的压缩比例最终得出进行图片缩放是根据宽度还是高度进行缩放,当前程序是根据宽度进行图片缩放。如果想根据高度进行图片缩放,可以将第22行的语句改成$widthratio>$heightratio
第28~31行:如果实际图片的长度或者宽度小于规定生成图片的长度或者宽度,则要么根据长度进行图片缩放,要么根据宽度进行图片缩放。
第33~34行:计算最终缩放生成的图片长宽。
第36~45行:根据计算出的最终生成图片的长宽改变图片大小,有两种改变图片大小的方法:ImageCopyResized()函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。ImageCopyResamples(),其像素插值算法得到的图像边缘比较平滑,但该函数的速度比ImageCopyResized()慢。
第47~49行:最终生成经过处理后的图片,如果需要生成GIF或PNG,需要将imagejpeg()函数改成imagegif()或imagepng()
第51~56行:如果实际图片的长宽小于规定生成的图片长宽,则保持图片原样,同理,如果需要生成GIF或PNG,需要将imagejpeg()函数改成imagegif()或imagepng()。
特别说明:
GD库1.6.2版以前支持GIF格式,但因GIF格式使用LZW算法牵涉专利权,因此在GD1.6.2版之后不支持GIF的格式。如果是WINDOWS的环境,只要进入PHP.INI文件找到extension=php_gd2.dll,将#去除,重启APACHE即可。如果是Linux环境,又想支持GIF,PNG,JPEG,需要去下载libpng,zlib,以及freetype字体并安装。
OK,PHP图片压缩函数完成,最后概述一下整个处理的思路:
通过计算实际图片的长宽与规定生成图片的长宽之间的缩放比例,根据实际的需求(按照宽度还是按照高度进行图片缩放)计算出最终生成图片的大小,然后应用PHP图片处理函数对图片进行处理,最后输出图片。
以上就是关于PHP图片处理中如何对图片进行压缩并保持不失真的函数说明。

热点内容
帝来哪个配置值得购买 发布:2025-05-16 21:12:29 浏览:461
什么是nodejs前端服务器 发布:2025-05-16 21:12:17 浏览:404
编译选项立即绑定未定义符号 发布:2025-05-16 20:55:13 浏览:905
linuxmysql慢日志 发布:2025-05-16 20:47:58 浏览:270
村两委有哪些配置 发布:2025-05-16 20:34:47 浏览:292
我的世界有什么服务器好玩的 发布:2025-05-16 20:28:57 浏览:482
c语言按位与运算 发布:2025-05-16 20:24:10 浏览:753
苹果手机如何修改密码安全 发布:2025-05-16 20:23:34 浏览:193
图片文字识别算法 发布:2025-05-16 20:21:54 浏览:46
校园ftp服务器 发布:2025-05-16 20:19:38 浏览:72