//图片水印 $mark_filename = './02.jpg'; //获取图片信息 $info = getimagesize($mark_filename); //获取图片的后缀 $type = image_type_to_extension($info[2], false); //拼接图片资源句柄函数 $func = 'imagecreatefrom' . $type; //创建图片资源句柄 $images = $func($mark_filename); //裁剪图片的大小为原图的4分之一 $w = $info[0] / 4; $h = $info[1] / 4; //在内存中创建画板 $mark_image = imagecreatetruecolor($w, $h); //裁剪 imagecopyresampled($mark_image, $images, 0, 0, 0, 0, $w, $h,$info[0],$info[1]); //销毁内存 imagedestroy($images); //需要加水印的图片 $filename = '11.jpg'; //获取图片信息 $mark = getimagesize($filename); //获取图片后缀 $mark_type = image_type_to_extension($mark[2], false); //创建图片资源句柄 $mark_func = 'imagecreatefrom' . $mark_type; $image = $mark_func($filename); //合并,添加水印图片 imagecopymerge($image, $mark_image, 20, 30, 0, 0, $w, $h, 50); //销毁内存 imagedestroy($mark_image); //文字水印 $font = "MSYH.TTF"; $content = "this 文字"; //设置字体的颜色及透明图 $col = imagecolorallocatealpha($image, 45, 56, 123, 50); //将文字写入图片 imagettftext($image, 20, 30, 400, 100, $col, $font, $content); //图片输出函数拼接 $outFunc = 'image'.$type; //设置文件路径 $ml = date('Ymd', time());//目录名称 $url = $ml.'/'.time().'.png';//存储的路径 //判断目录是否存在 if (file_exists($ml) == false){ //创建目录,并且设置最高权限 if (mkdir($ml, 0700, true)){ echo '创建目录成功'; }else{ echo '创建目录失败'; } } //判断是http还是https $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; //全路径 $urls = $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $url; //生成图片 if ($outFunc($image,$url)){ var_dump('压缩图片成功,图片路径为:'.$urls); }else{ var_dump('压缩图片失败'); }
参考文章:https://blog.csdn.net/Just_shunjian/article/details/78392628