php 等比例缩小图片
代码如下 | 复制代码 |
function imageresize2($width, $height, $targetw, $targeth) //方法二 if (!$max_width) //方法三 /* //单元格装得能装得进图片,则按图片的真实大小显示; |
?>
这是一款办外开的经典的php生成缩略图类函数,文件灵活实用,可以生成任何风格的图片,并且 可以把文本转换成图形。
copyright : smart-info limited. all right reserved.
author : jacky cheung
version : 1.1
create date : 24 september 2008
last modify : 15 march 2009
*/
class gd
{
var $font_face = "";
var $text = "";
var $size = 12;
var $color = "#000000";
var $angle = 0;
var $width = 0;
var $height = 0;
var $line_height = 0;
var $type = "png";
var $chmod = 0777;
var $bg_color = "#ffffff";
var $quality = 95;
var $antialias = true;
var $x = 0;
var $y = 0;
/*___| convert text to image |___*/
public function text2image ( $font_face="", $text="", $attributes=false, $width=0, $all=false )
{
$this->font_face = $font_face;
$this->text = $text;
$this->width = $width;
$this->size = 12;
$this->color = "#000000";
$this->angle = 0;
$this->line_height = 0;
$this->setprop ( $attributes );
if ( $this->width == 0 )
{
return $this->convert_text2image ( $this->text, $this->check_text_width( $this->text ) );
} else {
// word wrap
if ( $all === false )
{
$text = split(" ", $this->text);
$text = $this->word_wrap($this->text, $this->width, "<br>");
$text = split("<br>", $text );
} else if ( $all === true ) {
$temp = array();
for ( $i=0; $i<strlen($this->text); $i++ )
{
array_push ( $temp, mb_substr($this->text, $i, 1, "utf-8") );
}
$text = array();
$count_width = 0;
$i = 0;
foreach ( $temp as $k => $t )
{
$prop = $this->check_text_width($t);
if ( $count_width + floatval($prop["width"]) < $this->width )
{
$text[$i] = $text[$i] . $t;
$count_width += floatval($prop["width"]);
} else {
$count_width = 0;
$i++;
$text[$i] = "";
}
}
}
$img = array();
foreach ( $text as $k => $t )
{
$img[$k] = $this->convert_text2image ( $t, $this->check_text_width( $t ) );
}
$w = 0;
$h = 0;
foreach ( $img as $k => $v )
{
$w = (imagesx($img[$k]) > $w) ? imagesx($img[$k]) : $w;
if ($this->line_height == 0 ) $h += imagesy($img[$k]);
else $h += ($k < count($img)-1) ? $this->line_height : imagesy($img[$k]);
}
$base_img = $this->createtransparent($w, $h);
$locy = 0;
foreach ( $img as $k => $v )
{
if ($k > 0)
{
$locy = ($this->line_height == 0) ? $locy + imagesy($img[$k]) : $locy + $this->line_height;
}
$base_img = $this->attachgdimage ( $img[$k], $base_img, array ("x"=>0, "y"=>$locy) );
}
return $base_img;
}
}
private function word_wrap( $str, $width, $break )
{
$formatted = '';
$position = -1;
$prev_position = 0;
$last_line = -1;
/// looping the string stop at each space
while( $position = mb_stripos( $str, " ", ++$position, 'utf-8' ) ) {
if( $position > $last_line + $width + 1 ) {
$formatted.= mb_substr( $str, $last_line + 1, $prev_position - $last_line - 1, 'utf-8' ).$break;
$last_line = $prev_position;
}
$prev_position = $position;
}
/// adding last line without the break
$formatted.= mb_substr( $str, $last_line + 1, mb_strlen( $str ), 'utf-8' );
return $formatted;
}
public function convert_text2image ( $text, $prop )
{
$im = imagecreatetruecolor ( $prop["width"], $prop["height"] );
$rgb = $this->getrgb ( $this->color );
$color = imagecolorallocate ( $im, $rgb["red"], $rgb["green"], $rgb["blue"] );
$img = $this->createtransparent ( $prop["width"], $prop["height"] );
imagettftext ( $img, $this->size, $this->angle, 0, $prop["height"] - abs ( $prop["top"] ), $color, $this->font_face, $text );
return $img;
}
public function check_text_width ( $text )
{
$prop = array();
$bbox = imagettfbbox ( $this->size, $this->angle, $this->font_face, $text );
$prop["left"] = $bbox[0];
$prop["right"] = $bbox[2];
$prop["top"] = $bbox[1];
$prop["bottom"] = $bbox[7];
$padding = 2;
$prop["width"] = abs($prop["left"]) + abs($prop["right"]) + $padding;
$prop["height"] = abs($prop["top"]) + abs($prop["bottom"]) + $padding;
return $prop;
}
/*___| save to image file |___*/
public function save($gdimage, $filename, $attributes=false)
{
$this->type = "png";
$this->chmod = 0777;
$this->bg_color = "#ffffff";
$this->quality = 95;
$this->antialias = true;
$this->setprop ( $attributes );
// process
switch ( strtolower ( $this->type ) )
{
case "jpeg":
case "jpg":
$gdimage = $this->createbackground($gdimage, imagesx($gdimage), imagesy($gdimage));
imagejpeg ( $gdimage, $filename, $this->quality );
break;
case "gif":
$gdimage = $this->createbackground($gdimage, imagesx($gdimage), imagesy($gdimage));
imagegif ( $gdimage, $filename );
break;
case "png":
default :
imagepng ( $gdimage, $filename );
break;
}
chmod ( $filename, $this->chmod );
}
/*___| create gd background image |___*/
public function createbackground($gdimage, $width, $height)
{
$img = imagecreatetruecolor ( $width, $height );
$rgb = $this->getrgb ( $this->bg_color );
$color = imagecolorallocate ( $img, $rgb["red"], $rgb["green"], $rgb["blue"] );
imagefill ( $img, 0, 0, $color );
imagecopyresampled ( $img, $gdimage, 0, 0, 0, 0, $width, $height, $width, $height );
return $img;
}
/*___| create gd transparent image |___*/
public function createtransparent($width, $height)
{
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, false);
imagesavealpha($img, true);
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefilledrectangle($img, 0, 0, $width, $height, $transparent);
imagecopyresampled($img, $img, 0, 0, 0, 0, $width, $height, $width, $height);
return $img;
}
/*___| load image |___*/
public function createimagefrom($filename, $alpha=true)
{
if ( function_exists ( "exif_imagetype" ) )
{
if ( exif_imagetype ( $filename ) == imagetype_jpeg ) { return $this->createfromjpeg ( $filename ); }
else if ( exif_imagetype ( $filename ) == imagetype_gif ) { return $this->createfromgif ( $filename ); }
else if ( exif_imagetype ( $filename ) == imagetype_png ) { return $this->createfrompng ( $filename, $alpha ); }
}
else
{
if ( strstr ( strtoupper ( $filename ) , ".jpg" ) || strstr ( strtoupper ( $filename ), ".jpeg" )) { return $this->createfromjpeg ( $filename ); }
else if ( strstr ( strtoupper ( $filename ) , ".gif" )) { return $this->createfromgif ( $filename ); }
else if ( strstr ( strtoupper ( $filename ) , ".png" )) { return $this->createfrompng ( $filename, $alpha ); }
}
return false;
}
private function createfromjpeg ( $filename ) { return imagecreatefromjpeg ( $filename ); }
private function createfromgif ( $filename ) { return imagecreatefromgif ( $filename ); }
private function createfrompng ( $filename, $alpha=true )
{
if ( $alpha )
{
list ( $width, $height ) = getimagesize ( $filename );
$png_img = imagecreatefrompng ( $filename );
$img = imagecreatetruecolor ( $width, $height );
imagealphablending ( $img, false );
imagesavealpha ( $img, true );
imagecopyresampled ( $img, $png_img, 0, 0, 0, 0, $width, $height, $width, $height );
} else {
$img = imagecreatefrompng ( $filename );
}
return $img;
}
/*___| attach background image |___*/
public function attachbackgroundimage ( $gdimage, $filename, $attributes=false )
{
$this->x = 0;
$this->y = 0;
$this->setprop ( $attributes );
$img = $this->createimagefrom ( $filename );
imagecopyresampled ( $img, $gdimage, $this->x, $this->y, 0, 0, imagesx($gdimage), imagesy($gdimage), imagesx($gdimage), imagesy($gdimage) );
return $img;
}
/*___| attach image |___*/
public function attachimage ( $source, $target, $filename, $image_attributes=false, $attributes=false )
{
$source_img = $this->createimagefrom ( $source );
$target_img = $this->attachbackgroundimage ( $source_img, $target, $attributes );
$this->save ( $target_img, $filename, $image_attributes );
}
/*___| attach gd image resource |___*/
public function attachgdimage ( $gd_source, $gd_target, $attributes=false )
{
$this->x = 0;
$this->y = 0;
$this->width = 0;
$this->height = 0;
$this->setprop ( $attributes );
imagealphablending($gd_target, true );
imagealphablending($gd_source, true );
imagecopy ( $gd_target, $gd_source, $this->x, $this->y, 0, 0, imagesx($gd_source), imagesy($gd_source) );
return $gd_target;
}
/*___| get rgb color |___*/
public function getrgb($hex)
{
$rgb["red"] = hexdec ( substr ( $hex, 1, 2 ) ) ;
$rgb["green"] = hexdec ( substr ( $hex, 3, 2 ) ) ;
$rgb["blue"] = hexdec ( substr ( $hex, 5, 2 ) ) ;
return $rgb;
}
/*___| set properties |___*/
private function setprop ( $attributes=false )
{
if ( $attributes ) { foreach ( $attributes as $key => $value ) { $k = strtoupper ( $key ); $this->$k = $value; } }
}
}
//调用 方法
代码如下 | 复制代码 |
$imgresize = new imagetransform(); $imgresize->sourcefile = $source.$file; $imgresize->targetfile = $destination.$file; $imgresize->chmodvalue = 0777; $imgresize->resizetowidth = $tw; $imgresize->resizetoheight = $th; $imgresize->jpegoutputquality = 100; $imgresize->resizeifsmaller = false; $imgresize->resize(); |
?>
代码如下 | 复制代码 |
//adv0.jpg就是背景图片,注意函数与图片格式对应 |
生成水印方法二
代码如下 | 复制代码 |
public final class imageutils { } public final static string getpressimgpath(){ /** // 水印文件 /** public static void presstext(string presstext, string targetimg, string fontname,int fontstyle, int color, int fontsize, int x, int y) {
public static void main(string[] args) { |
代码如下 | 复制代码 |
$name = iconv("gb2312","utf-8",www.111cn.net 一聚教程网); $font = 'simhei.ttf';//水印字体 $im = imagecreatefromjpeg("test.jpg");//载入图片 $black = imagecolorallocate($im, 0, 0, 0);//设置颜色 imagettftext($im, 12, 0, 320, 84, $black, $font, $name);//打印水印 imagepng($im);//输出图片,如果要保存加个保存路径和文件名,如imagepng($im,'test1.jpg'); imagedestroy($im);//清空缓存 |
下面来看一款生成水印文字函数
代码如下 | 复制代码 |
function str2pic ($string,$source,$destination="",$f,$fontsize=10,$shadowcolor="#ffffff",$f,$x=10,$y=10) { function hex2dec($hexcolror) {#十六进制颜色转换成10进制颜色 |
在用户验证页面,如注册,登录的时候,为了加强用户登录的安全性,添加验证码验证代码,下面我为各位朋友提供一四款不同同类型的php验证代码程序,最后一款是一款使用了验证代码的实例代码。
代码如下 | 复制代码 |
date_default_timezone_set('asia/shanghai'); function setcode($len) { $code = ''; for ($i=0;$i<$len;$i++)//生成随机长度 { $code .= chr(drand()); } return $code; } //生成一个随机字符 function drand() { $rand = mt_rand(0,2); $str = ''; switch ($rand) { case 0: $str = mt_rand(48,57);break;//数字 case 1: $str = mt_rand(65,90);break;//大写字母 case 2: $str = mt_rand(97,122);break;//小写字母 } return $str; } $_session['checkcode'] = $code = setcode(5); |
//php图片验证码原代码,需支持gd2.dll扩展,需修改php.ini后重启iis or ap
代码如下 | 复制代码 |
session_start(); $key = rand(0,3); @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backcolor);//背景位置 for($i=0;$i<=100;$i++){ @imagestring($im, 3, 5, 1, $randval, $stringcolor);
//调用此页面,如果下面的式子成立,则生成验证码图片 |
//验证码通过gd生成png图片,并把$randval随机数字赋给$_session['login_check_num'],在通过用户输入的$_post进行比较,来判断是否正确。达到需要实现的功能,需要修改php.ini文件,使php支持gd库
相关文章
使用PHP+JavaScript将HTML页面转换为图片的实例分享
这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13- 下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
- Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
- 这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
- 图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
- 共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
- PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
- 这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
C#中图片旋转和翻转(RotateFlipType)用法分析
这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25- ps软件是现在很多人比较喜欢的,有着非常不错的使用效果,这次文章就给大家介绍下ps怎么制作图片阴影效果,还不知道制作方法的赶紧来看看。 ps图片阴影效果怎么做方法/...2017-07-06
- 这篇文章主要介绍了OpenCV如何去除图片中的阴影的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
- 本文主要介绍用C#实现图片转换成字节流,字节流转换成图片,并根据图片路径返回图片的字节流,有需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了JavaScript 如何禁止用户保存图片,帮助大家完成需求,更好的理解和使用JavaScript,感兴趣的朋友可以了解下...2020-11-19
- 我们在php中上传文件就必须使用#_FILE变量了,这个自动全局变量 $_FILES 从 PHP 4.1.0 版本开始被支持。在这之前,从 4.0.0 版本开始,PHP 支持 $HTTP_POST_FILES 数组。这...2016-11-25
- SwiftUI是一种使用Swift语言在苹果设备上构建用户界面的创新且简单的方式,下面这篇文章主要给大家介绍了关于SwiftUI图片缩放、拼图等处理的相关资料,需要的朋友可以参考下...2021-08-23