search
HomeBackend DevelopmentPHP Tutorialphp gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码_PHP

复制代码 代码如下:
//上传文件类型列表
$uptypes=array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
$max_file_size = 200000; //上传文件大小限制, 单位BYTE
$path_im = "prod_img/"; //生成大图保存文件夹路径
$path_sim = "prod_simg/"; //缩略图保存文件夹路径
$watermark = 1; //是否加水印(1为加水印,其他为不加水印);
$watertype = 1; //水印类型(1为文字,2为图片)
$waterstring = "[url=http://www.jy17.com/]http://www.jy17.com/[/url]"; //水印字符串
$waterimg = "water.png"; //水印图片文件路径
$waterclearly = 100; //水印透明度0-100,数字小透明高
$imclearly = 100; //图片清晰度0-100,数字越大越清晰,文件尺寸越大
$simclearly = 75; //缩略图清晰度0-100,数字越大越清晰,文件尺寸越大
$smallmark = 1; //是否生成缩略图(1为加生成,其他为不);
$dst_sw = 80; //定义缩略图宽度,高度我采用等比例缩放,所以只要比较宽度就可以了
?>

上传文件:



允许上传的文件类型为:=implode(',',$uptypes)?>

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
{
echo "图片不存在!";
exit;
}
$file = $_FILES["upfile"];
if($max_file_size //检查文件大小
{ $max_file_size = $max_file_size/1000;
echo "文件太大,超过 ".$max_file_size." KB!";
exit;
}
if(!in_array($file["type"],$uptypes))
//检查文件类型
{
echo "文件类型不符!".$file["type"];
exit;
}
if(!file_exists($path_im))
//检查上传目录是否存在,不存在创建
{
mkdir($path_im);
}
if(!file_exists($path_sim))
//检查缩略图目录是否存在,不存在创建
{
mkdir($path_sim);
}
$filename = $file["tmp_name"];
$im_size = getimagesize($filename);
$src_w = $im_size[0];
$src_h = $im_size[1];
$src_type = $im_size[2];
$pinfo = pathinfo($file["name"]);
$filetype = $pinfo['extension'];
$all_path = $path_im.time().".".$filetype; //路径+文件名,目前以上传时间命名
if (file_exists($all_path))
{
echo "同名文件已经存在了";
exit;
}
if(!move_uploaded_file ($filename,$all_path))
{
echo "移动文件出错";
exit;
}
$pinfo = pathinfo($all_path);
$fname = $pinfo[basename];
echo "已经成功上传
文件名: ".$all_path."
";
echo "宽度:".$src_w."px ";
echo "长度:".$src_h."px ";
echo "
大小:".$file["size"]." bytes";
switch($src_type)//判断源图片文件类型
{
case 1://gif
$src_im = imagecreatefromgif($all_path);//从源图片文件取得图像
break;
case 2://jpg
$src_im = imagecreatefromjpeg($all_path);
break;
case 3://png
$src_im = imagecreatefrompng($all_path);
break;
//case 6:
//$src_im=imagecreatefromwbmp($all_path);
//break;
default:
die("不支持的文件类型");
exit;
}
if($watermark == 1)
{
//$iinfo = getimagesize($all_path,$iinfo);
$dst_im = imagecreatetruecolor($src_w,$src_h);
//根据原图尺寸创建一个相同大小的真彩色位图
$white = imagecolorallocate($dst_im,255,255,255);//白
//给新图填充背景色
$black = imagecolorallocate($dst_im,0,0,0);//黑
$red = imagecolorallocate($dst_im,255,0,0);//红
$orange = imagecolorallocate($dst_im,255,85,0);//橙
imagefill($dst_im,0,0,$white);
imagecopymerge($dst_im,$src_im,0,0,0,0,$src_w,$src_h,100);//原图图像写入新建真彩位图中
//imagefilledrectangle($dst_im,1,$src_h-15,80,$src_h,$white);
switch($watertype)
{
case 1: //加水印字符串
imagestring($dst_im,5,5,$src_h-20,$waterstring,$orange);//文字水印,字体5号颜色橙色,位于背景图左下角
break;
case 2: //加水印图片
$lim_size = getimagesize($waterimg); //取得水印图像尺寸,信息
switch($lim_size[2]) //判断水印图片文件类型
{
case 1://gif
$src_lim = imagecreatefromgif($waterimg); //取得水印图像
break;
case 2://jpg
$src_lim = imagecreatefromjpeg($waterimg);
break;
case 3://png
$src_lim = imagecreatefrompng($waterimg);
break;
//case 6:
//$src_im=imagecreatefromwbmp($waterimg);
//break;
default:
die("不支持的文件类型");
exit;
}
$src_lw = ($src_w-$lim_size[0])/2; //水印位于背景图正中央width定位
$src_lh = ($src_h-$lim_size[1])/2; //height定位
imagecopymerge($dst_im,$src_lim,$src_lw,$src_lh,0,0,$lim_size[0],$lim_size[1],$waterclearly);// 合并两个图像,设置水印透明度$waterclearly
imagedestroy($src_lim);
break;
}
switch($src_type)
{
case 1:
imagegif($dst_im,$all_path,$imclearly);//生成gif文件,图片清晰度0-100
break;
case 2:
imagejpeg($dst_im,$all_path,$imclearly);//生成jpg文件,图片清晰度0-100
break;
case 3:
imagepng($dst_im,$all_path,$imclearly);//生成png文件,图片清晰度0-100
break;
//case 6:
//imagewbmp($dst_im,$all_path);
break;
}
//释放缓存
imagedestroy($dst_im);
}
if($smallmark == 1)
{
$sall_path = $path_sim.time().".".$filetype;
if (file_exists($sall_path))
{
echo "同名文件已经存在了";
exit;
}
if($src_w {
$dst_sim = imagecreatetruecolor($src_w,$src_h); //新建缩略图真彩位图
imagecopymerge($dst_sim,$src_im,0,0,0,0,$src_w,$src_h,100); //原图图像写入新建真彩位图中
}
if($src_w > $dst_sw) // 原图尺寸 > 缩略图尺寸
{
$dst_sh = $dst_sw/$src_w*$src_h;
$dst_sim = imagecreatetruecolor($dst_sw,$dst_sh); //新建缩略图真彩位图(等比例缩小原图尺寸)
imagecopyresampled($dst_sim,$src_im,0,0,0,0,$dst_sw,$dst_sh,$src_w,$src_h); //原图图像写入新建真彩位图中
}
switch($src_type)
{
case 1:
imagegif($dst_sim,$sall_path,$simclearly);//生成gif文件,图片清晰度0-100
break;
case 2:
imagejpeg($dst_sim,$sall_path,$simclearly);//生成jpg文件,图片清晰度0-100
break;
case 3:
imagepng($dst_sim,$sall_path,$simclearly);//生成png文件,图片清晰度0-100
break;
//case 6:
//imagewbmp($dst_sim,$sall_path);
break;
}
//释放缓存
imagedestroy($dst_sim);
}
//释放缓存
imagedestroy($src_im);
}
?>

php等比例生成缩略图函数2
复制代码 代码如下:
function reSizeImg($imgSrc, $resize_width, $resize_height, $isCut=false) {
//图片的类型
$type = substr ( strrchr ( $imgSrc, "." ), 1 );
//初始化图象
if ($type == "jpg") {
$im = imagecreatefromjpeg ( $imgSrc );
}
if ($type == "gif") {
$im = imagecreatefromgif ( $imgSrc );
}
if ($type == "png") {
$im = imagecreatefrompng ( $imgSrc );
}
//目标图象地址
$full_length = strlen ( $imgSrc );
$type_length = strlen ( $type );
$name_length = $full_length - $type_length;
$name = substr ( $imgSrc, 0, $name_length - 1 );
$dstimg = $name . "_s." . $type;
$width = imagesx ( $im );
$height = imagesy ( $im );
//生成图象
//改变后的图象的比例
$resize_ratio = ($resize_width) / ($resize_height);
//实际图象的比例
$ratio = ($width) / ($height);
if (($isCut) == 1) //裁图
{
if ($ratio >= $resize_ratio) //高度优先
{
$newimg = imagecreatetruecolor ( $resize_width, $resize_height );
imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, (($height) * $resize_ratio), $height );
ImageJpeg ( $newimg, $dstimg );
}
if ($ratio {
$newimg = imagecreatetruecolor ( $resize_width, $resize_height );
imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, $width, (($width) / $resize_ratio) );
ImageJpeg ( $newimg, $dstimg );
}
} else //不裁图
{
if ($ratio >= $resize_ratio) {
$newimg = imagecreatetruecolor ( $resize_width, ($resize_width) / $ratio );
imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $resize_width, ($resize_width) / $ratio, $width, $height );
ImageJpeg ( $newimg, $dstimg );
}
if ($ratio $newimg = imagecreatetruecolor ( ($resize_height) * $ratio, $resize_height );
imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, ($resize_height) * $ratio, $resize_height, $width, $height );
ImageJpeg ( $newimg, $dstimg );
}
}
ImageDestroy ( $im );
}
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.