php生成缩略图类

 更新时间:2016年11月25日 16:56  点击:1244
生成缩略图是当我们要上传图片时如果图片较大我们需要生成一张指定大小的小图以在列表页面显示,这样可节省资源也是现在常用的做法,下面我来分享一个php生成缩略图类,支持自定义高和宽。还支持按高和宽截图
 代码如下 复制代码

class resizeimage 

    //图片类型 
    var $type; 
    //实际宽度 
    var $width; 
    //实际高度 
    var $height; 
    //改变后的宽度 
    var $resize_width; 
    //改变后的高度 
    var $resize_height; 
    //是否裁图 
    var $cut; 
    //源图象 
    var $srcimg; 
    //目标图象地址 
    var $dstimg; 
    //临时创建的图象 
    var $im; 
    function resizeimage($img, $wid, $hei,$c,$dstpath) 
    { 
        $this->srcimg = $img; 
        $this->resize_width = $wid; 
        $this->resize_height = $hei; 
        $this->cut = $c; 
        //图片的类型 

$this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); 
        //初始化图象 
        $this->initi_img(); 
        //目标图象地址 
        $this -> dst_img($dstpath); 
        //-- 
        $this->width = imagesx($this->im); 
        $this->height = imagesy($this->im); 
        //生成图象 
        $this->newimg(); 
        ImageDestroy ($this->im); 
    } 
    function newimg() 
    { 
        //改变后的图象的比例 
        $resize_ratio = ($this->resize_width)/($this->resize_height); 
        //实际图象的比例 
        $ratio = ($this->width)/($this->height); 
        if(($this->cut)=="1") 
        //裁图 
        { 
            if($ratio>=$resize_ratio) 
            //高度优先 
            { 
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
            if($ratio<$resize_ratio) 
            //宽度优先 
            { 
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio)); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
        } 
        else
        //不裁图 
        { 
            if($ratio>=$resize_ratio) 
            { 
                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); 
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
            if($ratio<$resize_ratio) 
            { 
                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); 
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
        } 
    } 
    //初始化图象 
    function initi_img() 
    { 
        if($this->type=="jpg") 
        { 
            $this->im = imagecreatefromjpeg($this->srcimg); 
        } 
        if($this->type=="gif") 
        { 
            $this->im = imagecreatefromgif($this->srcimg); 
        } 
        if($this->type=="png") 
        { 
            $this->im = imagecreatefrompng($this->srcimg); 
        } 
    } 
    //图象目标地址 
    function dst_img($dstpath) 
    { 
        $full_length  = strlen($this->srcimg); 
        $type_length  = strlen($this->type); 
        $name_length  = $full_length-$type_length; 

        $name         = substr($this->srcimg,0,$name_length-1); 
        $this->dstimg = $dstpath; 

//echo $this->dstimg; 
    } 

$resizeimage = new resizeimage("11.jpg", "200", "150", "1","17.jpg"); 
?>

实例二

 代码如下 复制代码

<?php

/**
 * 生成缩略图
 * @author yangzhiguo0903@163.com
 * @param string     源图绝对完整地址{带文件名及后缀名}
 * @param string     目标图绝对完整地址{带文件名及后缀名}
 * @param int        缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}
 * @param int        缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}
 * @param int        是否裁切{宽,高必须非0}
 * @param int/float  缩放{0:不缩放, 0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}
 * @return boolean
 */
function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
{
    if(!is_file($src_img))
    {
        return false;
    }
    $ot = fileext($dst_img);
    $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);
    $srcinfo = getimagesize($src_img);
    $src_w = $srcinfo[0];
    $src_h = $srcinfo[1];
    $type  = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
    $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);

    $dst_h = $height;
    $dst_w = $width;
    $x = $y = 0;

    /**
     * 缩略图不超过源图尺寸(前提是宽或高只有一个)
     */
    if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0))
    {
        $proportion = 1;
    }
    if($width> $src_w)
    {
        $dst_w = $width = $src_w;
    }
    if($height> $src_h)
    {
        $dst_h = $height = $src_h;
    }

    if(!$width && !$height && !$proportion)
    {
        return false;
    }
    if(!$proportion)
    {
        if($cut == 0)
        {
            if($dst_w && $dst_h)
            {
                if($dst_w/$src_w> $dst_h/$src_h)
                {
                    $dst_w = $src_w * ($dst_h / $src_h);
                    $x = 0 - ($dst_w - $width) / 2;
                }
                else
                {
                    $dst_h = $src_h * ($dst_w / $src_w);
                    $y = 0 - ($dst_h - $height) / 2;
                }
            }
            else if($dst_w xor $dst_h)
            {
                if($dst_w && !$dst_h)  //有宽无高
                {
                    $propor = $dst_w / $src_w;
                    $height = $dst_h  = $src_h * $propor;
                }
                else if(!$dst_w && $dst_h)  //有高无宽
                {
                    $propor = $dst_h / $src_h;
                    $width  = $dst_w = $src_w * $propor;
                }
            }
        }
        else
        {
            if(!$dst_h)  //裁剪时无高
            {
                $height = $dst_h = $dst_w;
            }
            if(!$dst_w)  //裁剪时无宽
            {
                $width = $dst_w = $dst_h;
            }
            $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
            $dst_w = (int)round($src_w * $propor);
            $dst_h = (int)round($src_h * $propor);
            $x = ($width - $dst_w) / 2;
            $y = ($height - $dst_h) / 2;
        }
    }
    else
    {
        $proportion = min($proportion, 1);
        $height = $dst_h = $src_h * $proportion;
        $width  = $dst_w = $src_w * $proportion;
    }

    $src = $createfun($src_img);
    $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
    $white = imagecolorallocate($dst, 255, 255, 255);
    imagefill($dst, 0, 0, $white);

    if(function_exists('imagecopyresampled'))
    {
        imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    }
    else
    {
        imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    }
    $otfunc($dst, $dst_img);
    imagedestroy($dst);
    imagedestroy($src);
    return true;
}

使用方法

 代码如下 复制代码

$src_img = "./ROSI_050_002.JPG";
$dst_img = "./ROSI_050_002_thumb.jpg";
$stat = img2thumb($src_img, $dst_img, $width = 200, $height = 300, $cut = 0, $proportion = 0);
if($stat){
 echo 'Resize Image Success!<br />';
 echo '<img src="'.$dst_img.'" />'; 
}else{
 echo 'Resize Image Fail!'; 
}

在php中生成缩略图是程序开发中常用的,下面我找了几个不错的php生成缩略图的实现程序,有需要的朋友可使用,本人亲测绝对好用哦。

 

创建图像缩略图需要许多时间,此代码将有助于了解缩略图的逻辑。

 代码如下 复制代码

 
/**********************
*@filename - path to the image
*@tmpname - temporary path to thumbnail
*@xmax - max width
*@ymax - max height
*/
function resize_image($filename, $tmpname, $xmax, $ymax)
{
    $ext = explode(".", $filename);
    $ext = $ext[count($ext)-1];

    if($ext == "jpg" || $ext == "jpeg")
        $im = imagecreatefromjpeg($tmpname);
    elseif($ext == "png")
        $im = imagecreatefrompng($tmpname);
    elseif($ext == "gif")
        $im = imagecreatefromgif($tmpname);

    $x = imagesx($im);
    $y = imagesy($im);

    if($x <= $xmax && $y <= $ymax)
        return $im;

    if($x >= $y) {
        $newx = $xmax;
        $newy = $newx * $y / $x;
    }
    else {
        $newy = $ymax;
        $newx = $x / $y * $newy;
    }

    $im2 = imagecreatetruecolor($newx, $newy);
    imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
    return $im2;
}

例2

 代码如下 复制代码

function creat_thumbnail($img,$w,$h,$path)
{
 $org_info = getimagesize($img); //获得图像大小且是通过post传递过来的
 //var_dump($org_info);
 //Array ( [0] => 1024 [1] => 768 [2] => 3 [3] => width="1024" height="768" [bits] => 8 [mime] => image/png )
 $orig_x = $org_info[0]; //图像宽度
 $orig_y = $org_info[1]; //图像高度
 $orig_type = $org_info[2]; //图片类别即后缀 1 = GIF,2 = JPG,3 = PNG,

 //是真彩色图像
 if (function_exists("imagecreatetruecolor"))
 {
  switch($orig_type)
  {
   //从给定的gif文件名中取得的图像
   case 1  : $thumb_type = ".gif"; $_creatImage = "imagegif"; $_function = "imagecreatefromgif";
   break;
   //从给定的jpeg,jpg文件名中取得的图像
   case 2  : $thumb_type = ".jpg"; $_creatImage = "imagejpeg"; $_function = "imagecreatefromjpeg";
   break;
   //从给定的png文件名中取得的图像
   case 3  : $thumb_type = ".png"; $_creatImage = "imagepng"; $_function = "imagecreatefrompng";
   break;
  }
 }
 //如果从给定的文件名可取得的图像
 if(function_exists($_function))
 {
  $orig_image = $_function($img); //从给定的$img文件名中取得的图像
 }
 if (($orig_x / $orig_y) >= (4 / 3)) //如果宽/高 >= 4/3
 {
  $y = round($orig_y / ($orig_x / $w)); //对浮点数进行四舍五入
  $x = $w;
 }
 else //即 高/宽 >= 4/3
 {
  $x = round($orig_x / ($orig_y / $h));
  $y = $h;
 }
 $sm_image = imagecreatetruecolor($x, $y); //创建真彩色图片
 //重采样拷贝部分图像并调整大小
 Imagecopyresampled($sm_image, $orig_image, 0, 0, 0, 0, $x, $y, $orig_x, $orig_y);
 //imageJPEG($sm_image, '', 80); //在浏览器输出图像
 $tnpath = $path."/"."s_".date('YmdHis').$thumb_type; //缩略图的路径
 $thumbnail = @$_creatImage($sm_image, $tnpath, 80); //生成图片,成功返回true(或1)
 imagedestroy ($sm_image); //销毁图像
 if($thumbnail==true)
 {
  return $tnpath;
 }
}

php中要给中文加水印需要作一些简单的处理,一就是要把字符转换成gbk,还有就是要加载中文字体哦,有了这两个条件在图片加中文水印就简单了。

例1

 代码如下 复制代码

<?php
Header("Content-type: image/png");      /*通知浏览器,要输出图像*/
$im       = imagecreate(400 , 300);        /*定义图像的大小*/
$gray     = ImageColorAllocate($im , 235 , 235 , 235);
$pink     = ImageColorAllocate($im, 255 , 128 , 255);

$fontfile = "simkai.ttf";

/* $fontfile 字体的路径,视操作系统而定,可以是 simhei.ttf(黑体) , SIMKAI.TTF(楷体) , SIMFANG.TTF(仿宋) ,SIMSUN.TTC(宋体&新宋体) 等 GD 支持的中文字体*/
$str   = iconv('GB2312','UTF-8','中文水印');     /*将 gb2312 的字符集转换成 UTF-8 的字符*/
ImageTTFText($im, 30, 0, 100, 200, $pink , $fontfile , $str);
/* 加入中文水印 */
Imagepng($im);
ImageDestroy($im);
?>

例2

 代码如下 复制代码

<?php
// **************************************** //
// 功能:给图片添加文字
// 参数: $img 图片文件名
// $new_img 另存图片文件名,如果为空表示不另存图片
// $text 字符串内容
// text_size 字符串大小
// text_angle 字型串输出角度
// text_x 字符串输出 x 坐标
// text_y 字符串输出 y 坐标
// $text_font 字型文件名
// $r,$g,$b 字符串颜色RGB值
// **************************************** //
function img_text($img, $new_img, $text, $text_size, $text_angle, $text_x, $text_y, $text_font, $r, $g, $b){

$text=iconv("gb2312","UTF-8",$text);
Header("Content-type: image/gif");
$im = @imagecreatefromstring(file_get_contents($img)) or die ("打开图片失败!");
$color = ImageColorAllocate($im, $r,$g,$b);

//ImageTTFText(int im, int size, int angle, int x, int y, int col, string fontfile, string text):
//本函数将 TTF (TrueType Fonts) 字型文字写入图片。
//参数: size 为字形的尺寸;
// angle 为字型的角度,顺时针计算,0 度为水平(由左到右),90 度则为由下到上的文字;
// x,y 二参数为文字的坐标值 (原点为左上角);
// col 为字的颜色;
// fontfile 为字型文件名称;
// text 是字符串内容。
ImageTTFText($im, $text_size, $text_angle, $text_x, $text_y, $color, $text_font, $text);

if ($new_img==""):
ImageGif($im); // 不保存图片,只显示
else:
ImageGif($im,$new_img); // 保存图片,但不显示
endif;

ImageDestroy($im); //结束图形,释放内存空间
}  
?>

例3

 

 代码如下 复制代码

<?php
/*
* 功能:PHP图片水印 (水印支持图片或文字)
* 参数:
* $groundImage 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
* $waterPos 水印位置,有10种状态,0为随机位置;
* 1为顶端居左,2为顶端居中,3为顶端居右;
* 4为中部居左,5为中部居中,6为中部居右;
* 7为底端居左,8为底端居中,9为底端居右;
* $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
* $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
* $textFont 文字大小,值为1、2、3、4或5,默认为5;
* $textColor 文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
*
* 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
* $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。
* 当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。
* 加水印后的图片的文件名和 $groundImage 一样。
* 作者:longware @ 2004-11-3 14:15:13
*/
function imageWaterMark($groundImage,$waterPos=0,$waterImage=”",$waterText=”",$textFont=5,$textColor=”#FF0000″)
{
$isWaterImage = FALSE;
$formatMsg = “暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。”;

//读取水印文件
if(!emptyempty($waterImage) && file_exists($waterImage))
{
$isWaterImage = TRUE;
$water_info = getimagesize($waterImage);
$water_w = $water_info[0];//取得水印图片的宽
$water_h = $water_info[1];//取得水印图片的高

switch($water_info[2])//取得水印图片的格式
{
case 1:$water_im = imagecreatefromgif($waterImage);break;
case 2:$water_im = imagecreatefromjpeg($waterImage);break;
case 3:$water_im = imagecreatefrompng($waterImage);break;
default:die($formatMsg);
}
}

//读取背景图片
if(!emptyempty($groundImage) && file_exists($groundImage))
{
$ground_info = getimagesize($groundImage);
$ground_w = $ground_info[0];//取得背景图片的宽
$ground_h = $ground_info[1];//取得背景图片的高

switch($ground_info[2])//取得背景图片的格式
{
case 1:$ground_im = imagecreatefromgif($groundImage);break;
case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
case 3:$ground_im = imagecreatefrompng($groundImage);break;
default:die($formatMsg);
}
}
else
{
die(”需要加水印的图片不存在!”);
}

//水印位置
if($isWaterImage)//图片水印
{
$w = $water_w;
$h = $water_h;
$label = “图片的”;
}
else//文字水印
{
$temp = imagettfbbox(ceil($textFont*5),0,”./cour.ttf”,$waterText);//取得使用 TrueType 字体的文本的范围
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
unset($temp);
$label = “文字区域”;
}
if( ($ground_w<$w) || ($ground_h<$h) )
{
echo “需要加水印的图片的长度或宽度比水印”.$label.”还小,无法生成水印!”;
return;
}
switch($waterPos)
{
case 0://随机
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
case 1://1为顶端居左
$posX = 0;
$posY = 0;
break;
case 2://2为顶端居中
$posX = ($ground_w - $w) / 2;
$posY = 0;
break;
case 3://3为顶端居右
$posX = $ground_w - $w;
$posY = 0;
break;
case 4://4为中部居左
$posX = 0;
$posY = ($ground_h - $h) / 2;
break;
case 5://5为中部居中
$posX = ($ground_w - $w) / 2;
$posY = ($ground_h - $h) / 2;
break;
case 6://6为中部居右
$posX = $ground_w - $w;
$posY = ($ground_h - $h) / 2;
break;
case 7://7为底端居左
$posX = 0;
$posY = $ground_h - $h;
break;
case 8://8为底端居中
$posX = ($ground_w - $w) / 2;
$posY = $ground_h - $h;
break;
case 9://9为底端居右
$posX = $ground_w - $w;
$posY = $ground_h - $h;
break;
default://随机
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
}

//设定图像的混色模式
imagealphablending($ground_im, true);

if($isWaterImage)//图片水印
{
imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
}
else//文字水印
{
if( !emptyempty($textColor) && (strlen($textColor)==7) )
{
$R = hexdec(substr($textColor,1,2));
$G = hexdec(substr($textColor,3,2));
$B = hexdec(substr($textColor,5));
}
else
{
die(”水印文字颜色格式不正确!”);
}
imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
}

//生成水印后的图片
@unlink($groundImage);
switch($ground_info[2])//取得背景图片的格式
{
case 1:imagegif($ground_im,$groundImage);break;
case 2:imagejpeg($ground_im,$groundImage);break;
case 3:imagepng($ground_im,$groundImage);break;
default:die($errorMsg);
}

//释放内存
if(isset($water_info)) unset($water_info);
if(isset($water_im)) imagedestroy($water_im);
unset($ground_info);
imagedestroy($ground_im);
}
//—————————————————————————————
$id=$_REQUEST['id'];
$num = count($_FILES['userfile']['name']);
print_r($_FILES['userfile']);
print_r($_FILES['userfile']['name']);

echo $num;
echo “<bR>”;
if(isset($id)){
for($i=0;$i<$id;$i++){

if(isset($_FILES) && !emptyempty($_FILES['userfile']) && $_FILES['userfile']['size']>0)
{
$uploadfile = “./”.time().”_”.$_FILES['userfile'][name][$i];
echo “<br>”;
echo $uploadfile;
if (copy($_FILES['userfile']['tmp_name'][$i], $uploadfile))
{
echo “OK<br>”;

//文字水印
//imageWaterMark($uploadfile,5,”",”HTTP://www.lvye.info”,5,”#cccccc“);

//图片水印
$waterImage=”logo_ok1.gif”;//水印图片路径
imageWaterMark($uploadfile,9,$waterImage);

echo “<img src=”".$uploadfile.”” border=”0”>”;
}
else
{
echo “Fail<br>”;
}
}
}
}

?>
<form enctype=”multipart/form-data” method=”POST”>
<?php
for($a=0;$a<$id;$a++){
echo “文件: <input name=”userfile[]” type=”file”><br>”;

}
?>
<input type=”submit” value=”上传”>
</form>

文章介绍了在php生成一个透明背景的png缩略图程序,在php要中处理这个很简单我们只要用到imagealphablending($thumb,false);与imagesavealpha($thumb,true);就可以了,下面看程序。

生成PNG缩略图的时候,背景是黑色,今天又写了一个函数来弥补一下。代码很简单,就是imagealphablending($thumb,false);与imagesavealpha($thumb,true);很重要.主要就是把PNG的alpha值保存,不要丢失而已。

 

 代码如下 复制代码
<?PHP
/*
 *$sourePic:原图路径
 * $smallFileName:小图名称
 * $width:小图宽
 * $heigh:小图高
 * 转载注明 www.111cn.net */
function pngthumb($sourePic,$smallFileName,$width,$heigh){
 $image=imagecreatefrompng($sourePic);//PNG
    imagesavealpha($image,true);//这里很重要 意思是不要丢了$sourePic图像的透明色;
    $BigWidth=imagesx($image);//大图宽度
   $BigHeigh=imagesy($image);//大图高度
   $thumb = imagecreatetruecolor($width,$heigh);
   imagealphablending($thumb,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
   imagesavealpha($thumb,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
   if(imagecopyresampled($thumb,$image,0,0,0,0,$width,$heigh,$BigWidth,$BigHeigh)){
   imagepng($thumb,$smallFileName);}
   return $smallFileName;//返回小图路径 }
 
pngthumb("a.png", "c.png", 300, 300);//调用
?>

上面我们全部使用的是php自带的函数,没使用任何第三方程序了,有需要的朋友可以看看哦。

 

这里是自己的学习时的验证码图形生成的学习笔记,后来经过自己的深入学习,可以获取远程的图片到本地,不过这里需要php gd库开启哦。
 代码如下 复制代码

header("Content-type:image/png");
set_time_limit(0);//设置PHP超时时间
$url = $_GET['url'];
$url = "http://vcer.baidu.com/verify";
$imginfo = GetImageSize ( $url );  
$imgw = $imginfo [0];  
$imgh = $imginfo [1];
$bg = imagecreatetruecolor($imgw,$imgh);
$image = imagecreatefromjpeg($url);
imagecolorallocate($image,255,255,255);
imagecopy($bg,$image,0,0, 0,0,$imgw,$imgh);
imagedestroy($image);
ImagePng($bg);

获取远程验证码到本地

 代码如下 复制代码

header("Content-type:image/png");
set_time_limit(0);//设置PHP超时时间
$url = $_GET['url'];
$url = "http://vcer.baidu.com/verify";
if(empty($url)){
 echo "没有图片";
 die;
}
$imginfo = GetImageSize ( $url );  
$type = exif_imagetype($url);
$imgw = $imginfo [0];  
$imgh = $imginfo [1];
$bg = imagecreatetruecolor($imgw,$imgh);
if($type==IMAGETYPE_GIF){
 $image = imagecreatefromgif($url);
}elseif($type==IMAGETYPE_JPEG){
 $image = imagecreatefromjpeg($url);
}elseif($type==IMAGETYPE_PNG){
 $image = imagecreatefrompng($url);
}
 
imagecolorallocate($image,255,255,255);
imagecopy($bg,$image,0,0, 0,0,$imgw,$imgh);
imagedestroy($image);
ImagePng($bg);

[!--infotagslink--]

相关文章

  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • Java生成随机姓名、性别和年龄的实现示例

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
  • C#生成随机数功能示例

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php生成唯一数字id的方法汇总

    关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
  • jQuery为动态生成的select元素添加事件的方法

    下面小编就为大家带来一篇jQuery为动态生成的select元素添加事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-09-01
  • PHP自动生成后台导航网址的最佳方法

    经常制作开发不同的网站的后台,写过很多种不同的后台导航写法。 最终积累了这种最写法,算是最好的吧...2013-09-29
  • js生成随机数的方法实例

    js生成随机数主要用到了内置的Math对象的random()方法。用法如:Math.random()。它返回的是一个 0 ~ 1 之间的随机数。有了这么一个方法,那生成任意随机数就好理解了。比如实际中我们可能会有如下的需要: (1)生成一个 0 - 1...2015-10-21
  • c#生成高清缩略图的二个示例分享

    这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下...2020-06-25
  • PHP验证码生成与验证例子

    验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
  • PHP生成不同颜色、不同大小的tag标签函数

    复制代码 代码如下:function getTagStyle(){ $minFontSize=8; //最小字体大小,可根据需要自行更改 $maxFontSize=18; //最大字体大小,可根据需要自行更改 return 'font-size:'.($minFontSize+lcg_value()*(abs($maxFo...2013-10-04
  • php中利用str_pad函数生成数字递增形式的产品编号

    解决办法:$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
  • JS生成某个范围的随机数【四种情况详解】

    下面小编就为大家带来一篇JS生成某个范围的随机数【四种情况详解】。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧...2016-04-22
  • C#生成Word文档代码示例

    这篇文章主要介绍了C#生成Word文档代码示例,本文直接给出代码实例,需要的朋友可以参考下...2020-06-25
  • Vue组件文档生成工具库的方法

    本文主要介绍了Vue组件文档生成工具库的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-11
  • PHP简单实现生成txt文件到指定目录的方法

    这篇文章主要介绍了PHP简单实现生成txt文件到指定目录的方法,简单对比分析了PHP中fwrite及file_put_contents等函数的使用方法,需要的朋友可以参考下...2016-04-28
  • PHP批量生成图片缩略图(1/5)

    这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。 这款教程会用到php文件 view.php config.php funs.php index.php 功能: -------...2016-11-25
  • C#实现为一张大尺寸图片创建缩略图的方法

    这篇文章主要介绍了C#实现为一张大尺寸图片创建缩略图的方法,涉及C#创建缩略图的相关图片操作技巧,需要的朋友可以参考下...2020-06-25
  • 史上最简洁C# 生成条形码图片思路及示例分享

    这篇文章主要介绍了史上最简洁C# 生成条形码图片思路及示例分享,需要的朋友可以参考下...2020-06-25
  • php 上传文件并生成缩略图代码

    if( isset($_FILES['upImg']) ) { if( $userGroup[$loginArr['group']]['upload'] == 0 ) { echo '{"error":"您所在的用户组无权上传图片!"}'; } else...2016-11-25
  • 简单入门级php 生成xml文档代码

    $doc = new domdocument('1.0'); // we want a nice output $doc->formatoutput = true; 代码如下 复制代码 $root = $doc->createelement('bo...2016-11-25