您的位置:首页 > 编程语言 > PHP开发

PHP图像处理函数:文字水印,图片水印,缩略图(等比缩放)

2013-05-05 12:37 966 查看
<?php

 /*gd图像函数大全,制作jamin

 2013.5.5

 */

 

 /*图像基本信息*/

 

 $img="city.jpg";//大图源地址

 $img_info=getimagesize($img);//获得大图信息数组

 $img_width=$img_info[0];//大图宽度

 $img_height=$img_info[1];//大图高度

 $img_form=$img_info[2];//大图格式

 

 $img_logo="logo.gif";//小图源地址

 $img_info2=getimagesize($img_logo);//获得小图信息数组

 $img_width2=$img_info2[0];//小图宽度

 $img_height2=$img_info2[1];//小图高度

 $img_form2=$img_info2[2];//小图格式

 

 /*判断图像格式并载入大图像*/

 switch($img_form){

  case 1:

  $im=imagecreatefromgif($img);

  break;

  case 2:

  $im=imagecreatefromjpeg($img);

  break;

  case 3:

  $im=imagecreatefrompng($img);

  break;

 }

 

 /*判断图像格式并载入logo图像*/

 

 switch($img_form2){

  case 1:

  $im2=imagecreatefromgif($img_logo);

  break;

  case 2:

  $im2=imagecreatefromjpeg($img_logo);

  break;

  case 3:

  $im2=imagecreatefrompng($img_logo);

  break;

 }

   

   

 

 /*颜色*/

 $white=imagecolorallocate($im,255,255,255);//白色

 $red=imagecolorallocate($im,178,034,034);//红色

 

 /*文字水印*/

 $string='www.lgdyf.com';//水印文字

 $font="jdzyj.ttf";//字体

    imagettftext($im,12,0,$img_width-200,$img_height-20,$white,$font,$string);//文字打印函数

 

 

 /*图像水印*/

 imagecopy($im,$im2,400,3,0,0,$img_width2,$img_height2);//图像水印函数

 

 

 /*--缩略图--*/

 

    /*新图宽度算法,即等比缩放算法*/

 $gd_width=500;//设定的宽度

 if($img_width>$gd_width){

  $heitht_dy=($gd_width*$img_height)/$img_width;

  $width_dy=$gd_width;

 }else{

  $heitht_dy=$img_height;

  $width_dy=$img_width;

 }

 

 $new_sl=imagecreatetruecolor($width_dy,$heitht_dy);//新建真彩图像,用于制作缩略图

 imagecopyresized($new_sl,$im,0,0,0,0,$width_dy,$heitht_dy,$img_width,$img_height);//图像剪切函数

 

 

 /*输出图片*/

 imagejpeg($new_sl,"suolue.jpg");//另存为suoluo.jpg

 imagejpeg($im,"yuan.jpg");//存储大图像

 imagejpeg($new_sl);//输出到屏幕

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐