php 上传图片并按比例生成指定大小图
图象缩略函数
参数说明:
代码如下 | 复制代码 |
$srcfile 原图地址; $dir 新图目录 $thumbwidth 缩小图宽最大尺寸 $thumbheitht 缩小图高最大尺寸 $ratio 默认等比例缩放 为1是缩小到固定尺寸。 */ function makethumb($srcfile,$dir,$thumbwidth,$thumbheight,$ratio=0) { //判断文件是否存在 if (!file_exists($srcfile))return false; //生成新的同名文件,但目录不同 $imgname=explode('/',$srcfile); $arrcount=count($imgname); $dstfile = $dir.$imgname[$arrcount-1]; //缩略图大小 $tow = $thumbwidth; $toh = $thumbheight; if($tow < 40) $tow = 40; if($toh < 45) $toh = 45; //获取图片信息 $im =''; if($data = getimagesize($srcfile)) { if($data[2] == 1) { $make_max = 0;//gif不处理 if(function_exists("imagecreatefromgif")) { $im = imagecreatefromgif($srcfile); } } elseif($data[2] == 2) { if(function_exists("imagecreatefromjpeg")) { $im = imagecreatefromjpeg($srcfile); } } elseif($data[2] == 3) { if(function_exists("imagecreatefrompng")) { $im = imagecreatefrompng($srcfile); } } } if(!$im) return ''; $srcw = imagesx($im); $srch = imagesy($im); $towh = $tow/$toh; $srcwh = $srcw/$srch; if($towh <= $srcwh){ $ftow = $tow; $ftoh = $ftow*($srch/$srcw); } else { $ftoh = $toh; $ftow = $ftoh*($srcw/$srch); } if($ratio){ $ftow = $tow; $ftoh = $toh; } //缩小图片 if($srcw > $tow || $srch > $toh || $ratio) { if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh)) { imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch); } elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh)) { imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch); } else { return ''; } if(function_exists('imagejpeg')) { imagejpeg($ni, $dstfile); } elseif(function_exists('imagepng')) { imagepng($ni, $dstfile); } }else { //小于尺寸直接复制 copy($srcfile,$dstfile); } imagedestroy($im); if(!file_exists($dstfile)) { return ''; } else { return $dstfile; } } |
?>
本款是一款php 读取目录下图像文件哦,代码,他利用了opendir来打开目录然后获取文件后缀名,判断是否为指定文件。代码如下 | 复制代码 |
$directory = 'gallery'; $allowed_types=array('jpg','jpeg','gif','png'); $dir_handle = @opendir($directory) or die("there is an error with your image directory!"); while ($file = readdir($dir_handle)) $title = implode('.',$file_parts); closedir($dir_handle); |
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(); |
?>
本文章收藏了四款关于利用php 等比例缩小图片代码函数,我们可定义图片宽度或高度对图片缩小或放大的图片宽度哦,好了看看四款实例那一款适合于你吧。代码如下 | 复制代码 |
function imageresize2($width, $height, $targetw, $targeth) //方法二 if (!$max_width) //方法三 /* //单元格装得能装得进图片,则按图片的真实大小显示; |
?>
代码如下 | 复制代码 |
//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) { |
相关文章
pytorch nn.Conv2d()中的padding以及输出大小方式
今天小编就为大家分享一篇pytorch nn.Conv2d()中的padding以及输出大小方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27- 本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
- 这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
- 这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
- 在本篇内容里小编给大家分享的是关于C#比较时间大小的方法总结,对此有需要的朋友们可以学习下。...2020-06-25
- 下面小编就为大家带来一篇jQuery为动态生成的select元素添加事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-09-01
- 经常制作开发不同的网站的后台,写过很多种不同的后台导航写法。 最终积累了这种最写法,算是最好的吧...2013-09-29
- 我们在php中上传文件就必须使用#_FILE变量了,这个自动全局变量 $_FILES 从 PHP 4.1.0 版本开始被支持。在这之前,从 4.0.0 版本开始,PHP 支持 $HTTP_POST_FILES 数组。这...2016-11-25
安卓利用按钮Button更改的字体大小、字体颜色、背景颜色代码
本文章来介绍在android开发中,我们通过button按钮来动态改变字体大小、字体颜色、背景颜色代码,有需要了解的朋友可参考参考。 实现的逻辑:通过遍历View的方式,判断Vi...2016-09-20- 这篇文章主要介绍了java 用itext设置pdf纸张大小操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-31
- js生成随机数主要用到了内置的Math对象的random()方法。用法如:Math.random()。它返回的是一个 0 ~ 1 之间的随机数。有了这么一个方法,那生成任意随机数就好理解了。比如实际中我们可能会有如下的需要: (1)生成一个 0 - 1...2015-10-21
- 验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
- 复制代码 代码如下:function getTagStyle(){ $minFontSize=8; //最小字体大小,可根据需要自行更改 $maxFontSize=18; //最大字体大小,可根据需要自行更改 return 'font-size:'.($minFontSize+lcg_value()*(abs($maxFo...2013-10-04
- 下面小编就为大家带来一篇JS生成某个范围的随机数【四种情况详解】。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧...2016-04-22
- matploglib能够绘制出精美的图表,有时候我们希望把一组图放在一起进行比较,就需要用到matplotlib中提供的subplot了,这篇文章主要给大家介绍了关于python中subplot大小的设置方法,需要的朋友可以参考下...2021-06-28
- 解决办法:$str=”QB”.str_pad(($maxid[0]["max(id)"]+1),5,”0″,STR_PAD_LEFT ); 其中$maxid[0]["max(id)"]+1) 是利用max函数从数据库中找也ID最大的一个值, ID为主键,不会重复。 str_pad() 函数把字符串填充为指...2013-10-04
- 这篇文章主要介绍了C++获取文件大小的4种常见技巧分享,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C#生成Word文档代码示例,本文直接给出代码实例,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了Vue组件文档生成工具库的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-11